From 041760f9e9fcd4345d3623f2ef6335afe2bf7ae9 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Thu, 15 Jul 2021 19:13:31 -0400 Subject: [PATCH] clippy --- src/cache/disk.rs | 61 +++++++++++++++++++++++------------------------ src/config.rs | 4 ++-- src/metrics.rs | 24 +++++++++---------- 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/cache/disk.rs b/src/cache/disk.rs index 78428ab..cf779e7 100644 --- a/src/cache/disk.rs +++ b/src/cache/disk.rs @@ -196,39 +196,38 @@ async fn db_listener( /// Returns if a file was successfully deleted. async fn remove_file_handler(key: String) -> bool { - if let Err(e) = remove_file(&key).await { - match e.kind() { - std::io::ErrorKind::NotFound => { - if let Ok(bytes) = hex::decode(&key) { - if bytes.len() == 16 { - let hash = Md5Hash(*GenericArray::from_slice(&bytes)); - let path: PathBuf = hash.into(); - if let Err(e) = remove_file(&path).await { - warn!( - "Failed to delete file `{}` from cache: {}", - path.to_string_lossy(), - e - ); - false - } else { - true - } - } else { - warn!("Failed to delete file `{}`; invalid hash size.", &key); - false - } - } else { - warn!("Failed to delete file `{}`; not a md5hash.", &key); - false - } - } - _ => { - warn!("Failed to delete file `{}` from cache: {}", &key, e); - false - } + let error = if let Err(e) = remove_file(&key).await { + e + } else { + return true; + }; + + if error.kind() != std::io::ErrorKind::NotFound { + warn!("Failed to delete file `{}` from cache: {}", &key, error); + return false; + } + + if let Ok(bytes) = hex::decode(&key) { + if bytes.len() != 16 { + warn!("Failed to delete file `{}`; invalid hash size.", &key); + return false; + } + + let hash = Md5Hash(*GenericArray::from_slice(&bytes)); + let path: PathBuf = hash.into(); + if let Err(e) = remove_file(&path).await { + warn!( + "Failed to delete file `{}` from cache: {}", + path.to_string_lossy(), + e + ); + false + } else { + true } } else { - true + warn!("Failed to delete file `{}`; not a md5hash.", &key); + false } } diff --git a/src/config.rs b/src/config.rs index ac29b83..5018777 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,11 +73,11 @@ pub fn load_config() -> Result { Ordering::Release, ); - config.proxy.clone().map(|socket| { + if let Some(socket) = config.proxy.clone() { USE_PROXY .set(socket) .expect("USE_PROXY to be set only by this function"); - }); + } DISABLE_CERT_VALIDATION.store( config diff --git a/src/metrics.rs b/src/metrics.rs index 1de6937..538c64d 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -104,20 +104,20 @@ pub async fn load_geo_ip_data(license_key: ClientSecret) -> Result<(), DbLoadErr // Check date of db let db_date_created = metadata(DB_PATH) .ok() - .and_then(|metadata| match metadata.created() { - Ok(time) => Some(time), - Err(_) => { + .and_then(|metadata| { + if let Ok(time) = metadata.created() { + Some(time) + } else { debug("fs didn't report birth time, fall back to last modified instead"); metadata.modified().ok() } }) .unwrap_or(SystemTime::UNIX_EPOCH); - let duration = match SystemTime::now().duration_since(db_date_created) { - Ok(time) => Duration::from_std(time).expect("duration to fit"), - Err(_) => { - warn!("Clock may have gone backwards?"); - Duration::max_value() - } + let duration = if let Ok(time) = SystemTime::now().duration_since(db_date_created) { + Duration::from_std(time).expect("duration to fit") + } else { + warn!("Clock may have gone backwards?"); + Duration::max_value() }; // DB expired, fetch a new one @@ -172,14 +172,12 @@ async fn fetch_db(license_key: ClientSecret) -> Result<(), DbLoadError> { } pub fn record_country_visit(country: Option) { - let iso_code = if let Some(country) = country { + let iso_code = country.map_or("unknown", |country| { country .country .and_then(|c| c.iso_code) .unwrap_or("unknown") - } else { - "unknown" - }; + }); COUNTRY_VISIT_COUNTER .get_metric_with_label_values(&[iso_code])