From 68ec4a82a90ef16a3fe0a770e9fdb7679da861b1 Mon Sep 17 00:00:00 2001 From: HuijingHei Date: Fri, 20 Sep 2024 16:35:10 +0800 Subject: [PATCH] Convert to `let-else` syntax Convert to [let else](https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html) where possible. Inspired by https://github.com/coreos/bootupd/pull/723#discussion_r1757480310 --- src/bios.rs | 4 +--- src/bootupd.rs | 10 +++------- src/efi.rs | 4 +--- src/filetree.rs | 8 ++------ src/util.rs | 4 +--- 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/bios.rs b/src/bios.rs index 481fff16..fbe12856 100644 --- a/src/bios.rs +++ b/src/bios.rs @@ -95,9 +95,7 @@ impl Component for Bios { device: &str, _update_firmware: bool, ) -> Result { - let meta = if let Some(meta) = get_component_update(src_root, self)? { - meta - } else { + let Some(meta) = get_component_update(src_root, self)? else { anyhow::bail!("No update metadata for component {} found", self.name()); }; diff --git a/src/bootupd.rs b/src/bootupd.rs index 4f029e83..dd441096 100644 --- a/src/bootupd.rs +++ b/src/bootupd.rs @@ -249,9 +249,7 @@ pub(crate) fn adopt_and_update(name: &str) -> Result { ensure_writable_boot()?; - let update = if let Some(update) = component.query_update(&sysroot)? { - update - } else { + let Some(update) = component.query_update(&sysroot)? else { anyhow::bail!("Component {} has no available update", name); }; let mut state_guard = @@ -270,12 +268,10 @@ pub(crate) fn adopt_and_update(name: &str) -> Result { pub(crate) fn validate(name: &str) -> Result { let state = SavedState::load_from_disk("/")?.unwrap_or_default(); let component = component::new_from_name(name)?; - let inst = if let Some(inst) = state.installed.get(name) { - inst.clone() - } else { + let Some(inst) = state.installed.get(name) else { anyhow::bail!("Component {} is not installed", name); }; - component.validate(&inst) + component.validate(inst) } pub(crate) fn status() -> Result { diff --git a/src/efi.rs b/src/efi.rs index 75c34f4c..2ab46c24 100644 --- a/src/efi.rs +++ b/src/efi.rs @@ -283,9 +283,7 @@ impl Component for Efi { device: &str, update_firmware: bool, ) -> Result { - let meta = if let Some(meta) = get_component_update(src_root, self)? { - meta - } else { + let Some(meta) = get_component_update(src_root, self)? else { anyhow::bail!("No update metadata for component {} found", self.name()); }; log::debug!("Found metadata {}", meta.version); diff --git a/src/filetree.rs b/src/filetree.rs index 8234dcba..cf10f8c3 100644 --- a/src/filetree.rs +++ b/src/filetree.rs @@ -104,9 +104,7 @@ impl FileTree { let mut ret = HashMap::new(); for entry in dir.list_dir(".")? { let entry = entry?; - let name = if let Some(name) = entry.file_name().to_str() { - name - } else { + let Some(name) = entry.file_name().to_str() else { bail!("Invalid UTF-8 filename: {:?}", entry.file_name()) }; if name.starts_with(TMP_PREFIX) { @@ -239,9 +237,7 @@ impl FileTree { fn cleanup_tmp(dir: &openat::Dir) -> Result<()> { for entry in dir.list_dir(".")? { let entry = entry?; - let name = if let Some(name) = entry.file_name().to_str() { - name - } else { + let Some(name) = entry.file_name().to_str() else { // Skip invalid UTF-8 for now, we will barf on it later though. continue; }; diff --git a/src/util.rs b/src/util.rs index d52a745e..0635ef41 100644 --- a/src/util.rs +++ b/src/util.rs @@ -37,9 +37,7 @@ pub(crate) fn filenames(dir: &openat::Dir) -> Result> { let mut ret = HashSet::new(); for entry in dir.list_dir(".")? { let entry = entry?; - let name = if let Some(name) = entry.file_name().to_str() { - name - } else { + let Some(name) = entry.file_name().to_str() else { bail!("Invalid UTF-8 filename: {:?}", entry.file_name()) }; match dir.get_file_type(&entry)? {