Skip to content

Commit

Permalink
refactor: consistent naming for enum variants (#2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Feb 14, 2025
1 parent b9416d8 commit 38e45c6
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 184 deletions.
2 changes: 2 additions & 0 deletions .github/DISCUSSION_TEMPLATE/1-q-a.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 9 additions & 28 deletions yazi-config/src/popup/origin.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,32 +18,6 @@ pub enum Origin {
Hovered,
}

impl FromStr for Origin {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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<String> for Origin {
type Error = anyhow::Error;

fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) }
}

impl Display for Origin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Expand All @@ -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, Self::Err> {
Self::deserialize(serde::de::value::StrDeserializer::new(s))
}
}
23 changes: 1 addition & 22 deletions yazi-config/src/preview/wrap.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
Ok(match s {
"no" => Self::No,
"yes" => Self::Yes,
_ => bail!("Invalid `wrap` value: {s}"),
})
}
}

impl TryFrom<String> for PreviewWrap {
type Error = anyhow::Error;

fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) }
}
24 changes: 1 addition & 23 deletions yazi-config/src/priority.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
match s {
"low" => Ok(Self::Low),
"normal" => Ok(Self::Normal),
"high" => Ok(Self::High),
_ => Err(anyhow!("Invalid priority: {s}")),
}
}
}

impl TryFrom<String> for Priority {
type Error = anyhow::Error;

fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
}
31 changes: 1 addition & 30 deletions yazi-config/src/theme/is.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,32 +18,6 @@ pub enum Is {
Sticky,
}

impl FromStr for Is {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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<String> for Is {
type Error = anyhow::Error;

fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
}

impl Is {
#[inline]
pub fn check(&self, cha: &Cha) -> bool {
Expand Down
24 changes: 1 addition & 23 deletions yazi-config/src/which/sorting.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
Ok(match s {
"none" => Self::None,
"key" => Self::Key,
"desc" => Self::Desc,
_ => bail!("Invalid `sort_by` value: {s}"),
})
}
}

impl TryFrom<String> for SortBy {
type Error = anyhow::Error;

fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) }
}
23 changes: 3 additions & 20 deletions yazi-fs/src/sorting.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Self, Self::Err> {
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<String> for SortBy {
type Error = anyhow::Error;

fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
}

impl Display for SortBy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Expand Down
7 changes: 3 additions & 4 deletions yazi-plugin/preset/plugins/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions yazi-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
13 changes: 4 additions & 9 deletions yazi-proxy/src/options/notify.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -42,7 +42,7 @@ impl TryFrom<mlua::Table> for NotifyOpt {
}
}

#[derive(Clone, Copy, Default, Eq, PartialEq)]
#[derive(Clone, Copy, Default, Deserialize, Eq, PartialEq)]
pub enum NotifyLevel {
#[default]
Info,
Expand Down Expand Up @@ -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<Self, Self::Err> {
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))
}
}
Loading

0 comments on commit 38e45c6

Please sign in to comment.