minor refactoring

- var names
- logging
This commit is contained in:
Jörn-Michael Miehe 2025-06-13 16:02:37 +00:00
parent 09d22a0ad9
commit 5556a658f5
5 changed files with 30 additions and 16 deletions

View file

@ -104,7 +104,7 @@ impl AppState {
}) })
} }
fn finish_bar(&self) { fn finish_progressbar(&self) {
let mut slot = self.progress.borrow_mut(); let mut slot = self.progress.borrow_mut();
if let Some(bar) = slot.as_ref() { if let Some(bar) = slot.as_ref() {
bar.finish(); bar.finish();
@ -112,23 +112,25 @@ impl AppState {
} }
} }
pub fn upload_chunk(&mut self) -> sharry::Result<Option<()>> { pub fn upload_chunk(&mut self) -> sharry::Result<bool> {
let Some(mut uploading) = self.inner.pop_file(&self.http) else { let Some(mut uploading) = self.inner.pop_file(&self.http) else {
self.inner self.inner
.share_notify(&self.http) .share_notify(&self.http)
.unwrap_or_else(|e| warn!("Failed to notify the share: {e}")); .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 let chunk = uploading
.read(&mut self.buffer) .read(&mut self.buffer)
.map_err(ClientError::from)?; .map_err(ClientError::from)?;
debug!("{chunk:?}");
self.http.file_patch( self.http.file_patch(
chunk.get_patch_uri(), chunk.get_patch_uri(),
self.inner.alias_id(), self.inner.alias_id(),
@ -146,13 +148,13 @@ impl AppState {
drop(bar); drop(bar);
self.inner.push_file(uploading); self.inner.push_file(uploading);
Ok(Some(())) Ok(false)
} }
Err(path) => { Err(path) => {
debug!("Finished {:?}!", path.display()); debug!("Finished {:?}!", path.display());
self.finish_bar(); self.finish_progressbar();
Ok(self.inner.has_file().then_some(())) Ok(self.inner.is_empty())
} }
} }
} }

View file

@ -99,8 +99,8 @@ impl CacheFile {
self.files.iter().map(FileState::file_name).collect() self.files.iter().map(FileState::file_name).collect()
} }
pub fn has_file(&self) -> bool { pub fn is_empty(&self) -> bool {
!self.files.is_empty() self.files.is_empty()
} }
pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> { pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> {

View file

@ -1,9 +1,21 @@
use std::fmt;
pub struct Chunk<'t> { pub struct Chunk<'t> {
data: &'t [u8], data: &'t [u8],
patch_uri: &'t str, patch_uri: &'t str,
offset: u64, 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> { impl<'t> Chunk<'t> {
pub fn new(data: &'t [u8], patch_uri: &'t str, offset: u64) -> Self { pub fn new(data: &'t [u8], patch_uri: &'t str, offset: u64) -> Self {
Self { Self {

View file

@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use super::{Chunk, FileTrait}; use super::{Chunk, FileTrait};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize)]
pub struct Uploading { pub struct Uploading {
path: PathBuf, path: PathBuf,
size: u64, size: u64,
@ -16,7 +16,7 @@ pub struct Uploading {
offset: u64, offset: u64,
} }
impl fmt::Display for Uploading { impl fmt::Debug for Uploading {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,

View file

@ -94,7 +94,7 @@ fn main() {
"{} Failed to save {} state: {e}", "{} Failed to save {} state: {e}",
style("Warning:").red().bold(), style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(), style("ShrUpl").yellow().bold(),
) );
}); });
state state
} }
@ -116,14 +116,14 @@ fn main() {
loop { loop {
match state.upload_chunk() { match state.upload_chunk() {
Err(e) => error!("error: {e:?}"), // HACK handle errors better Err(e) => error!("error: {e:?}"), // HACK handle errors better
Ok(None) => { Ok(true) => {
info!("all uploads done"); info!("all uploads done");
state.clear().unwrap_or_else(|e| { state.clear().unwrap_or_else(|e| {
eprintln!( eprintln!(
"{} Failed to remove {} state: {e}", "{} Failed to remove {} state: {e}",
style("Warning:").red().bold(), style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(), style("ShrUpl").yellow().bold(),
) );
}); });
break; break;
} }
@ -135,7 +135,7 @@ fn main() {
"{} Failed to save {} state: {e}", "{} Failed to save {} state: {e}",
style("Warning:").red().bold(), style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(), style("ShrUpl").yellow().bold(),
) );
}); });
check_ctrlc(); check_ctrlc();
} }