From b92cad0938329b024db87d095065caff7bdf5d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Tue, 19 Jan 2021 12:09:33 +0100 Subject: [PATCH] clippy: convert single match to if let MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabian Grünbichler --- src/api2/node/apt.rs | 11 +++--- src/api2/status.rs | 75 +++++++++++++++++++--------------------- src/server/gc_job.rs | 7 ++-- src/server/jobstate.rs | 7 ++-- src/server/verify_job.rs | 7 ++-- src/tools/disks.rs | 29 +++++++--------- src/tools/fuse_loop.rs | 18 ++++------ 7 files changed, 66 insertions(+), 88 deletions(-) diff --git a/src/api2/node/apt.rs b/src/api2/node/apt.rs index 7fdc8ade..52eeb976 100644 --- a/src/api2/node/apt.rs +++ b/src/api2/node/apt.rs @@ -35,13 +35,10 @@ use crate::api2::types::{Authid, APTUpdateInfo, NODE_SCHEMA, UPID_SCHEMA}; /// List available APT updates fn apt_update_available(_param: Value) -> Result { - match apt::pkg_cache_expired() { - Ok(false) => { - if let Ok(Some(cache)) = apt::read_pkg_state() { - return Ok(json!(cache.package_status)); - } - }, - _ => (), + if let Ok(false) = apt::pkg_cache_expired() { + if let Ok(Some(cache)) = apt::read_pkg_state() { + return Ok(json!(cache.package_status)); + } } let cache = apt::update_cache()?; diff --git a/src/api2/status.rs b/src/api2/status.rs index 743b1355..dc354d70 100644 --- a/src/api2/status.rs +++ b/src/api2/status.rs @@ -127,49 +127,46 @@ fn datastore_status( rrd_mode, ); - match (total_res, used_res) { - (Some((start, reso, total_list)), Some((_, _, used_list))) => { - let mut usage_list: Vec = Vec::new(); - let mut time_list: Vec = Vec::new(); - let mut history = Vec::new(); + if let (Some((start, reso, total_list)), Some((_, _, used_list))) = (total_res, used_res) { + let mut usage_list: Vec = Vec::new(); + let mut time_list: Vec = Vec::new(); + let mut history = Vec::new(); - for (idx, used) in used_list.iter().enumerate() { - let total = if idx < total_list.len() { - total_list[idx] + for (idx, used) in used_list.iter().enumerate() { + let total = if idx < total_list.len() { + total_list[idx] + } else { + None + }; + + match (total, used) { + (Some(total), Some(used)) if total != 0.0 => { + time_list.push(start + (idx as u64)*reso); + let usage = used/total; + usage_list.push(usage); + history.push(json!(usage)); + }, + _ => { + history.push(json!(null)) + } + } + } + + entry["history-start"] = start.into(); + entry["history-delta"] = reso.into(); + entry["history"] = history.into(); + + // we skip the calculation for datastores with not enough data + if usage_list.len() >= 7 { + if let Some((a,b)) = linear_regression(&time_list, &usage_list) { + if b != 0.0 { + let estimate = (1.0 - a) / b; + entry["estimated-full-date"] = Value::from(estimate.floor() as u64); } else { - None - }; - - match (total, used) { - (Some(total), Some(used)) if total != 0.0 => { - time_list.push(start + (idx as u64)*reso); - let usage = used/total; - usage_list.push(usage); - history.push(json!(usage)); - }, - _ => { - history.push(json!(null)) - } + entry["estimated-full-date"] = Value::from(0); } } - - entry["history-start"] = start.into(); - entry["history-delta"] = reso.into(); - entry["history"] = history.into(); - - // we skip the calculation for datastores with not enough data - if usage_list.len() >= 7 { - if let Some((a,b)) = linear_regression(&time_list, &usage_list) { - if b != 0.0 { - let estimate = (1.0 - a) / b; - entry["estimated-full-date"] = Value::from(estimate.floor() as u64); - } else { - entry["estimated-full-date"] = Value::from(0); - } - } - } - }, - _ => {}, + } } list.push(entry); diff --git a/src/server/gc_job.rs b/src/server/gc_job.rs index 7a7e6de2..6f3d9538 100644 --- a/src/server/gc_job.rs +++ b/src/server/gc_job.rs @@ -39,13 +39,12 @@ pub fn do_garbage_collection_job( let status = worker.create_state(&result); - match job.finish(status) { - Err(err) => eprintln!( + if let Err(err) = job.finish(status) { + eprintln!( "could not finish job state for {}: {}", job.jobtype().to_string(), err - ), - Ok(_) => (), + ); } if let Some(email) = email { diff --git a/src/server/jobstate.rs b/src/server/jobstate.rs index e55a193e..ea934e00 100644 --- a/src/server/jobstate.rs +++ b/src/server/jobstate.rs @@ -207,11 +207,8 @@ impl Job { /// Start the job and update the statefile accordingly /// Fails if the job was already started pub fn start(&mut self, upid: &str) -> Result<(), Error> { - match self.state { - JobState::Started { .. } => { - bail!("cannot start job that is started!"); - } - _ => {} + if let JobState::Started { .. } = self.state { + bail!("cannot start job that is started!"); } self.state = JobState::Started { diff --git a/src/server/verify_job.rs b/src/server/verify_job.rs index 6791196b..ca6eb554 100644 --- a/src/server/verify_job.rs +++ b/src/server/verify_job.rs @@ -83,13 +83,12 @@ pub fn do_verification_job( let status = worker.create_state(&job_result); - match job.finish(status) { - Err(err) => eprintln!( + if let Err(err) = job.finish(status) { + eprintln!( "could not finish job state for {}: {}", job.jobtype().to_string(), err - ), - Ok(_) => (), + ); } if let Some(email) = email { diff --git a/src/tools/disks.rs b/src/tools/disks.rs index bfea5a8b..8573695d 100644 --- a/src/tools/disks.rs +++ b/src/tools/disks.rs @@ -566,23 +566,18 @@ pub fn get_partition_type_info() -> Result>, Error> let mut res: HashMap> = HashMap::new(); let output: serde_json::Value = output.parse()?; - match output["blockdevices"].as_array() { - Some(list) => { - for info in list { - let path = match info["path"].as_str() { - Some(p) => p, - None => continue, - }; - let partition_type = match info["parttype"].as_str() { - Some(t) => t.to_owned(), - None => continue, - }; - let devices = res.entry(partition_type).or_insert(Vec::new()); - devices.push(path.to_string()); - } - } - None => { - + if let Some(list) = output["blockdevices"].as_array() { + for info in list { + let path = match info["path"].as_str() { + Some(p) => p, + None => continue, + }; + let partition_type = match info["parttype"].as_str() { + Some(t) => t.to_owned(), + None => continue, + }; + let devices = res.entry(partition_type).or_insert(Vec::new()); + devices.push(path.to_string()); } } Ok(res) diff --git a/src/tools/fuse_loop.rs b/src/tools/fuse_loop.rs index ebc30398..b93381a5 100644 --- a/src/tools/fuse_loop.rs +++ b/src/tools/fuse_loop.rs @@ -357,18 +357,12 @@ pub fn find_all_mappings() -> Result { - let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy()); - match get_backing_file(&loopdev) { - Ok(file) => { - // insert filename only, strip RUN_DIR/ - loopmap.insert(file[RUN_DIR.len()+1..].to_owned(), loopdev); - }, - Err(_) => {}, - } - }, - Err(_) => {}, + if let Ok(ent) = ent { + let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy()); + if let Ok(file) = get_backing_file(&loopdev) { + // insert filename only, strip RUN_DIR/ + loopmap.insert(file[RUN_DIR.len()+1..].to_owned(), loopdev); + } } }