refactor: simplify the flow with let-else (#56)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-15 07:27:46 +08:00 committed by GitHub
parent 7e32b5de6a
commit 80619597d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 30 additions and 67 deletions

View File

@ -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);
}
}

View File

@ -27,7 +27,7 @@ impl Default for Key {
impl From<KeyEvent> 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,

View File

@ -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 })

View File

@ -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;
};

View File

@ -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;
}

View File

@ -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;
};

View File

@ -21,14 +21,11 @@ impl Watcher {
{
let tx = tx.clone();
move |res: Result<notify::Event, notify::Error>| {
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
}

View File

@ -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()

View File

@ -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;
};

View File

@ -4,12 +4,9 @@ use tokio::fs;
pub async fn absolute_path(p: impl AsRef<Path>) -> 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;
};

View File

@ -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;
};