From e739ce8171705dd0b87c317cc0f4509b7f16f6df Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sat, 20 Sep 2025 12:57:41 +0300 Subject: config: Add merge!() macros to reduce boilerplate --- niri-config/src/appearance.rs | 112 +++++++++++------------------------------- niri-config/src/lib.rs | 3 ++ niri-config/src/macros.rs | 37 ++++++++++++++ niri-config/src/utils.rs | 6 +++ 4 files changed, 74 insertions(+), 84 deletions(-) create mode 100644 niri-config/src/macros.rs diff --git a/niri-config/src/appearance.rs b/niri-config/src/appearance.rs index cc698ef6..5785d5c7 100644 --- a/niri-config/src/appearance.rs +++ b/niri-config/src/appearance.rs @@ -565,41 +565,17 @@ pub struct TabIndicatorRule { } impl MergeWith for BorderRule { - fn merge_with(&mut self, other: &Self) { - if other.off { - self.off = true; - self.on = false; - } + fn merge_with(&mut self, part: &Self) { + merge_on_off!((self, part)); - if other.on { - self.off = false; - self.on = true; - } + merge_clone_opt!((self, part), width); - if let Some(x) = other.width { - self.width = Some(x); - } - if let Some(x) = other.active_color { - self.active_color = Some(x); - self.active_gradient = None; - } - if let Some(x) = other.inactive_color { - self.inactive_color = Some(x); - self.inactive_gradient = None; - } - if let Some(x) = other.urgent_color { - self.urgent_color = Some(x); - self.urgent_gradient = None; - } - if let Some(x) = other.active_gradient { - self.active_gradient = Some(x); - } - if let Some(x) = other.inactive_gradient { - self.inactive_gradient = Some(x); - } - if let Some(x) = other.urgent_gradient { - self.urgent_gradient = Some(x); - } + merge_color_gradient_opt!( + (self, part), + (active_color, active_gradient), + (inactive_color, inactive_gradient), + (urgent_color, urgent_gradient), + ); } } @@ -640,35 +616,18 @@ impl BorderRule { } impl MergeWith for ShadowRule { - fn merge_with(&mut self, other: &Self) { - if other.off { - self.off = true; - self.on = false; - } - - if other.on { - self.off = false; - self.on = true; - } - - if let Some(x) = other.offset { - self.offset = Some(x); - } - if let Some(x) = other.softness { - self.softness = Some(x); - } - if let Some(x) = other.spread { - self.spread = Some(x); - } - if let Some(x) = other.draw_behind_window { - self.draw_behind_window = Some(x); - } - if let Some(x) = other.color { - self.color = Some(x); - } - if let Some(x) = other.inactive_color { - self.inactive_color = Some(x); - } + fn merge_with(&mut self, part: &Self) { + merge_on_off!((self, part)); + + merge_clone_opt!( + (self, part), + offset, + softness, + spread, + draw_behind_window, + color, + inactive_color, + ); } } @@ -703,28 +662,13 @@ impl ShadowRule { } impl MergeWith for TabIndicatorRule { - fn merge_with(&mut self, other: &Self) { - if let Some(x) = other.active_color { - self.active_color = Some(x); - self.active_gradient = None; - } - if let Some(x) = other.inactive_color { - self.inactive_color = Some(x); - self.inactive_gradient = None; - } - if let Some(x) = other.urgent_color { - self.urgent_color = Some(x); - self.urgent_gradient = None; - } - if let Some(x) = other.active_gradient { - self.active_gradient = Some(x); - } - if let Some(x) = other.inactive_gradient { - self.inactive_gradient = Some(x); - } - if let Some(x) = other.urgent_gradient { - self.urgent_gradient = Some(x); - } + fn merge_with(&mut self, part: &Self) { + merge_color_gradient_opt!( + (self, part), + (active_color, active_gradient), + (inactive_color, inactive_gradient), + (urgent_color, urgent_gradient), + ); } } diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index fbbe8aba..575be90b 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -8,6 +8,9 @@ use std::path::{Path, PathBuf}; use miette::{Context as _, IntoDiagnostic as _}; +#[macro_use] +pub mod macros; + pub mod animations; pub mod appearance; pub mod binds; diff --git a/niri-config/src/macros.rs b/niri-config/src/macros.rs new file mode 100644 index 00000000..380111e2 --- /dev/null +++ b/niri-config/src/macros.rs @@ -0,0 +1,37 @@ +macro_rules! merge_clone_opt { + (($self:expr, $part:expr), $($field:ident),+ $(,)*) => { + $( + if $part.$field.is_some() { + $self.$field.clone_from(&$part.$field); + } + )+ + }; +} + +macro_rules! merge_color_gradient_opt { + (($self:expr, $part:expr), $(($color:ident, $gradient:ident)),+ $(,)*) => { + $( + if let Some(x) = $part.$color { + $self.$color = Some(x); + $self.$gradient = None; + } + if let Some(x) = $part.$gradient { + $self.$gradient = Some(x); + } + )+ + }; +} + +macro_rules! merge_on_off { + (($self:expr, $part:expr)) => { + if $part.off { + $self.off = true; + $self.on = false; + } + + if $part.on { + $self.off = false; + $self.on = true; + } + }; +} diff --git a/niri-config/src/utils.rs b/niri-config/src/utils.rs index e2a24901..e41d7b90 100644 --- a/niri-config/src/utils.rs +++ b/niri-config/src/utils.rs @@ -51,6 +51,12 @@ impl FromStr for Percent { } } +impl MergeWith> for f64 { + fn merge_with(&mut self, part: &FloatOrInt) { + *self = part.0; + } +} + impl knuffel::DecodeScalar for FloatOrInt { -- cgit