From 617716e1839e0c277f1436a93ead086b72052530 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 26 Feb 2024 10:11:33 +0800 Subject: [PATCH] Simplify the code --- yazi-config/src/theme/filetype.rs | 62 ++----------------------------- yazi-config/src/theme/icon.rs | 62 ++----------------------------- yazi-config/src/theme/is.rs | 61 ++++++++++++++++++++++++++++++ yazi-config/src/theme/mod.rs | 2 + 4 files changed, 71 insertions(+), 116 deletions(-) create mode 100644 yazi-config/src/theme/is.rs diff --git a/yazi-config/src/theme/filetype.rs b/yazi-config/src/theme/filetype.rs index 94ffbed0..e61069f5 100644 --- a/yazi-config/src/theme/filetype.rs +++ b/yazi-config/src/theme/filetype.rs @@ -1,14 +1,11 @@ -use std::str::FromStr; - -use anyhow::bail; use serde::{Deserialize, Deserializer}; use yazi_shared::fs::File; -use super::{Color, Style, StyleShadow}; +use super::{Color, Is, Style, StyleShadow}; use crate::Pattern; pub struct Filetype { - pub is: FiletypeIs, + pub is: Is, pub name: Option, pub mime: Option, pub style: Style, @@ -16,18 +13,7 @@ pub struct Filetype { impl Filetype { pub fn matches(&self, file: &File, mime: Option<&str>) -> bool { - let b = match self.is { - FiletypeIs::None => true, - FiletypeIs::Block => file.cha.is_block_device(), - FiletypeIs::Char => file.cha.is_char_device(), - FiletypeIs::Exec => file.cha.is_exec(), - FiletypeIs::Fifo => file.cha.is_fifo(), - FiletypeIs::Link => file.cha.is_link(), - FiletypeIs::Orphan => file.cha.is_orphan(), - FiletypeIs::Sock => file.cha.is_socket(), - FiletypeIs::Sticky => file.cha.is_sticky(), - }; - if !b { + if !self.is.check(&file.cha) { return false; } @@ -48,7 +34,7 @@ impl Filetype { #[derive(Deserialize)] struct FiletypeRule { #[serde(default)] - is: FiletypeIs, + is: Is, name: Option, mime: Option, @@ -101,43 +87,3 @@ impl Filetype { ) } } - -// --- FiletypeIs -#[derive(Default, Deserialize)] -#[serde(try_from = "String")] -pub enum FiletypeIs { - #[default] - None, - Block, - Char, - Exec, - Fifo, - Link, - Orphan, - Sock, - Sticky, -} - -impl FromStr for FiletypeIs { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "block" => Self::Block, - "char" => Self::Char, - "exec" => Self::Exec, - "fifo" => Self::Fifo, - "link" => Self::Link, - "orphan" => Self::Orphan, - "sock" => Self::Sock, - "sticky" => Self::Sticky, - _ => bail!("invalid filetype: {s}"), - }) - } -} - -impl TryFrom for FiletypeIs { - type Error = anyhow::Error; - - fn try_from(s: String) -> Result { Self::from_str(&s) } -} diff --git a/yazi-config/src/theme/icon.rs b/yazi-config/src/theme/icon.rs index 3933b5b9..c9b0f4db 100644 --- a/yazi-config/src/theme/icon.rs +++ b/yazi-config/src/theme/icon.rs @@ -1,14 +1,11 @@ -use std::str::FromStr; - -use anyhow::bail; use serde::{Deserialize, Deserializer}; use yazi_shared::fs::File; use super::Style; -use crate::{preset::Preset, theme::{Color, StyleShadow}, Pattern}; +use crate::{preset::Preset, theme::{Color, Is, StyleShadow}, Pattern}; pub struct Icon { - pub is: FiletypeIs, + pub is: Is, pub name: Pattern, pub text: String, pub style: Style, @@ -16,18 +13,7 @@ pub struct Icon { impl Icon { pub fn matches(&self, file: &File) -> bool { - let b = match self.is { - FiletypeIs::None => true, - FiletypeIs::Block => file.cha.is_block_device(), - FiletypeIs::Char => file.cha.is_char_device(), - FiletypeIs::Exec => file.cha.is_exec(), - FiletypeIs::Fifo => file.cha.is_fifo(), - FiletypeIs::Link => file.cha.is_link(), - FiletypeIs::Orphan => file.cha.is_orphan(), - FiletypeIs::Sock => file.cha.is_socket(), - FiletypeIs::Sticky => file.cha.is_sticky(), - }; - if !b { + if !self.is.check(&file.cha) { return false; } @@ -51,7 +37,7 @@ impl Icon { #[derive(Deserialize)] struct IconRule { #[serde(default)] - is: FiletypeIs, + is: Is, name: Pattern, text: String, @@ -82,43 +68,3 @@ impl Icon { ) } } - -// --- FiletypeIs -#[derive(Default, Deserialize)] -#[serde(try_from = "String")] -pub enum FiletypeIs { - #[default] - None, - Block, - Char, - Exec, - Fifo, - Link, - Orphan, - Sock, - Sticky, -} - -impl FromStr for FiletypeIs { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok(match s { - "block" => Self::Block, - "char" => Self::Char, - "exec" => Self::Exec, - "fifo" => Self::Fifo, - "link" => Self::Link, - "orphan" => Self::Orphan, - "sock" => Self::Sock, - "sticky" => Self::Sticky, - _ => bail!("invalid filetype: {s}"), - }) - } -} - -impl TryFrom for FiletypeIs { - 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 new file mode 100644 index 00000000..994d620b --- /dev/null +++ b/yazi-config/src/theme/is.rs @@ -0,0 +1,61 @@ +use std::str::FromStr; + +use anyhow::bail; +use serde::Deserialize; +use yazi_shared::fs::Cha; + +#[derive(Default, Deserialize)] +#[serde(try_from = "String")] +pub enum Is { + #[default] + None, + Block, + Char, + Exec, + Fifo, + Link, + Orphan, + Sock, + Sticky, +} + +impl FromStr for Is { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + Ok(match s { + "block" => Self::Block, + "char" => Self::Char, + "exec" => Self::Exec, + "fifo" => Self::Fifo, + "link" => Self::Link, + "orphan" => Self::Orphan, + "sock" => Self::Sock, + "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 { + match self { + Self::None => true, + Self::Block => cha.is_block_device(), + Self::Char => cha.is_char_device(), + Self::Exec => cha.is_exec(), + Self::Fifo => cha.is_fifo(), + Self::Link => cha.is_link(), + Self::Orphan => cha.is_orphan(), + Self::Sock => cha.is_socket(), + Self::Sticky => cha.is_sticky(), + } + } +} diff --git a/yazi-config/src/theme/mod.rs b/yazi-config/src/theme/mod.rs index ed9aa3df..c3f0b004 100644 --- a/yazi-config/src/theme/mod.rs +++ b/yazi-config/src/theme/mod.rs @@ -1,11 +1,13 @@ mod color; mod filetype; mod icon; +mod is; mod style; mod theme; pub use color::*; pub use filetype::*; pub use icon::*; +pub use is::*; pub use style::*; pub use theme::*;