diff --git a/src/appstate.rs b/src/appstate.rs index 5ae3596..155fc35 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -104,7 +104,7 @@ impl AppState { }) } - fn finish_bar(&self) { + fn finish_progressbar(&self) { let mut slot = self.progress.borrow_mut(); if let Some(bar) = slot.as_ref() { bar.finish(); @@ -112,23 +112,25 @@ impl AppState { } } - pub fn upload_chunk(&mut self) -> sharry::Result> { + pub fn upload_chunk(&mut self) -> sharry::Result { let Some(mut uploading) = self.inner.pop_file(&self.http) else { self.inner .share_notify(&self.http) .unwrap_or_else(|e| warn!("Failed to notify the share: {e}")); - return Ok(None); + return Ok(true); }; - self.get_or_create_progressbar(&uploading); + debug!("{uploading:?}"); - debug!("{uploading} chunk {}", self.buffer.len()); + self.get_or_create_progressbar(&uploading); let chunk = uploading .read(&mut self.buffer) .map_err(ClientError::from)?; + debug!("{chunk:?}"); + self.http.file_patch( chunk.get_patch_uri(), self.inner.alias_id(), @@ -146,13 +148,13 @@ impl AppState { drop(bar); self.inner.push_file(uploading); - Ok(Some(())) + Ok(false) } Err(path) => { debug!("Finished {:?}!", path.display()); - self.finish_bar(); + self.finish_progressbar(); - Ok(self.inner.has_file().then_some(())) + Ok(self.inner.is_empty()) } } } diff --git a/src/cachefile.rs b/src/cachefile.rs index 6f11312..164f4ec 100644 --- a/src/cachefile.rs +++ b/src/cachefile.rs @@ -99,8 +99,8 @@ impl CacheFile { self.files.iter().map(FileState::file_name).collect() } - pub fn has_file(&self) -> bool { - !self.files.is_empty() + pub fn is_empty(&self) -> bool { + self.files.is_empty() } pub fn pop_file(&mut self, http: &impl Client) -> Option { diff --git a/src/file/chunk.rs b/src/file/chunk.rs index 838e66b..8f69dd2 100644 --- a/src/file/chunk.rs +++ b/src/file/chunk.rs @@ -1,9 +1,21 @@ +use std::fmt; + pub struct Chunk<'t> { data: &'t [u8], patch_uri: &'t str, offset: u64, } +impl fmt::Debug for Chunk<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Chunk") + .field("patch_uri", &self.patch_uri) + .field("offset", &self.offset) + .field("data.len()", &self.data.len()) + .finish_non_exhaustive() + } +} + impl<'t> Chunk<'t> { pub fn new(data: &'t [u8], patch_uri: &'t str, offset: u64) -> Self { Self { diff --git a/src/file/uploading.rs b/src/file/uploading.rs index 962c2c6..26a83e7 100644 --- a/src/file/uploading.rs +++ b/src/file/uploading.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; use super::{Chunk, FileTrait}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize)] pub struct Uploading { path: PathBuf, size: u64, @@ -16,7 +16,7 @@ pub struct Uploading { offset: u64, } -impl fmt::Display for Uploading { +impl fmt::Debug for Uploading { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, diff --git a/src/main.rs b/src/main.rs index d9b5bb8..38b799d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,7 +94,7 @@ fn main() { "{} Failed to save {} state: {e}", style("Warning:").red().bold(), style("ShrUpl").yellow().bold(), - ) + ); }); state } @@ -116,14 +116,14 @@ fn main() { loop { match state.upload_chunk() { Err(e) => error!("error: {e:?}"), // HACK handle errors better - Ok(None) => { + Ok(true) => { info!("all uploads done"); state.clear().unwrap_or_else(|e| { eprintln!( "{} Failed to remove {} state: {e}", style("Warning:").red().bold(), style("ShrUpl").yellow().bold(), - ) + ); }); break; } @@ -135,7 +135,7 @@ fn main() { "{} Failed to save {} state: {e}", style("Warning:").red().bold(), style("ShrUpl").yellow().bold(), - ) + ); }); check_ctrlc(); }