From 38e45c647b9e93a9d837b6f4e3f345d85608cc1a 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: Fri, 14 Feb 2025 23:17:06 +0800 Subject: [PATCH] refactor: consistent naming for enum variants (#2339) --- .github/DISCUSSION_TEMPLATE/1-q-a.yml | 2 ++ Cargo.lock | 17 ++++++------ Cargo.toml | 2 +- yazi-config/src/popup/origin.rs | 37 +++++++-------------------- yazi-config/src/preview/wrap.rs | 23 +---------------- yazi-config/src/priority.rs | 24 +---------------- yazi-config/src/theme/is.rs | 31 +--------------------- yazi-config/src/which/sorting.rs | 24 +---------------- yazi-fs/src/sorting.rs | 23 +++-------------- yazi-plugin/preset/plugins/json.lua | 7 +++-- yazi-proxy/Cargo.toml | 1 + yazi-proxy/src/options/notify.rs | 13 +++------- yazi-shared/src/layer.rs | 21 ++++----------- 13 files changed, 41 insertions(+), 184 deletions(-) diff --git a/.github/DISCUSSION_TEMPLATE/1-q-a.yml b/.github/DISCUSSION_TEMPLATE/1-q-a.yml index c667e122..ab64c16e 100644 --- a/.github/DISCUSSION_TEMPLATE/1-q-a.yml +++ b/.github/DISCUSSION_TEMPLATE/1-q-a.yml @@ -50,5 +50,7 @@ body: label: Checklist description: Before submitting the post, please make sure you have completed the following options: + - label: I have read all the documentation + required: true - label: I have searched the existing discussions/issues required: true diff --git a/Cargo.lock b/Cargo.lock index 78bd19bc..5f873add 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -334,9 +334,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.13" +version = "1.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" +checksum = "0c3d1b2e905a3a7b00a6141adb0e4c0bb941d11caf55349d863942a1cc44e3c9" dependencies = [ "jobserver", "libc", @@ -373,9 +373,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.28" +version = "4.5.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff" +checksum = "8acebd8ad879283633b343856142139f2da2317c96b05b4dd6181c61e2480184" dependencies = [ "clap_builder", "clap_derive", @@ -383,9 +383,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.27" +version = "4.5.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7" +checksum = "f6ba32cbda51c7e1dfd49acc1457ba1a7dec5b64fe360e828acb13ca8dc9c2f9" dependencies = [ "anstream", "anstyle", @@ -1529,9 +1529,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924" +checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" dependencies = [ "adler2", "simd-adler32", @@ -3581,6 +3581,7 @@ version = "25.2.11" dependencies = [ "anyhow", "mlua", + "serde", "tokio", "yazi-config", "yazi-macro", diff --git a/Cargo.toml b/Cargo.toml index 6ed62059..0fbb320b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ ansi-to-tui = "7.0.0" anyhow = "1.0.95" base64 = "0.22.1" bitflags = "2.8.0" -clap = { version = "4.5.28", features = [ "derive" ] } +clap = { version = "4.5.29", features = [ "derive" ] } core-foundation-sys = "0.8.7" crossterm = { version = "0.28.1", features = [ "event-stream" ] } dirs = "6.0.0" diff --git a/yazi-config/src/popup/origin.rs b/yazi-config/src/popup/origin.rs index 39ffa1d6..b11b6df4 100644 --- a/yazi-config/src/popup/origin.rs +++ b/yazi-config/src/popup/origin.rs @@ -1,10 +1,9 @@ use std::{fmt::Display, str::FromStr}; -use anyhow::bail; use serde::Deserialize; #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum Origin { #[default] TopLeft, @@ -19,32 +18,6 @@ pub enum Origin { Hovered, } -impl FromStr for Origin { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "top-left" => Self::TopLeft, - "top-center" => Self::TopCenter, - "top-right" => Self::TopRight, - - "bottom-left" => Self::BottomLeft, - "bottom-center" => Self::BottomCenter, - "bottom-right" => Self::BottomRight, - - "center" => Self::Center, - "hovered" => Self::Hovered, - _ => bail!("Invalid `origin` value: {s}"), - }) - } -} - -impl TryFrom for Origin { - type Error = anyhow::Error; - - fn try_from(value: String) -> Result { Self::from_str(&value) } -} - impl Display for Origin { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { @@ -61,3 +34,11 @@ impl Display for Origin { }) } } + +impl FromStr for Origin { + type Err = serde::de::value::Error; + + fn from_str(s: &str) -> Result { + Self::deserialize(serde::de::value::StrDeserializer::new(s)) + } +} diff --git a/yazi-config/src/preview/wrap.rs b/yazi-config/src/preview/wrap.rs index cdf28245..af24f20e 100644 --- a/yazi-config/src/preview/wrap.rs +++ b/yazi-config/src/preview/wrap.rs @@ -1,29 +1,8 @@ -use std::str::FromStr; - -use anyhow::bail; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum PreviewWrap { No, Yes, } - -impl FromStr for PreviewWrap { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "no" => Self::No, - "yes" => Self::Yes, - _ => bail!("Invalid `wrap` value: {s}"), - }) - } -} - -impl TryFrom for PreviewWrap { - type Error = anyhow::Error; - - fn try_from(value: String) -> Result { Self::from_str(&value) } -} diff --git a/yazi-config/src/priority.rs b/yazi-config/src/priority.rs index 50535297..1aa5ff9d 100644 --- a/yazi-config/src/priority.rs +++ b/yazi-config/src/priority.rs @@ -1,32 +1,10 @@ -use std::str::FromStr; - -use anyhow::anyhow; use serde::Deserialize; #[derive(Default, Clone, Copy, Debug, Deserialize)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum Priority { Low = 0, #[default] Normal = 1, High = 2, } - -impl FromStr for Priority { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s { - "low" => Ok(Self::Low), - "normal" => Ok(Self::Normal), - "high" => Ok(Self::High), - _ => Err(anyhow!("Invalid priority: {s}")), - } - } -} - -impl TryFrom for Priority { - type Error = anyhow::Error; - - fn try_from(s: String) -> Result { Self::from_str(&s) } -} diff --git a/yazi-config/src/theme/is.rs b/yazi-config/src/theme/is.rs index 92b1813e..61f18b9b 100644 --- a/yazi-config/src/theme/is.rs +++ b/yazi-config/src/theme/is.rs @@ -1,11 +1,8 @@ -use std::str::FromStr; - -use anyhow::bail; use serde::Deserialize; use yazi_fs::Cha; #[derive(Default, Deserialize)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum Is { #[default] None, @@ -21,32 +18,6 @@ pub enum Is { Sticky, } -impl FromStr for Is { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "hidden" => Self::Hidden, - "link" => Self::Link, - "orphan" => Self::Orphan, - "dummy" => Self::Dummy, - "block" => Self::Block, - "char" => Self::Char, - "fifo" => Self::Fifo, - "sock" => Self::Sock, - "exec" => Self::Exec, - "sticky" => Self::Sticky, - _ => bail!("invalid filetype: {s}"), - }) - } -} - -impl TryFrom for Is { - type Error = anyhow::Error; - - fn try_from(s: String) -> Result { Self::from_str(&s) } -} - impl Is { #[inline] pub fn check(&self, cha: &Cha) -> bool { diff --git a/yazi-config/src/which/sorting.rs b/yazi-config/src/which/sorting.rs index 73939d58..2a4c475a 100644 --- a/yazi-config/src/which/sorting.rs +++ b/yazi-config/src/which/sorting.rs @@ -1,32 +1,10 @@ -use std::str::FromStr; - -use anyhow::bail; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum SortBy { #[default] None, Key, Desc, } - -impl FromStr for SortBy { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "none" => Self::None, - "key" => Self::Key, - "desc" => Self::Desc, - _ => bail!("Invalid `sort_by` value: {s}"), - }) - } -} - -impl TryFrom for SortBy { - type Error = anyhow::Error; - - fn try_from(value: String) -> Result { Self::from_str(&value) } -} diff --git a/yazi-fs/src/sorting.rs b/yazi-fs/src/sorting.rs index 0ae94a28..14b8a976 100644 --- a/yazi-fs/src/sorting.rs +++ b/yazi-fs/src/sorting.rs @@ -1,10 +1,9 @@ use std::{fmt::Display, str::FromStr}; -use anyhow::bail; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] -#[serde(try_from = "String")] +#[serde(rename_all = "kebab-case")] pub enum SortBy { #[default] None, @@ -18,29 +17,13 @@ pub enum SortBy { } impl FromStr for SortBy { - type Err = anyhow::Error; + type Err = serde::de::value::Error; fn from_str(s: &str) -> Result { - Ok(match s { - "none" => Self::None, - "mtime" => Self::Mtime, - "btime" => Self::Btime, - "extension" => Self::Extension, - "alphabetical" => Self::Alphabetical, - "natural" => Self::Natural, - "size" => Self::Size, - "random" => Self::Random, - _ => bail!("invalid sort_by value: {s}"), - }) + Self::deserialize(serde::de::value::StrDeserializer::new(s)) } } -impl TryFrom for SortBy { - type Error = anyhow::Error; - - fn try_from(s: String) -> Result { Self::from_str(&s) } -} - impl Display for SortBy { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { diff --git a/yazi-plugin/preset/plugins/json.lua b/yazi-plugin/preset/plugins/json.lua index d4a5d491..14503e30 100644 --- a/yazi-plugin/preset/plugins/json.lua +++ b/yazi-plugin/preset/plugins/json.lua @@ -32,10 +32,9 @@ function M:peek(job) ya.manager_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true }) else lines = lines:gsub("\t", string.rep(" ", PREVIEW.tab_size)) - ya.preview_widgets( - job, - { ui.Text.parse(lines):area(job.area):wrap(PREVIEW.wrap == "Yes" and ui.Text.WRAP or ui.Text.WRAP_NO) } - ) + ya.preview_widgets(job, { + ui.Text.parse(lines):area(job.area):wrap(PREVIEW.wrap == "yes" and ui.Text.WRAP or ui.Text.WRAP_NO), + }) end end diff --git a/yazi-proxy/Cargo.toml b/yazi-proxy/Cargo.toml index c284180f..a69793c3 100644 --- a/yazi-proxy/Cargo.toml +++ b/yazi-proxy/Cargo.toml @@ -20,4 +20,5 @@ yazi-shared = { path = "../yazi-shared", version = "25.2.11" } # External dependencies anyhow = { workspace = true } mlua = { workspace = true } +serde = { workspace = true } tokio = { workspace = true } diff --git a/yazi-proxy/src/options/notify.rs b/yazi-proxy/src/options/notify.rs index 1c2669bc..6c8f091d 100644 --- a/yazi-proxy/src/options/notify.rs +++ b/yazi-proxy/src/options/notify.rs @@ -1,7 +1,7 @@ use std::{str::FromStr, time::Duration}; -use anyhow::bail; use mlua::{ExternalError, ExternalResult}; +use serde::Deserialize; use yazi_config::THEME; use yazi_shared::{event::CmdCow, theme::Style}; @@ -42,7 +42,7 @@ impl TryFrom for NotifyOpt { } } -#[derive(Clone, Copy, Default, Eq, PartialEq)] +#[derive(Clone, Copy, Default, Deserialize, Eq, PartialEq)] pub enum NotifyLevel { #[default] Info, @@ -71,14 +71,9 @@ impl NotifyLevel { } impl FromStr for NotifyLevel { - type Err = anyhow::Error; + type Err = serde::de::value::Error; fn from_str(s: &str) -> Result { - Ok(match s { - "info" => Self::Info, - "warn" => Self::Warn, - "error" => Self::Error, - _ => bail!("Invalid notify level: {s}"), - }) + Self::deserialize(serde::de::value::StrDeserializer::new(s)) } } diff --git a/yazi-shared/src/layer.rs b/yazi-shared/src/layer.rs index 06b0689a..3a1ca21a 100644 --- a/yazi-shared/src/layer.rs +++ b/yazi-shared/src/layer.rs @@ -1,8 +1,9 @@ use std::{fmt::Display, str::FromStr}; -use anyhow::bail; +use serde::Deserialize; -#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)] +#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy, Deserialize)] +#[serde(rename_all = "kebab-case")] pub enum Layer { #[default] App, @@ -35,21 +36,9 @@ impl Display for Layer { } impl FromStr for Layer { - type Err = anyhow::Error; + type Err = serde::de::value::Error; fn from_str(s: &str) -> Result { - Ok(match s { - "app" => Self::App, - "manager" => Self::Manager, - "tasks" => Self::Tasks, - "spot" => Self::Spot, - "pick" => Self::Pick, - "input" => Self::Input, - "confirm" => Self::Confirm, - "help" => Self::Help, - "completion" => Self::Completion, - "which" => Self::Which, - _ => bail!("invalid layer: {s}"), - }) + Self::deserialize(serde::de::value::StrDeserializer::new(s)) } }