config: Add RegexEq util type instead of manual PartialEq

This commit is contained in:
Ivan Molodetskikh
2024-11-14 09:44:07 +03:00
parent 0e5e764c78
commit 1028639186
3 changed files with 33 additions and 20 deletions
+8 -18
View File
@@ -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,
+23
View File
@@ -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)
}
}
+2 -2
View File
@@ -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;
}
}