aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2024-11-14 09:44:07 +0300
committerIvan Molodetskikh <yalterz@gmail.com>2024-11-14 09:44:07 +0300
commit10286391868a6591650c66df7987bca8271dcda1 (patch)
tree26b2a64285f1cc0f9107871916b62b17cdb43e05
parent0e5e764c78421dc61c82ca6df630f33e8221005a (diff)
downloadniri-10286391868a6591650c66df7987bca8271dcda1.tar.gz
niri-10286391868a6591650c66df7987bca8271dcda1.tar.bz2
niri-10286391868a6591650c66df7987bca8271dcda1.zip
config: Add RegexEq util type instead of manual PartialEq
-rw-r--r--niri-config/src/lib.rs26
-rw-r--r--niri-config/src/utils.rs23
-rw-r--r--src/window/mod.rs4
3 files changed, 33 insertions, 20 deletions
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<bool>,
}
-// 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<Regex>,
+ pub app_id: Option<RegexEq>,
#[knuffel(property, str)]
- pub title: Option<Regex>,
+ pub title: Option<RegexEq>,
#[knuffel(property)]
pub is_active: Option<bool>,
#[knuffel(property)]
@@ -1019,17 +1020,6 @@ pub struct Match {
pub at_startup: Option<bool>,
}
-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 = <Regex as FromStr>::Err;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ 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;
}
}