From 323d19668918e58e5d406c234e4455f8edde8432 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 7 Jul 2025 20:38:40 +0300 Subject: [PATCH] refactor(config): remove repetitive code --- src/config/colors.rs | 25 +++++++---------- src/config/keybinds.rs | 61 +++++++++++++----------------------------- src/config/mod.rs | 11 ++++++++ 3 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/config/colors.rs b/src/config/colors.rs index 894d7ff..c49ffab 100644 --- a/src/config/colors.rs +++ b/src/config/colors.rs @@ -1,3 +1,4 @@ +use crate::merge_fields; use ratatui::prelude::*; use serde::{Deserialize, Serialize}; @@ -33,21 +34,15 @@ impl ColorsConfig { } pub fn merge(&mut self, other: Self) { - if let Some(highlight_background) = other.highlight_background { - self.highlight_background = Some(highlight_background); - } - if let Some(highlight_foreground) = other.highlight_foreground { - self.highlight_foreground = Some(highlight_foreground); - } - if let Some(warning_foreground) = other.warning_foreground { - self.warning_foreground = Some(warning_foreground); - } - if let Some(info_foreground) = other.info_foreground { - self.info_foreground = Some(info_foreground); - } - if let Some(error_foreground) = other.error_foreground { - self.error_foreground = Some(error_foreground); - } + merge_fields!( + self, + other, + highlight_background, + highlight_foreground, + warning_foreground, + info_foreground, + error_foreground + ); } } diff --git a/src/config/keybinds.rs b/src/config/keybinds.rs index 47b7619..fa37a24 100644 --- a/src/config/keybinds.rs +++ b/src/config/keybinds.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use crate::merge_fields; #[derive(Debug, Deserialize, Serialize)] pub struct KeybindsConfig { @@ -20,48 +21,24 @@ pub struct KeybindsConfig { impl KeybindsConfig { pub fn merge(&mut self, other: Self) { - if let Some(quit) = other.quit { - self.quit = Some(quit); - } - if let Some(next_tab) = other.next_tab { - self.next_tab = Some(next_tab); - } - if let Some(prev_tab) = other.prev_tab { - self.prev_tab = Some(prev_tab); - } - if let Some(next_torrent) = other.next_torrent { - self.next_torrent = Some(next_torrent); - } - if let Some(prev_torrent) = other.prev_torrent { - self.prev_torrent = Some(prev_torrent); - } - if let Some(switch_tab_1) = other.switch_tab_1 { - self.switch_tab_1 = Some(switch_tab_1); - } - if let Some(switch_tab_2) = other.switch_tab_2 { - self.switch_tab_2 = Some(switch_tab_2); - } - if let Some(switch_tab_3) = other.switch_tab_3 { - self.switch_tab_3 = Some(switch_tab_3); - } - if let Some(toggle_torrent) = other.toggle_torrent { - self.toggle_torrent = Some(toggle_torrent); - } - if let Some(toggle_all) = other.toggle_all { - self.toggle_all = Some(toggle_all); - } - if let Some(delete) = other.delete { - self.delete = Some(delete); - } - if let Some(delete_force) = other.delete_force { - self.delete_force = Some(delete_force); - } - if let Some(select) = other.select { - self.select = Some(select); - } - if let Some(toggle_help) = other.toggle_help { - self.toggle_help = Some(toggle_help); - } + merge_fields!( + self, + other, + quit, + next_tab, + prev_tab, + next_torrent, + prev_torrent, + switch_tab_1, + switch_tab_2, + switch_tab_3, + toggle_torrent, + toggle_all, + delete, + delete_force, + select, + toggle_help + ); } } diff --git a/src/config/mod.rs b/src/config/mod.rs index 2431c04..b0c893b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -7,6 +7,17 @@ use keybinds::KeybindsConfig; use serde::{Deserialize, Serialize}; use std::path::PathBuf; +#[macro_export] +macro_rules! merge_fields { + ($self:ident, $other:ident, $($field:ident),*) => { + $( + if let Some($field) = $other.$field { + $self.$field = Some($field); + } + )* + }; +} + #[derive(Debug, Default, Deserialize, Serialize)] pub struct Config { pub keybinds: KeybindsConfig,