From c8af1009a240b1e47b658be051e00fa6f3e6f171 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 30 Jan 2018 15:54:44 +1100 Subject: [PATCH] Clean up archive downloading --- src/http.rs | 16 +++++++++------- src/installer.rs | 18 +++++++++--------- src/main.rs | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/http.rs b/src/http.rs index 0d06660..110558a 100644 --- a/src/http.rs +++ b/src/http.rs @@ -9,19 +9,21 @@ use reqwest; use std::io::Read; /// Streams a file from a HTTP server. -pub fn stream_file(url : String, callback : F) -> Result<(), String> - // |data : Vec, total : u64| - where F: Fn(Vec, u64) -> () { +pub fn stream_file(url: String, callback: F) -> Result<(), String> +// |data : Vec, total : u64| +where + F: Fn(Vec, u64) -> (), +{ let mut client = match reqwest::get(&url) { Ok(v) => v, Err(v) => return Err(format!("Failed to GET resource: {:?}", v)), }; let size = { - let size : Option<&ContentLength> = client.headers().get(); + let size: Option<&ContentLength> = client.headers().get(); match size { Some(&ContentLength(v)) => v, - None => 0 + None => 0, } }; @@ -30,14 +32,14 @@ pub fn stream_file(url : String, callback : F) -> Result<(), String> let len = client.read(&mut buf); let len = match len { Ok(v) => v, - Err(v) => return Err(format!("Failed to read resource: {:?}", v)) + Err(v) => return Err(format!("Failed to read resource: {:?}", v)), }; if len == 0 { break; } - let buf_copy = &buf[0 .. len]; + let buf_copy = &buf[0..len]; let buf_copy = buf_copy.to_vec(); callback(buf_copy, size); diff --git a/src/installer.rs b/src/installer.rs index 2c0a1c0..f00fb98 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -18,7 +18,7 @@ use config::Config; use http::stream_file; -use number_prefix::{decimal_prefix, Standalone, Prefixed}; +use number_prefix::{decimal_prefix, Prefixed, Standalone}; /// A message thrown during the installation of packages. #[derive(Serialize)] @@ -35,7 +35,7 @@ pub struct InstallerFramework { } struct DownloadProgress { - downloaded: usize + downloaded: usize, } impl InstallerFramework { @@ -135,9 +135,7 @@ impl InstallerFramework { println!("{:?}", latest_file); // Download this file - let lock = Arc::new(Mutex::new(DownloadProgress { - downloaded: 0 - })); + let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 })); stream_file(latest_file.url, |data, size| { let mut reference = lock.lock().unwrap(); @@ -157,18 +155,20 @@ impl InstallerFramework { // Pretty print data volumes let pretty_current = match decimal_prefix(reference.downloaded as f64) { - Standalone(bytes) => format!("{} bytes", bytes), + Standalone(bytes) => format!("{} bytes", bytes), Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix), }; let pretty_total = match decimal_prefix(size as f64) { - Standalone(bytes) => format!("{} bytes", bytes), + Standalone(bytes) => format!("{} bytes", bytes), Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix), }; messages .send(InstallMessage::Status( - format!("Downloading {} ({} of {})", package.name, pretty_current, - pretty_total), + format!( + "Downloading {} ({} of {})", + package.name, pretty_current, pretty_total + ), global_percentage, )) .unwrap(); diff --git a/src/main.rs b/src/main.rs index 8f780f4..1a02520 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,8 @@ extern crate hyper; extern crate hyper_tls; extern crate tokio_core; -extern crate reqwest; extern crate number_prefix; +extern crate reqwest; extern crate includedir; extern crate phf;