feat: use filecaster crate

This commit is contained in:
2025-07-15 15:57:26 +03:00
parent eff0fe20e4
commit 46486a3409
11 changed files with 87 additions and 256 deletions

View File

@@ -1,34 +1,13 @@
use derive_macro::FromFile;
use merge::{Merge, option::overwrite_none};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, Merge)]
pub struct ColorConfigFile {
#[merge(strategy = overwrite_none)]
pub highlight_background: Option<String>,
#[merge(strategy = overwrite_none)]
pub highlight_foreground: Option<String>,
#[merge(strategy = overwrite_none)]
pub header_foreground: Option<String>,
#[merge(strategy = overwrite_none)]
pub info_foreground: Option<String>,
}
use filecaster::FromFile;
#[derive(Debug, Clone, FromFile)]
pub struct ColorConfig {
#[from_file(default = "magenta")]
pub highlight_background: String,
#[from_file(default = "black")]
pub highlight_foreground: String,
#[from_file(default = "yellow")]
pub header_foreground: String,
#[from_file(default = "blue")]
pub info_foreground: String,
}
impl Default for ColorConfigFile {
fn default() -> Self {
Self {
highlight_background: Some("magenta".to_string()),
highlight_foreground: Some("black".to_string()),
header_foreground: Some("yellow".to_string()),
info_foreground: Some("blue".to_string()),
}
}
}

View File

@@ -1,78 +1,35 @@
use derive_macro::FromFile;
use merge::{Merge, option::overwrite_none};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, Merge)]
pub struct KeybindsConfigFile {
#[merge(strategy = overwrite_none)]
pub quit: Option<String>,
#[merge(strategy = overwrite_none)]
pub next_tab: Option<String>,
#[merge(strategy = overwrite_none)]
pub prev_tab: Option<String>,
#[merge(strategy = overwrite_none)]
pub next_torrent: Option<String>,
#[merge(strategy = overwrite_none)]
pub prev_torrent: Option<String>,
#[merge(strategy = overwrite_none)]
pub switch_tab_1: Option<String>,
#[merge(strategy = overwrite_none)]
pub switch_tab_2: Option<String>,
#[merge(strategy = overwrite_none)]
pub switch_tab_3: Option<String>,
#[merge(strategy = overwrite_none)]
pub toggle_torrent: Option<String>,
#[merge(strategy = overwrite_none)]
pub toggle_all: Option<String>,
#[merge(strategy = overwrite_none)]
pub delete: Option<String>,
#[merge(strategy = overwrite_none)]
pub delete_force: Option<String>,
#[merge(strategy = overwrite_none)]
pub select: Option<String>,
#[merge(strategy = overwrite_none)]
pub toggle_help: Option<String>,
#[merge(strategy = overwrite_none)]
pub move_torrent: Option<String>,
}
use filecaster::FromFile;
#[derive(Debug, Clone, FromFile)]
pub struct KeybindsConfig {
#[from_file(default = "q")]
pub quit: String,
#[from_file(default = "l")]
pub next_tab: String,
#[from_file(default = "h")]
pub prev_tab: String,
#[from_file(default = "j")]
pub next_torrent: String,
#[from_file(default = "k")]
pub prev_torrent: String,
#[from_file(default = "1")]
pub switch_tab_1: String,
#[from_file(default = "2")]
pub switch_tab_2: String,
#[from_file(default = "3")]
pub switch_tab_3: String,
#[from_file(default = "enter")]
pub toggle_torrent: String,
#[from_file(default = "a")]
pub toggle_all: String,
#[from_file(default = "d")]
pub delete: String,
#[from_file(default = "D")]
pub delete_force: String,
#[from_file(default = " ")]
pub select: String,
#[from_file(default = "?")]
pub toggle_help: String,
#[from_file(default = "m")]
pub move_torrent: String,
}
impl Default for KeybindsConfigFile {
fn default() -> Self {
Self {
quit: Some("q".to_string()),
next_tab: Some("l".to_string()),
prev_tab: Some("h".to_string()),
next_torrent: Some("j".to_string()),
prev_torrent: Some("k".to_string()),
switch_tab_1: Some("1".to_string()),
switch_tab_2: Some("2".to_string()),
switch_tab_3: Some("3".to_string()),
toggle_torrent: Some("enter".to_string()),
toggle_all: Some("a".to_string()),
delete: Some("d".to_string()),
delete_force: Some("D".to_string()),
select: Some(" ".to_string()),
toggle_help: Some("?".to_string()),
move_torrent: Some("m".to_string()),
}
}
}

View File

@@ -1,30 +1,11 @@
use derive_macro::FromFile;
use merge::{Merge, option::overwrite_none};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, Merge)]
pub struct LogConfigFile {
#[merge(strategy = overwrite_none)]
pub traxor: Option<String>,
#[merge(strategy = overwrite_none)]
pub ratatui: Option<String>,
#[merge(strategy = overwrite_none)]
pub transmission_rpc: Option<String>,
}
use filecaster::FromFile;
#[derive(Debug, Clone, FromFile)]
pub struct LogConfig {
#[from_file(default = "warn")]
pub traxor: String,
#[from_file(default = "warn")]
pub ratatui: String,
#[from_file(default = "warn")]
pub transmission_rpc: String,
}
impl Default for LogConfigFile {
fn default() -> Self {
Self {
traxor: Some("warn".to_string()),
ratatui: Some("warn".to_string()),
transmission_rpc: Some("warn".to_string()),
}
}
}

View File

@@ -2,32 +2,22 @@ pub mod color;
pub mod keybinds;
pub mod log;
use color::{ColorConfig, ColorConfigFile};
use color::ColorConfig;
use color_eyre::{
Result,
eyre::{Context, ContextCompat, Ok},
};
use keybinds::{KeybindsConfig, KeybindsConfigFile};
use log::{LogConfig, LogConfigFile};
use merge::{Merge, option::overwrite_none};
use serde::{Deserialize, Serialize};
use filecaster::FromFile;
use keybinds::KeybindsConfig;
use log::LogConfig;
use merge::Merge;
use std::{
fs::read_to_string,
path::{Path, PathBuf},
};
use tracing::{debug, info, warn};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Merge)]
pub struct ConfigFile {
#[merge(strategy = overwrite_none)]
pub keybinds: Option<KeybindsConfigFile>,
#[merge(strategy = overwrite_none)]
pub colors: Option<ColorConfigFile>,
#[merge(strategy = overwrite_none)]
pub log: Option<LogConfigFile>,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, FromFile)]
pub struct Config {
pub keybinds: KeybindsConfig,
pub colors: ColorConfig,
@@ -56,16 +46,6 @@ impl Config {
}
}
impl From<ConfigFile> for Config {
fn from(value: ConfigFile) -> Self {
Self {
keybinds: value.keybinds.into(),
colors: value.colors.into(),
log: value.log.into(),
}
}
}
#[tracing::instrument(name = "Getting config path")]
fn get_config_path() -> Result<PathBuf> {
let config_dir =