clippy: convert single match to if let

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2021-01-19 12:09:33 +01:00
parent ea368a06cd
commit b92cad0938
7 changed files with 66 additions and 88 deletions

View File

@ -35,13 +35,10 @@ use crate::api2::types::{Authid, APTUpdateInfo, NODE_SCHEMA, UPID_SCHEMA};
/// List available APT updates /// List available APT updates
fn apt_update_available(_param: Value) -> Result<Value, Error> { fn apt_update_available(_param: Value) -> Result<Value, Error> {
match apt::pkg_cache_expired() { if let Ok(false) = apt::pkg_cache_expired() {
Ok(false) => { if let Ok(Some(cache)) = apt::read_pkg_state() {
if let Ok(Some(cache)) = apt::read_pkg_state() { return Ok(json!(cache.package_status));
return Ok(json!(cache.package_status)); }
}
},
_ => (),
} }
let cache = apt::update_cache()?; let cache = apt::update_cache()?;

View File

@ -127,49 +127,46 @@ fn datastore_status(
rrd_mode, rrd_mode,
); );
match (total_res, used_res) { if let (Some((start, reso, total_list)), Some((_, _, used_list))) = (total_res, used_res) {
(Some((start, reso, total_list)), Some((_, _, used_list))) => { let mut usage_list: Vec<f64> = Vec::new();
let mut usage_list: Vec<f64> = Vec::new(); let mut time_list: Vec<u64> = Vec::new();
let mut time_list: Vec<u64> = Vec::new(); let mut history = Vec::new();
let mut history = Vec::new();
for (idx, used) in used_list.iter().enumerate() { for (idx, used) in used_list.iter().enumerate() {
let total = if idx < total_list.len() { let total = if idx < total_list.len() {
total_list[idx] 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 { } else {
None entry["estimated-full-date"] = Value::from(0);
};
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 {
entry["estimated-full-date"] = Value::from(0);
}
}
}
},
_ => {},
} }
list.push(entry); list.push(entry);

View File

@ -39,13 +39,12 @@ pub fn do_garbage_collection_job(
let status = worker.create_state(&result); let status = worker.create_state(&result);
match job.finish(status) { if let Err(err) = job.finish(status) {
Err(err) => eprintln!( eprintln!(
"could not finish job state for {}: {}", "could not finish job state for {}: {}",
job.jobtype().to_string(), job.jobtype().to_string(),
err err
), );
Ok(_) => (),
} }
if let Some(email) = email { if let Some(email) = email {

View File

@ -207,11 +207,8 @@ impl Job {
/// Start the job and update the statefile accordingly /// Start the job and update the statefile accordingly
/// Fails if the job was already started /// Fails if the job was already started
pub fn start(&mut self, upid: &str) -> Result<(), Error> { pub fn start(&mut self, upid: &str) -> Result<(), Error> {
match self.state { if let JobState::Started { .. } = self.state {
JobState::Started { .. } => { bail!("cannot start job that is started!");
bail!("cannot start job that is started!");
}
_ => {}
} }
self.state = JobState::Started { self.state = JobState::Started {

View File

@ -83,13 +83,12 @@ pub fn do_verification_job(
let status = worker.create_state(&job_result); let status = worker.create_state(&job_result);
match job.finish(status) { if let Err(err) = job.finish(status) {
Err(err) => eprintln!( eprintln!(
"could not finish job state for {}: {}", "could not finish job state for {}: {}",
job.jobtype().to_string(), job.jobtype().to_string(),
err err
), );
Ok(_) => (),
} }
if let Some(email) = email { if let Some(email) = email {

View File

@ -566,23 +566,18 @@ pub fn get_partition_type_info() -> Result<HashMap<String, Vec<String>>, Error>
let mut res: HashMap<String, Vec<String>> = HashMap::new(); let mut res: HashMap<String, Vec<String>> = HashMap::new();
let output: serde_json::Value = output.parse()?; let output: serde_json::Value = output.parse()?;
match output["blockdevices"].as_array() { if let Some(list) = output["blockdevices"].as_array() {
Some(list) => { for info in list {
for info in list { let path = match info["path"].as_str() {
let path = match info["path"].as_str() { Some(p) => p,
Some(p) => p, None => continue,
None => continue, };
}; let partition_type = match info["parttype"].as_str() {
let partition_type = match info["parttype"].as_str() { Some(t) => t.to_owned(),
Some(t) => t.to_owned(), None => continue,
None => continue, };
}; let devices = res.entry(partition_type).or_insert(Vec::new());
let devices = res.entry(partition_type).or_insert(Vec::new()); devices.push(path.to_string());
devices.push(path.to_string());
}
}
None => {
} }
} }
Ok(res) Ok(res)

View File

@ -357,18 +357,12 @@ pub fn find_all_mappings() -> Result<impl Iterator<Item = (String, Option<String
// get map of all /dev/loop mappings belonging to us // get map of all /dev/loop mappings belonging to us
let mut loopmap = HashMap::new(); let mut loopmap = HashMap::new();
for ent in fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)? { for ent in fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)? {
match ent { if let Ok(ent) = ent {
Ok(ent) => { let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy());
let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy()); if let Ok(file) = get_backing_file(&loopdev) {
match get_backing_file(&loopdev) { // insert filename only, strip RUN_DIR/
Ok(file) => { loopmap.insert(file[RUN_DIR.len()+1..].to_owned(), loopdev);
// insert filename only, strip RUN_DIR/ }
loopmap.insert(file[RUN_DIR.len()+1..].to_owned(), loopdev);
},
Err(_) => {},
}
},
Err(_) => {},
} }
} }