config: Add RegexEq util type instead of manual PartialEq
Browse files at this commit10286391868a6591650c66df7987bca8271dcda1Parent: 0e5e7643 changed files +33 -20
- niri-config/src/lib.rs +8 -18
- niri-config/src/utils.rs +23 -0
- src/window/mod.rs +2 -2
niri-config/src/lib.rsmodified+8 -18
| @@ -12,7 +12,6 @@ use knuffel::errors::DecodeError; | |||
| 12 | use knuffel::Decode as _; | 12 | use knuffel::Decode as _; |
| 13 | use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; | 13 | use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; |
| 14 | use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg}; | 14 | use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg}; |
| 15 | -use regex::Regex; | ||
| 16 | use smithay::backend::renderer::Color32F; | 15 | use smithay::backend::renderer::Color32F; |
| 17 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; | 16 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; |
| 18 | use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; | 17 | use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; |
| @@ -21,6 +20,9 @@ use smithay::reexports::input; | |||
| 21 | 20 | ||
| 22 | pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.2, 0.2, 0.2, 1.]); | 21 | pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.2, 0.2, 0.2, 1.]); |
| 23 | 22 | ||
| 23 | +mod utils; | ||
| 24 | +pub use utils::RegexEq; | ||
| 25 | + | ||
| 24 | #[derive(knuffel::Decode, Debug, PartialEq)] | 26 | #[derive(knuffel::Decode, Debug, PartialEq)] |
| 25 | pub struct Config { | 27 | pub struct Config { |
| 26 | #[knuffel(child, default)] | 28 | #[knuffel(child, default)] |
| @@ -1002,13 +1004,12 @@ pub struct WindowRule { | |||
| 1002 | pub variable_refresh_rate: Option<bool>, | 1004 | pub variable_refresh_rate: Option<bool>, |
| 1003 | } | 1005 | } |
| 1004 | 1006 | ||
| 1005 | -// Remember to update the PartialEq impl when adding fields! | 1007 | +#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| 1006 | -#[derive(knuffel::Decode, Debug, Default, Clone)] | ||
| 1007 | pub struct Match { | 1008 | pub struct Match { |
| 1008 | #[knuffel(property, str)] | 1009 | #[knuffel(property, str)] |
| 1009 | - pub app_id: Option<Regex>, | 1010 | + pub app_id: Option<RegexEq>, |
| 1010 | #[knuffel(property, str)] | 1011 | #[knuffel(property, str)] |
| 1011 | - pub title: Option<Regex>, | 1012 | + pub title: Option<RegexEq>, |
| 1012 | #[knuffel(property)] | 1013 | #[knuffel(property)] |
| 1013 | pub is_active: Option<bool>, | 1014 | pub is_active: Option<bool>, |
| 1014 | #[knuffel(property)] | 1015 | #[knuffel(property)] |
| @@ -1019,17 +1020,6 @@ pub struct Match { | |||
| 1019 | pub at_startup: Option<bool>, | 1020 | pub at_startup: Option<bool>, |
| 1020 | } | 1021 | } |
| 1021 | 1022 | ||
| 1022 | -impl PartialEq for Match { | ||
| 1023 | - fn eq(&self, other: &Self) -> bool { | ||
| 1024 | - self.is_active == other.is_active | ||
| 1025 | - && self.is_focused == other.is_focused | ||
| 1026 | - && self.is_active_in_column == other.is_active_in_column | ||
| 1027 | - && self.at_startup == other.at_startup | ||
| 1028 | - && self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) | ||
| 1029 | - && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) | ||
| 1030 | - } | ||
| 1031 | -} | ||
| 1032 | - | ||
| 1033 | #[derive(Debug, Default, Clone, Copy, PartialEq)] | 1023 | #[derive(Debug, Default, Clone, Copy, PartialEq)] |
| 1034 | pub struct CornerRadius { | 1024 | pub struct CornerRadius { |
| 1035 | pub top_left: f32, | 1025 | pub top_left: f32, |
| @@ -3361,7 +3351,7 @@ mod tests { | |||
| 3361 | ]), | 3351 | ]), |
| 3362 | window_rules: vec![WindowRule { | 3352 | window_rules: vec![WindowRule { |
| 3363 | matches: vec![Match { | 3353 | matches: vec![Match { |
| 3364 | - app_id: Some(Regex::new(".*alacritty").unwrap()), | 3354 | + app_id: Some(RegexEq::from_str(".*alacritty").unwrap()), |
| 3365 | title: None, | 3355 | title: None, |
| 3366 | is_active: None, | 3356 | is_active: None, |
| 3367 | is_focused: None, | 3357 | is_focused: None, |
| @@ -3371,7 +3361,7 @@ mod tests { | |||
| 3371 | excludes: vec![ | 3361 | excludes: vec![ |
| 3372 | Match { | 3362 | Match { |
| 3373 | app_id: None, | 3363 | app_id: None, |
| 3374 | - title: Some(Regex::new("~").unwrap()), | 3364 | + title: Some(RegexEq::from_str("~").unwrap()), |
| 3375 | is_active: None, | 3365 | is_active: None, |
| 3376 | is_focused: None, | 3366 | is_focused: None, |
| 3377 | is_active_in_column: None, | 3367 | is_active_in_column: None, |
niri-config/src/utils.rsadded+23 -0
| @@ -0,0 +1,23 @@ | |||
| 1 | +use std::str::FromStr; | ||
| 2 | + | ||
| 3 | +use regex::Regex; | ||
| 4 | + | ||
| 5 | +/// `Regex` that implements `PartialEq` by its string form. | ||
| 6 | +#[derive(Debug, Clone)] | ||
| 7 | +pub struct RegexEq(pub Regex); | ||
| 8 | + | ||
| 9 | +impl PartialEq for RegexEq { | ||
| 10 | + fn eq(&self, other: &Self) -> bool { | ||
| 11 | + self.0.as_str() == other.0.as_str() | ||
| 12 | + } | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +impl Eq for RegexEq {} | ||
| 16 | + | ||
| 17 | +impl FromStr for RegexEq { | ||
| 18 | + type Err = <Regex as FromStr>::Err; | ||
| 19 | + | ||
| 20 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 21 | + Regex::from_str(s).map(Self) | ||
| 22 | + } | ||
| 23 | +} | ||
src/window/mod.rsmodified+2 -2
| @@ -262,7 +262,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: | |||
| 262 | let Some(app_id) = &role.app_id else { | 262 | let Some(app_id) = &role.app_id else { |
| 263 | return false; | 263 | return false; |
| 264 | }; | 264 | }; |
| 265 | - if !app_id_re.is_match(app_id) { | 265 | + if !app_id_re.0.is_match(app_id) { |
| 266 | return false; | 266 | return false; |
| 267 | } | 267 | } |
| 268 | } | 268 | } |
| @@ -271,7 +271,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: | |||
| 271 | let Some(title) = &role.title else { | 271 | let Some(title) = &role.title else { |
| 272 | return false; | 272 | return false; |
| 273 | }; | 273 | }; |
| 274 | - if !title_re.is_match(title) { | 274 | + if !title_re.0.is_match(title) { |
| 275 | return false; | 275 | return false; |
| 276 | } | 276 | } |
| 277 | } | 277 | } |