cleanup some unwrap()

This commit is contained in:
Jörn-Michael Miehe 2025-06-12 23:28:42 +00:00
parent 4b650fd82f
commit f77acc1afd
3 changed files with 31 additions and 10 deletions

View file

@ -6,7 +6,7 @@ use std::{
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use log::debug;
use log::{debug, warn};
use super::{
cachefile::CacheFile,
@ -89,7 +89,7 @@ impl AppState {
),
style("/").magenta(),
))
.unwrap(), // safe as long as the style template is valid
.expect("style template is not valid"),
)
.with_position(uploading.get_offset())
.with_message(uploading.get_name().to_owned());
@ -99,8 +99,9 @@ impl AppState {
}
drop(slot);
// unwrap is safe: We just made sure it's `Some`.
Ref::map(self.current_bar.borrow(), |opt| opt.as_ref().unwrap())
Ref::map(self.current_bar.borrow(), |opt| {
opt.as_ref().expect("somehow the slot holds None")
})
}
fn finish_bar(&self) {
@ -113,7 +114,9 @@ impl AppState {
pub fn upload_chunk(&mut self) -> sharry::Result<Option<()>> {
let Some(mut uploading) = self.inner.pop_file(&self.http) else {
self.inner.share_notify(&self.http).unwrap(); // HACK unwrap
self.inner
.share_notify(&self.http)
.unwrap_or_else(|e| warn!("Failed to notify the share: {e}"));
return Ok(None);
};

View file

@ -108,7 +108,7 @@ impl CacheFile {
let endpoint = self
.uri
.endpoint(format!("alias/upload/{}/files/tus", self.share_id));
Some(state.start_upload(http, &endpoint, &self.alias_id).unwrap()) // HACK unwrap
Some(state.start_upload(http, &endpoint, &self.alias_id).unwrap()) // HACK unwrap, somehow retry
} else {
None
}

View file

@ -89,7 +89,13 @@ fn main() {
match AppState::from_args(&args) {
Ok(state) => {
state.save().unwrap(); // HACK unwrap
state.save().unwrap_or_else(|e| {
eprintln!(
"{} Failed to save {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
});
state
}
Err(e) => {
@ -109,16 +115,28 @@ fn main() {
loop {
match state.upload_chunk() {
Err(e) => error!("error: {e:?}"),
Err(e) => error!("error: {e:?}"), // HACK handle errors better
Ok(None) => {
info!("all uploads done");
state.clear().unwrap(); // HACK unwrap
state.clear().unwrap_or_else(|e| {
eprintln!(
"{} Failed to remove {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
});
break;
}
_ => (),
}
state.save().unwrap(); // HACK unwrap
state.save().unwrap_or_else(|e| {
eprintln!(
"{} Failed to save {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
});
check_ctrlc();
}
}