From 80619597d34a09956a34a3398503aa7c30800d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Tue, 15 Aug 2023 07:27:46 +0800 Subject: [PATCH] refactor: simplify the flow with `let-else` (#56) --- app/src/manager/preview.rs | 8 +++----- config/src/keymap/key.rs | 2 +- config/src/open/opener.rs | 8 +++----- config/src/preset.rs | 4 +--- core/src/manager/manager.rs | 14 ++++---------- core/src/manager/tab.rs | 12 +++--------- core/src/manager/watcher.rs | 14 +++++--------- core/src/tasks/tasks.rs | 6 ++---- core/src/tasks/workers/file.rs | 4 +--- shared/src/fns.rs | 13 ++++--------- shared/src/fs.rs | 12 +++--------- 11 files changed, 30 insertions(+), 67 deletions(-) diff --git a/app/src/manager/preview.rs b/app/src/manager/preview.rs index dc48ebe5..475223a4 100644 --- a/app/src/manager/preview.rs +++ b/app/src/manager/preview.rs @@ -27,21 +27,19 @@ impl<'a> Widget for Preview<'a> { ); let manager = &self.cx.manager; - let hovered = if let Some(h) = manager.hovered() { - h.path() - } else { + let Some(hovered) = manager.hovered().map(|h| &h.path) else { return; }; let preview = manager.active().preview(); - if !preview.same_path(&hovered) { + if !preview.same_path(hovered) { return; } match &preview.data { PreviewData::None => {} PreviewData::Folder => { - if let Some(folder) = manager.active().history(&hovered) { + if let Some(folder) = manager.active().history(hovered) { Folder::new(self.cx, folder).with_preview(true).render(area, buf); } } diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 94e96fa7..1c5c8ec8 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -27,7 +27,7 @@ impl Default for Key { impl From for Key { fn from(value: KeyEvent) -> Self { - let shift = if let KeyCode::Char(c) = value.code { c.is_ascii_uppercase() } else { false }; + let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase()); Self { code: value.code, diff --git a/config/src/open/opener.rs b/config/src/open/opener.rs index fbc4eb5a..37256ba0 100644 --- a/config/src/open/opener.rs +++ b/config/src/open/opener.rs @@ -66,11 +66,9 @@ impl<'de> Deserialize<'de> for Opener { if exec.is_empty() { return Err(serde::de::Error::custom("`exec` cannot be empty")); } - let display_name = if let Some(s) = shadow.display_name { - s - } else { - exec.split_whitespace().next().unwrap().to_string() - }; + + let display_name = + shadow.display_name.unwrap_or_else(|| exec.split_whitespace().next().unwrap().to_string()); let spread = exec.contains("$*") || exec.contains("$@"); Ok(Self { exec, block: shadow.block, display_name, spread }) diff --git a/config/src/preset.rs b/config/src/preset.rs index 164ed31a..88536a22 100644 --- a/config/src/preset.rs +++ b/config/src/preset.rs @@ -8,9 +8,7 @@ pub(crate) struct Preset; impl Preset { fn merge(a: &mut Table, b: &Table, max: u8) { for (k, v) in b { - let a = if let Some(a) = a.get_mut(k) { - a - } else { + let Some(a) = a.get_mut(k) else { a.insert(k.clone(), v.clone()); continue; }; diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index b013ee11..e5e0d1e9 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -52,9 +52,7 @@ impl Manager { } pub fn preview(&mut self, show_image: bool) -> bool { - let hovered = if let Some(h) = self.hovered() { - h.clone() - } else { + let Some(hovered) = self.hovered().cloned() else { return self.active_mut().preview.reset(); }; @@ -203,9 +201,7 @@ impl Manager { return self.bulk_rename(); } - let hovered = if let Some(h) = self.hovered() { - h.path.clone() - } else { + let Some(hovered) = self.hovered().map(|h| h.path()) else { return false; }; @@ -412,13 +408,11 @@ impl Manager { } pub fn update_preview(&mut self, path: PathBuf, mime: String, data: PreviewData) -> bool { - let hovered = if let Some(ref h) = self.current().hovered { - h.path() - } else { + let Some(hovered) = self.hovered().map(|h| &h.path) else { return self.active_mut().preview.reset(); }; - if hovered != path { + if path != *hovered { return false; } diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index 78706e25..c45c337e 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -75,9 +75,7 @@ impl Tab { } pub async fn cd(&mut self, mut target: PathBuf) -> bool { - let file = if let Ok(f) = File::from(&target).await { - f - } else { + let Ok(file) = File::from(&target).await else { return false; }; @@ -128,9 +126,7 @@ impl Tab { } pub fn enter(&mut self) -> bool { - let hovered = if let Some(ref h) = self.current.hovered { - h.clone() - } else { + let Some(hovered) = self.current.hovered.clone() else { return false; }; if !hovered.meta.is_dir() { @@ -161,9 +157,7 @@ impl Tab { .and_then(|p| if p == self.current.cwd { None } else { Some(p) }) .or_else(|| self.current.cwd.parent()); - let current = if let Some(c) = current { - c.to_owned() - } else { + let Some(current) = current.map(Path::to_path_buf) else { return false; }; diff --git a/core/src/manager/watcher.rs b/core/src/manager/watcher.rs index 18e4b334..f8be2d86 100644 --- a/core/src/manager/watcher.rs +++ b/core/src/manager/watcher.rs @@ -21,14 +21,11 @@ impl Watcher { { let tx = tx.clone(); move |res: Result| { - if res.is_err() { + let Ok(event) = res else { return; - } + }; - let event = res.unwrap(); - let path = if let Some(first) = event.paths.first() { - first.clone() - } else { + let Some(path) = event.paths.first().cloned() else { return; }; @@ -61,10 +58,9 @@ impl Watcher { } }, Default::default(), - ) - .unwrap(); + ); - let instance = Self { tx, watcher, watched: Default::default() }; + let instance = Self { tx, watcher: watcher.unwrap(), watched: Default::default() }; tokio::spawn(Self::changed(rx, instance.watched.clone())); instance } diff --git a/core/src/tasks/tasks.rs b/core/src/tasks/tasks.rs index 56919a16..3f302a9f 100644 --- a/core/src/tasks/tasks.rs +++ b/core/src/tasks/tasks.rs @@ -60,9 +60,7 @@ impl Tasks { } pub fn inspect(&self) -> bool { - let id = if let Some(id) = self.scheduler.running.read().get_id(self.cursor) { - id - } else { + let Some(id) = self.scheduler.running.read().get_id(self.cursor) else { return false; }; @@ -73,7 +71,7 @@ impl Tasks { let buffered = { let mut running = scheduler.running.write(); - let task = if let Some(task) = running.get_mut(id) { task } else { return }; + let Some(task) = running.get_mut(id) else { return }; task.logger = Some(tx); task.logs.clone() diff --git a/core/src/tasks/workers/file.rs b/core/src/tasks/workers/file.rs index 1551ace1..8f2f98f9 100644 --- a/core/src/tasks/workers/file.rs +++ b/core/src/tasks/workers/file.rs @@ -212,9 +212,7 @@ impl File { while let Ok(Some(entry)) = it.next_entry().await { let src = entry.path(); - let meta = if let Ok(meta) = Self::metadata(&src, task.follow).await { - meta - } else { + let Ok(meta) = Self::metadata(&src, task.follow).await else { continue; }; diff --git a/shared/src/fns.rs b/shared/src/fns.rs index e607c9de..1691d98d 100644 --- a/shared/src/fns.rs +++ b/shared/src/fns.rs @@ -4,12 +4,9 @@ use tokio::fs; pub async fn absolute_path(p: impl AsRef) -> PathBuf { let p = p.as_ref(); - if p.starts_with("~") { - if let Ok(home) = env::var("HOME") { - let mut expanded = PathBuf::new(); - expanded.push(home); - expanded.push(p.strip_prefix("~").unwrap()); - return expanded; + if let Ok(p) = p.strip_prefix("~") { + if let Some(home) = env::var_os("HOME") { + return PathBuf::from_iter([&home, p.as_os_str()]); } } fs::canonicalize(p).await.unwrap_or_else(|_| p.to_path_buf()) @@ -43,9 +40,7 @@ pub fn readable_size(size: u64) -> String { } pub async fn unique_path(mut p: PathBuf) -> PathBuf { - let name = if let Some(name) = p.file_name() { - name.to_os_string() - } else { + let Some(name) = p.file_name().map(|n| n.to_os_string()) else { return p; }; diff --git a/shared/src/fs.rs b/shared/src/fs.rs index 8201f44e..8240e7f0 100644 --- a/shared/src/fs.rs +++ b/shared/src/fs.rs @@ -7,9 +7,7 @@ pub async fn calculate_size(path: &Path) -> u64 { let mut total = 0; let mut stack = VecDeque::from([path.to_path_buf()]); while let Some(path) = stack.pop_front() { - let meta = if let Ok(meta) = fs::symlink_metadata(&path).await { - meta - } else { + let Ok(meta) = fs::symlink_metadata(&path).await else { continue; }; @@ -18,16 +16,12 @@ pub async fn calculate_size(path: &Path) -> u64 { continue; } - let mut it = if let Ok(it) = fs::read_dir(path).await { - it - } else { + let Ok(mut it) = fs::read_dir(path).await else { continue; }; while let Ok(Some(entry)) = it.next_entry().await { - let meta = if let Ok(m) = entry.metadata().await { - m - } else { + let Ok(meta) = entry.metadata().await else { continue; };