refactor(config): remove repetitive code

This commit is contained in:
Kristofers Solo 2025-07-07 20:38:40 +03:00
parent 06fa7c003d
commit 323d196689
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
3 changed files with 40 additions and 57 deletions

View File

@ -1,3 +1,4 @@
use crate::merge_fields;
use ratatui::prelude::*; use ratatui::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -33,21 +34,15 @@ impl ColorsConfig {
} }
pub fn merge(&mut self, other: Self) { pub fn merge(&mut self, other: Self) {
if let Some(highlight_background) = other.highlight_background { merge_fields!(
self.highlight_background = Some(highlight_background); self,
} other,
if let Some(highlight_foreground) = other.highlight_foreground { highlight_background,
self.highlight_foreground = Some(highlight_foreground); highlight_foreground,
} warning_foreground,
if let Some(warning_foreground) = other.warning_foreground { info_foreground,
self.warning_foreground = Some(warning_foreground); error_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);
}
} }
} }

View File

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::merge_fields;
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct KeybindsConfig { pub struct KeybindsConfig {
@ -20,48 +21,24 @@ pub struct KeybindsConfig {
impl KeybindsConfig { impl KeybindsConfig {
pub fn merge(&mut self, other: Self) { pub fn merge(&mut self, other: Self) {
if let Some(quit) = other.quit { merge_fields!(
self.quit = Some(quit); self,
} other,
if let Some(next_tab) = other.next_tab { quit,
self.next_tab = Some(next_tab); next_tab,
} prev_tab,
if let Some(prev_tab) = other.prev_tab { next_torrent,
self.prev_tab = Some(prev_tab); prev_torrent,
} switch_tab_1,
if let Some(next_torrent) = other.next_torrent { switch_tab_2,
self.next_torrent = Some(next_torrent); switch_tab_3,
} toggle_torrent,
if let Some(prev_torrent) = other.prev_torrent { toggle_all,
self.prev_torrent = Some(prev_torrent); delete,
} delete_force,
if let Some(switch_tab_1) = other.switch_tab_1 { select,
self.switch_tab_1 = Some(switch_tab_1); toggle_help
} );
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);
}
} }
} }

View File

@ -7,6 +7,17 @@ use keybinds::KeybindsConfig;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::PathBuf; 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)] #[derive(Debug, Default, Deserialize, Serialize)]
pub struct Config { pub struct Config {
pub keybinds: KeybindsConfig, pub keybinds: KeybindsConfig,