From 88f23ae568e4d4e8e2d69a4ece01d9913b69b564 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Thu, 31 Mar 2022 17:13:57 +0200 Subject: [PATCH] More refactoring --- src/dlreport.rs | 9 +++------ src/download.rs | 16 ++++++++-------- src/main.rs | 8 +------- src/zippy.rs | 10 +++++----- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/dlreport.rs b/src/dlreport.rs index 2578d44..0f97e9d 100644 --- a/src/dlreport.rs +++ b/src/dlreport.rs @@ -35,10 +35,7 @@ pub struct DlReporter { impl DlReporter { pub fn new(id: u32, transmitter: mpsc::UnboundedSender) -> DlReporter { - DlReporter { - id: id, - transmitter: transmitter, - } + DlReporter { id, transmitter } } pub fn send(&self, status: DlStatus) { @@ -98,12 +95,12 @@ fn print_accumulated_report( execute!( stdout(), - Print(format!("----------------------------------------")), + Print("----------------------------------------".to_string()), Clear(ClearType::UntilNewLine), Print("\n") )?; - for (_k, v) in statuses { + for v in statuses.values() { let percent_complete = v.progress as f64 / v.total_size as f64 * 100.0; execute!( diff --git a/src/download.rs b/src/download.rs index 5b9c9bc..ca79e32 100644 --- a/src/download.rs +++ b/src/download.rs @@ -25,7 +25,7 @@ impl RollingAverage { } fn value(&self) -> f64 { - if self.data.len() == 0 { + if self.data.is_empty() { 0.0 } else { let mut max = self.data[0]; @@ -64,7 +64,7 @@ impl RollingAverage { /// Get the filename at the end of the given URL. This will decode the URL Encoding. pub fn url_to_filename(url: &str) -> String { - let url_dec = percent_decode_str(&url) + let url_dec = percent_decode_str(url) .decode_utf8_lossy() .to_owned() .to_string(); @@ -74,7 +74,7 @@ pub fn url_to_filename(url: &str) -> String { .to_str() .unwrap(); // Split at ? and return the first part. If no ? is present, this just returns the full string - file_name.split("?").next().unwrap().to_string() + file_name.split('?').next().unwrap().to_string() } pub async fn download_feedback( @@ -123,7 +123,7 @@ pub async fn download_feedback_chunks( let mut ofile = opts .create(true) .write(true) - .truncate(!from_to.is_some()) + .truncate(from_to.is_none()) .open(into_file) .await?; @@ -196,7 +196,7 @@ pub async fn download_feedback_chunks( }); } - if buff.len() > 0 { + if !buff.is_empty() { ofile.write_all(&buff).await?; } @@ -243,8 +243,8 @@ pub async fn download_feedback_multi( let t_start = SystemTime::now(); for index in 0..conn_count { - let url = url.clone().to_owned(); - let into_file = into_file.clone().to_owned(); + let url = url.to_owned(); + let into_file = into_file.to_owned(); let tx = tx.clone(); @@ -284,7 +284,7 @@ pub async fn download_feedback_multi( let rep_task = rep.clone(); - let mut t_last = t_start.clone(); + let mut t_last = t_start; let manager_handle = tokio::task::spawn(async move { let rep = rep_task; diff --git a/src/main.rs b/src/main.rs index 412dd8f..0d9ded6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,13 +115,7 @@ async fn download_multiple(args: CLIArgs, raw_urls: Vec) -> Result<()> { } async fn download_job(urls: SyncQueue, reporter: UnboundedSender, cli_args: CLIArgs) { - loop { - // Get the next url to download or break if there are no more urls - let dlreq = match urls.lock().await.pop_front() { - Some(it) => it, - None => break, - }; - + while let Some(dlreq) = urls.lock().await.pop_front() { let reporter = DlReporter::new(dlreq.id as u32, reporter.clone()); // Resolve the zippy url to the direct download url if necessary diff --git a/src/zippy.rs b/src/zippy.rs index e58fa56..7f726bc 100644 --- a/src/zippy.rs +++ b/src/zippy.rs @@ -25,12 +25,12 @@ document.getElementById('dlbutton').href = "/d/0Ky7p1C6/" + (186549 % 51245 + 18 pub async fn resolve_link(url: &str) -> Result { // Regex to check if the provided url is a zippyshare download url let re = Regex::new(r"(https://www\d*\.zippyshare\.com)")?; - if !re.is_match(&url) { + if !re.is_match(url) { return Err(Error::new(ErrorKind::Other, "URL is not a zippyshare url").into()); } // Extract the hostname (with https:// prefix) for later - let base_host = &re.captures(&url).unwrap()[0]; + let base_host = &re.captures(url).unwrap()[0]; // Download the html body for the download page let body = reqwest::get(url).await?.text().await?; @@ -47,10 +47,10 @@ pub async fn resolve_link(url: &str) -> Result { let url_start = &cap_link[1]; let url_end = &cap_link[5]; - let n2: i32 = i32::from_str_radix(&cap_link[2], 10)?; - let n3: i32 = i32::from_str_radix(&cap_link[3], 10)?; + let n2: i32 = cap_link[2].parse()?; + let n3: i32 = cap_link[3].parse()?; let n4 = n2; - let n5: i32 = i32::from_str_radix(&cap_link[4], 10)?; + let n5: i32 = cap_link[4].parse()?; let mixed = n2 % n3 + n4 % n5;