diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2025-09-20 12:57:41 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2025-10-02 09:33:08 +0300 |
| commit | e739ce8171705dd0b87c317cc0f4509b7f16f6df (patch) | |
| tree | b4cd995410c63da9a1b9453cd5b56ee890242d0e | |
| parent | a2727ba2c9cfb22e4184235c6dfd8d8819328169 (diff) | |
| download | niri-e739ce8171705dd0b87c317cc0f4509b7f16f6df.tar.gz niri-e739ce8171705dd0b87c317cc0f4509b7f16f6df.tar.bz2 niri-e739ce8171705dd0b87c317cc0f4509b7f16f6df.zip | |
config: Add merge!() macros to reduce boilerplate
| -rw-r--r-- | niri-config/src/appearance.rs | 112 | ||||
| -rw-r--r-- | niri-config/src/lib.rs | 3 | ||||
| -rw-r--r-- | niri-config/src/macros.rs | 37 | ||||
| -rw-r--r-- | niri-config/src/utils.rs | 6 |
4 files changed, 74 insertions, 84 deletions
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<Self> 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<Self> 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<Self> 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<const MIN: i32, const MAX: i32> MergeWith<FloatOrInt<MIN, MAX>> for f64 { + fn merge_with(&mut self, part: &FloatOrInt<MIN, MAX>) { + *self = part.0; + } +} + impl<S: knuffel::traits::ErrorSpan, const MIN: i32, const MAX: i32> knuffel::DecodeScalar<S> for FloatOrInt<MIN, MAX> { |
