From 10286391868a6591650c66df7987bca8271dcda1 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Thu, 14 Nov 2024 09:44:07 +0300 Subject: config: Add RegexEq util type instead of manual PartialEq --- niri-config/src/lib.rs | 26 ++++++++------------------ niri-config/src/utils.rs | 23 +++++++++++++++++++++++ src/window/mod.rs | 4 ++-- 3 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 niri-config/src/utils.rs diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index ceeb36b8..7fa4f527 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -12,7 +12,6 @@ use knuffel::errors::DecodeError; use knuffel::Decode as _; use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg}; -use regex::Regex; use smithay::backend::renderer::Color32F; use smithay::input::keyboard::keysyms::KEY_NoSymbol; use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; @@ -21,6 +20,9 @@ use smithay::reexports::input; pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.2, 0.2, 0.2, 1.]); +mod utils; +pub use utils::RegexEq; + #[derive(knuffel::Decode, Debug, PartialEq)] pub struct Config { #[knuffel(child, default)] @@ -1002,13 +1004,12 @@ pub struct WindowRule { pub variable_refresh_rate: Option, } -// Remember to update the PartialEq impl when adding fields! -#[derive(knuffel::Decode, Debug, Default, Clone)] +#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] pub struct Match { #[knuffel(property, str)] - pub app_id: Option, + pub app_id: Option, #[knuffel(property, str)] - pub title: Option, + pub title: Option, #[knuffel(property)] pub is_active: Option, #[knuffel(property)] @@ -1019,17 +1020,6 @@ pub struct Match { pub at_startup: Option, } -impl PartialEq for Match { - fn eq(&self, other: &Self) -> bool { - self.is_active == other.is_active - && self.is_focused == other.is_focused - && self.is_active_in_column == other.is_active_in_column - && self.at_startup == other.at_startup - && self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) - && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) - } -} - #[derive(Debug, Default, Clone, Copy, PartialEq)] pub struct CornerRadius { pub top_left: f32, @@ -3361,7 +3351,7 @@ mod tests { ]), window_rules: vec![WindowRule { matches: vec![Match { - app_id: Some(Regex::new(".*alacritty").unwrap()), + app_id: Some(RegexEq::from_str(".*alacritty").unwrap()), title: None, is_active: None, is_focused: None, @@ -3371,7 +3361,7 @@ mod tests { excludes: vec![ Match { app_id: None, - title: Some(Regex::new("~").unwrap()), + title: Some(RegexEq::from_str("~").unwrap()), is_active: None, is_focused: None, is_active_in_column: None, diff --git a/niri-config/src/utils.rs b/niri-config/src/utils.rs new file mode 100644 index 00000000..cce4f398 --- /dev/null +++ b/niri-config/src/utils.rs @@ -0,0 +1,23 @@ +use std::str::FromStr; + +use regex::Regex; + +/// `Regex` that implements `PartialEq` by its string form. +#[derive(Debug, Clone)] +pub struct RegexEq(pub Regex); + +impl PartialEq for RegexEq { + fn eq(&self, other: &Self) -> bool { + self.0.as_str() == other.0.as_str() + } +} + +impl Eq for RegexEq {} + +impl FromStr for RegexEq { + type Err = ::Err; + + fn from_str(s: &str) -> Result { + Regex::from_str(s).map(Self) + } +} diff --git a/src/window/mod.rs b/src/window/mod.rs index 72917c25..7c77a2a7 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -262,7 +262,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: let Some(app_id) = &role.app_id else { return false; }; - if !app_id_re.is_match(app_id) { + if !app_id_re.0.is_match(app_id) { return false; } } @@ -271,7 +271,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: let Some(title) = &role.title else { return false; }; - if !title_re.is_match(title) { + if !title_re.0.is_match(title) { return false; } } -- cgit