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 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
);
}
}

View File

@ -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
);
}
}

View File

@ -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,