From 36ad1263b419c8dc439ba9893f62c1a1e0d7fde5 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Wed, 31 Mar 2021 17:30:16 +0200 Subject: [PATCH] Reduced panics --- src/dlreport.rs | 12 ++++-------- src/download.rs | 14 +++++++------- src/main.rs | 50 ++++++++++++++++++++++++++++--------------------- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/dlreport.rs b/src/dlreport.rs index 9c321bc..b69cf95 100644 --- a/src/dlreport.rs +++ b/src/dlreport.rs @@ -28,9 +28,7 @@ pub enum DlStatus { DoneErr { filename: String }, - Message(String), - /// Like Message but triggers a display refresh - MessageNow(String) + Message(String) } #[derive(Clone, Debug)] @@ -53,13 +51,14 @@ impl DlReporter { } } - pub fn send(& self, status: DlStatus) -> ResBE<()> { + pub fn send(& self, status: DlStatus) { + // This should not fail, so unwrap it here instead propagating the error self.transmitter.send( DlReport { id: self.id, status: status } - ).map_err(|e| e.into()) + ).unwrap(); } } @@ -215,9 +214,6 @@ pub async fn watch_and_print_reports(mut receiver: mpsc::UnboundedReceiver { msg_queue.push_back(msg); - }, - DlStatus::MessageNow(msg) => { - msg_queue.push_back(msg); moved_lines = print_accumulated_report(&statuses, &mut msg_queue, moved_lines)?; t_last = SystemTime::now(); } diff --git a/src/download.rs b/src/download.rs index 1661806..a67e5f5 100644 --- a/src/download.rs +++ b/src/download.rs @@ -120,7 +120,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte bytes_total: content_length, filename: filename.to_string() } - )?; + ); let mut curr_progress = 0; @@ -184,7 +184,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte speed_mbps: speed_mbps, bytes_curr: curr_progress } - )?; + ); } if buff.len() > 0 { @@ -201,7 +201,7 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte DlStatus::Done { duration_ms: duration_ms } - )?; + ); Ok(()) } @@ -255,7 +255,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter rep.send(DlStatus::Init { bytes_total: content_length, filename: into_file.to_string() - })?; + }); let rep_task = rep.clone(); @@ -292,7 +292,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter rep.send(DlStatus::Update { speed_mbps: speed, bytes_curr: curr - }).unwrap(); + }); } else { update_counter += 1; @@ -308,7 +308,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter }, // Just forwared everything else to the calling receiver - _ => rep.send(update.status).unwrap() + _ => rep.send(update.status) } } @@ -348,7 +348,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter rep.send(DlStatus::Done { duration_ms: t_start.elapsed()?.as_millis() as u64 - })?; + }); Ok(()) } diff --git a/src/main.rs b/src/main.rs index 099ec2a..3d926d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,8 +88,6 @@ async fn main() -> ResBE<()> { .get_matches(); - - let outdir = match arguments.value_of("outdir") { Some(it) => it, None => "./" @@ -133,9 +131,11 @@ async fn main() -> ResBE<()> { let is_zippy = arguments.is_present("zippyshare"); - if arguments.is_present("listfile") { - let s_listfile = arguments.value_of("listfile").unwrap(); + // Evaluate and execute the requested action. The 3 different actions are + // mutally exclusive, so only one of them will be executed + + if let Some(s_listfile) = arguments.value_of("listfile") { let listfile = Path::new(s_listfile); @@ -154,29 +154,28 @@ async fn main() -> ResBE<()> { download_multiple(urls, outdir, numparal, boost, is_zippy).await?; - } else if arguments.is_present("download") { + } + + if let Some(url) = arguments.value_of("download") { - let url = arguments.value_of("download").unwrap(); - - let numparal = if boost != 1 { + let boost = if boost != 1 { boost } else { numparal }; - download_multiple(vec![url.to_string()], outdir, 1, numparal, is_zippy).await?; + download_multiple(vec![url.to_string()], outdir, 1, boost, is_zippy).await?; - } else if arguments.is_present("resolve") { - - let url = arguments.value_of("resolve").unwrap(); + } + + if let Some(url) = arguments.value_of("resolve") { match zippy::resolve_link(&url).await { Ok(resolved_url) => { println!("{}", resolved_url); }, - Err(e) => { + Err(_e) => { println!("Zippyshare link could not be resolved"); - eprintln!("{}", e); exit(1); } @@ -233,8 +232,8 @@ async fn download_multiple(urls: Vec, outdir: &str, numparal: i32, boost Ok(url) => url, Err(_e) => { rep.send( - DlStatus::MessageNow(format!("Zippyshare link could not be resolved: {}", url)) - ).unwrap(); + DlStatus::Message(format!("Zippyshare link could not be resolved: {}", url)) + ); continue; } @@ -250,14 +249,23 @@ async fn download_multiple(urls: Vec, outdir: &str, numparal: i32, boost // If file with same name is present locally, check filesize if path_into_file.exists() { - let (filesize, _) = download::http_get_filesize_and_range_support(&url).await.unwrap(); + let filesize = match download::http_get_filesize_and_range_support(&url).await { + Ok((filesize, _)) => filesize, + Err(_e) => { + rep.send( + DlStatus::Message(format!("Error while querying metadata: {}", url)) + ); + continue; + } + }; + let local_filesize = std::fs::metadata(path_into_file).unwrap().len(); if filesize == local_filesize { - rep.send(DlStatus::MessageNow(format!("Skipping file '{}': already present", &file_name))).unwrap(); + rep.send(DlStatus::Message(format!("Skipping file '{}': already present", &file_name))); continue; } else { - rep.send(DlStatus::MessageNow(format!("Replacing file '{}': present but not completed", &file_name))).unwrap(); + rep.send(DlStatus::Message(format!("Replacing file '{}': present but not completed", &file_name))); } } @@ -265,13 +273,13 @@ async fn download_multiple(urls: Vec, outdir: &str, numparal: i32, boost if let Err(_e) = download::download_feedback(&url, &into_file, rep.clone()).await { rep.send(DlStatus::DoneErr { filename: into_file.to_string() - }).unwrap(); + }); } } else { if let Err(_e) = download::download_feedback_multi(&url, &into_file, rep.clone(), boost).await { rep.send(DlStatus::DoneErr { filename: into_file.to_string() - }).unwrap(); + }); } };