forge
Explore
Log in

Don't you dare go Hollow

config: Add RegexEq util type instead of manual PartialEq

Browse files at this commit

10286391868a6591650c66df7987bca8271dcda1Ivan MolodetskikhParent: 0e5e764
niri-config/src/lib.rsmodified+8 -18
@@ -12,7 +12,6 @@ use knuffel::errors::DecodeError;
1212 use knuffel::Decode as _;
1313 use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler};
1414 use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg};
15-use regex::Regex;
1615 use smithay::backend::renderer::Color32F;
1716 use smithay::input::keyboard::keysyms::KEY_NoSymbol;
1817 use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE};
@@ -21,6 +20,9 @@ use smithay::reexports::input;
2120
2221 pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.2, 0.2, 0.2, 1.]);
2322
23+mod utils;
24+pub use utils::RegexEq;
25+
2426 #[derive(knuffel::Decode, Debug, PartialEq)]
2527 pub struct Config {
2628 #[knuffel(child, default)]
@@ -1002,13 +1004,12 @@ pub struct WindowRule {
10021004 pub variable_refresh_rate: Option<bool>,
10031005 }
10041006
1005-// Remember to update the PartialEq impl when adding fields!
1006-#[derive(knuffel::Decode, Debug, Default, Clone)]
1007+#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)]
10071008 pub struct Match {
10081009 #[knuffel(property, str)]
1009- pub app_id: Option<Regex>,
1010+ pub app_id: Option<RegexEq>,
10101011 #[knuffel(property, str)]
1011- pub title: Option<Regex>,
1012+ pub title: Option<RegexEq>,
10121013 #[knuffel(property)]
10131014 pub is_active: Option<bool>,
10141015 #[knuffel(property)]
@@ -1019,17 +1020,6 @@ pub struct Match {
10191020 pub at_startup: Option<bool>,
10201021 }
10211022
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-
10331023 #[derive(Debug, Default, Clone, Copy, PartialEq)]
10341024 pub struct CornerRadius {
10351025 pub top_left: f32,
@@ -3361,7 +3351,7 @@ mod tests {
33613351 ]),
33623352 window_rules: vec![WindowRule {
33633353 matches: vec![Match {
3364- app_id: Some(Regex::new(".*alacritty").unwrap()),
3354+ app_id: Some(RegexEq::from_str(".*alacritty").unwrap()),
33653355 title: None,
33663356 is_active: None,
33673357 is_focused: None,
@@ -3371,7 +3361,7 @@ mod tests {
33713361 excludes: vec![
33723362 Match {
33733363 app_id: None,
3374- title: Some(Regex::new("~").unwrap()),
3364+ title: Some(RegexEq::from_str("~").unwrap()),
33753365 is_active: None,
33763366 is_focused: None,
33773367 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:
262262 let Some(app_id) = &role.app_id else {
263263 return false;
264264 };
265- if !app_id_re.is_match(app_id) {
265+ if !app_id_re.0.is_match(app_id) {
266266 return false;
267267 }
268268 }
@@ -271,7 +271,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m:
271271 let Some(title) = &role.title else {
272272 return false;
273273 };
274- if !title_re.is_match(title) {
274+ if !title_re.0.is_match(title) {
275275 return false;
276276 }
277277 }