mirror of
https://github.com/niri-wm/niri.git
synced 2026-06-22 02:01:55 +07:00
config: Add RegexEq util type instead of manual PartialEq
This commit is contained in:
+8
-18
@@ -12,7 +12,6 @@ use knuffel::errors::DecodeError;
|
|||||||
use knuffel::Decode as _;
|
use knuffel::Decode as _;
|
||||||
use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler};
|
use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler};
|
||||||
use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg};
|
use niri_ipc::{ConfiguredMode, LayoutSwitchTarget, SizeChange, Transform, WorkspaceReferenceArg};
|
||||||
use regex::Regex;
|
|
||||||
use smithay::backend::renderer::Color32F;
|
use smithay::backend::renderer::Color32F;
|
||||||
use smithay::input::keyboard::keysyms::KEY_NoSymbol;
|
use smithay::input::keyboard::keysyms::KEY_NoSymbol;
|
||||||
use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE};
|
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.]);
|
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)]
|
#[derive(knuffel::Decode, Debug, PartialEq)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[knuffel(child, default)]
|
#[knuffel(child, default)]
|
||||||
@@ -1002,13 +1004,12 @@ pub struct WindowRule {
|
|||||||
pub variable_refresh_rate: Option<bool>,
|
pub variable_refresh_rate: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remember to update the PartialEq impl when adding fields!
|
#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)]
|
||||||
#[derive(knuffel::Decode, Debug, Default, Clone)]
|
|
||||||
pub struct Match {
|
pub struct Match {
|
||||||
#[knuffel(property, str)]
|
#[knuffel(property, str)]
|
||||||
pub app_id: Option<Regex>,
|
pub app_id: Option<RegexEq>,
|
||||||
#[knuffel(property, str)]
|
#[knuffel(property, str)]
|
||||||
pub title: Option<Regex>,
|
pub title: Option<RegexEq>,
|
||||||
#[knuffel(property)]
|
#[knuffel(property)]
|
||||||
pub is_active: Option<bool>,
|
pub is_active: Option<bool>,
|
||||||
#[knuffel(property)]
|
#[knuffel(property)]
|
||||||
@@ -1019,17 +1020,6 @@ pub struct Match {
|
|||||||
pub at_startup: Option<bool>,
|
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)]
|
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||||
pub struct CornerRadius {
|
pub struct CornerRadius {
|
||||||
pub top_left: f32,
|
pub top_left: f32,
|
||||||
@@ -3361,7 +3351,7 @@ mod tests {
|
|||||||
]),
|
]),
|
||||||
window_rules: vec![WindowRule {
|
window_rules: vec![WindowRule {
|
||||||
matches: vec![Match {
|
matches: vec![Match {
|
||||||
app_id: Some(Regex::new(".*alacritty").unwrap()),
|
app_id: Some(RegexEq::from_str(".*alacritty").unwrap()),
|
||||||
title: None,
|
title: None,
|
||||||
is_active: None,
|
is_active: None,
|
||||||
is_focused: None,
|
is_focused: None,
|
||||||
@@ -3371,7 +3361,7 @@ mod tests {
|
|||||||
excludes: vec![
|
excludes: vec![
|
||||||
Match {
|
Match {
|
||||||
app_id: None,
|
app_id: None,
|
||||||
title: Some(Regex::new("~").unwrap()),
|
title: Some(RegexEq::from_str("~").unwrap()),
|
||||||
is_active: None,
|
is_active: None,
|
||||||
is_focused: None,
|
is_focused: None,
|
||||||
is_active_in_column: None,
|
is_active_in_column: None,
|
||||||
|
|||||||
@@ -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
@@ -262,7 +262,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m:
|
|||||||
let Some(app_id) = &role.app_id else {
|
let Some(app_id) = &role.app_id else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
if !app_id_re.is_match(app_id) {
|
if !app_id_re.0.is_match(app_id) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m:
|
|||||||
let Some(title) = &role.title else {
|
let Some(title) = &role.title else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
if !title_re.is_match(title) {
|
if !title_re.0.is_match(title) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user