mirror of
https://github.com/kristoferssolo/traxor.git
synced 2026-03-22 00:36:23 +00:00
refactor: use derive macro
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use crate::merge_fields;
|
||||
use crate::merge::Merge;
|
||||
use derive_macro::Merge;
|
||||
use ratatui::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Deserialize, Serialize, Merge)]
|
||||
pub struct ColorsConfig {
|
||||
pub highlight_background: Option<String>,
|
||||
pub highlight_foreground: Option<String>,
|
||||
@@ -32,18 +33,6 @@ impl ColorsConfig {
|
||||
None => Color::Reset,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
merge_fields!(
|
||||
self,
|
||||
other,
|
||||
highlight_background,
|
||||
highlight_foreground,
|
||||
warning_foreground,
|
||||
info_foreground,
|
||||
error_foreground
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ColorsConfig {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::merge::Merge;
|
||||
use derive_macro::Merge;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::merge_fields;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Deserialize, Serialize, Merge)]
|
||||
pub struct KeybindsConfig {
|
||||
pub quit: Option<String>,
|
||||
pub next_tab: Option<String>,
|
||||
@@ -19,29 +20,6 @@ pub struct KeybindsConfig {
|
||||
pub toggle_help: Option<String>,
|
||||
}
|
||||
|
||||
impl KeybindsConfig {
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KeybindsConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
||||
@@ -1,24 +1,15 @@
|
||||
mod colors;
|
||||
mod keybinds;
|
||||
|
||||
use crate::merge::Merge;
|
||||
use color_eyre::Result;
|
||||
use colors::ColorsConfig;
|
||||
use derive_macro::Merge;
|
||||
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)]
|
||||
#[derive(Debug, Default, Deserialize, Serialize, Merge)]
|
||||
pub struct Config {
|
||||
pub keybinds: KeybindsConfig,
|
||||
pub colors: ColorsConfig,
|
||||
@@ -64,9 +55,4 @@ impl Config {
|
||||
});
|
||||
Ok(config_dir.join("traxor").join("config.toml"))
|
||||
}
|
||||
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
self.keybinds.merge(other.keybinds);
|
||||
self.colors.merge(other.colors);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user