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();
if let Some(bar) = slot.as_ref() {
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 {
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())
}
}
}

View file

@ -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<file::Uploading> {

View file

@ -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 {

View file

@ -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,

View file

@ -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();
}