refactor(clippy): fix clippy warning

This commit is contained in:
2025-07-10 19:54:48 +03:00
parent b563c7ea24
commit ae0fc2bbf5
25 changed files with 499 additions and 295 deletions

34
src/config/color.rs Normal file
View File

@@ -0,0 +1,34 @@
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>,
}
#[derive(Debug, Clone, FromFile)]
pub struct ColorConfig {
pub highlight_background: String,
pub highlight_foreground: String,
pub header_foreground: String,
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,59 +0,0 @@
use derive_macro::FromFile;
use merge::{Merge, option::overwrite_none};
use ratatui::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, Merge)]
pub struct ColorsConfigFile {
#[merge(strategy = overwrite_none)]
pub highlight_background: Option<String>,
#[merge(strategy = overwrite_none)]
pub highlight_foreground: Option<String>,
#[merge(strategy = overwrite_none)]
pub warning_foreground: Option<String>,
#[merge(strategy = overwrite_none)]
pub info_foreground: Option<String>,
#[merge(strategy = overwrite_none)]
pub error_foreground: Option<String>,
}
#[derive(Debug, Clone, FromFile)]
pub struct ColorsConfig {
pub highlight_background: String,
pub highlight_foreground: String,
pub warning_foreground: String,
pub info_foreground: String,
pub error_foreground: String,
}
impl ColorsConfig {
pub fn get_color(&self, color_name: &str) -> Color {
match color_name.to_lowercase().as_str() {
"black" => Color::Black,
"blue" => Color::Blue,
"cyan" => Color::Cyan,
"darkgray" => Color::DarkGray,
"gray" => Color::Gray,
"green" => Color::Green,
"lightgreen" => Color::LightGreen,
"lightred" => Color::LightRed,
"magenta" => Color::Magenta,
"red" => Color::Red,
"white" => Color::White,
"yellow" => Color::Yellow,
_ => Color::Reset, // Default to reset, if color name is not recognized
}
}
}
impl Default for ColorsConfigFile {
fn default() -> Self {
Self {
highlight_background: Some("magenta".to_string()),
highlight_foreground: Some("black".to_string()),
warning_foreground: Some("yellow".to_string()),
info_foreground: Some("blue".to_string()),
error_foreground: Some("red".to_string()),
}
}
}

View File

@@ -1,14 +1,20 @@
mod colors;
mod keybinds;
mod log;
pub mod color;
pub mod keybinds;
pub mod log;
use color_eyre::Result;
use colors::{ColorsConfig, ColorsConfigFile};
use color::{ColorConfig, ColorConfigFile};
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 std::path::PathBuf;
use std::{
fs::read_to_string,
path::{Path, PathBuf},
};
use tracing::{debug, info, warn};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Merge)]
@@ -16,7 +22,7 @@ pub struct ConfigFile {
#[merge(strategy = overwrite_none)]
pub keybinds: Option<KeybindsConfigFile>,
#[merge(strategy = overwrite_none)]
pub colors: Option<ColorsConfigFile>,
pub colors: Option<ColorConfigFile>,
#[merge(strategy = overwrite_none)]
pub log: Option<LogConfigFile>,
}
@@ -24,53 +30,29 @@ pub struct ConfigFile {
#[derive(Debug, Clone)]
pub struct Config {
pub keybinds: KeybindsConfig,
pub colors: ColorsConfig,
pub colors: ColorConfig,
pub log: LogConfig,
}
impl Config {
/// # Errors
///
/// TODO: add error types
#[tracing::instrument(name = "Loading configuration")]
pub fn load() -> Result<Self> {
let mut config = ConfigFile::default();
let mut cfg_file = ConfigFile::default();
// Load system-wide config
let system_config_path = PathBuf::from("/etc/xdg/traxor/config.toml");
if system_config_path.exists() {
info!("Loading system-wide config from: {:?}", system_config_path);
let config_str = std::fs::read_to_string(&system_config_path)?;
let system_config = toml::from_str::<ConfigFile>(&config_str)?;
config.merge(system_config);
info!("Successfully loaded system-wide config.");
} else {
warn!("System-wide config not found at: {:?}", system_config_path);
}
let candidates = [
("system-wide", PathBuf::from("/etc/xdg/traxor/config.toml")),
("user-specific", get_config_path()?),
];
// Load user-specific config
let user_config_path = Self::get_config_path()?;
if user_config_path.exists() {
info!("Loading user-specific config from: {:?}", user_config_path);
let config_str = std::fs::read_to_string(&user_config_path)?;
let user_config = toml::from_str::<ConfigFile>(&config_str)?;
config.merge(user_config);
info!("Successfully loaded user-specific config.");
} else {
warn!("User-specific config not found at: {:?}", user_config_path);
for (label, path) in &candidates {
merge_config(&mut cfg_file, label, path)?;
}
debug!("Configuration loaded successfully.");
Ok(config.into())
}
#[tracing::instrument(name = "Getting config path")]
fn get_config_path() -> Result<PathBuf> {
let config_dir = std::env::var("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| {
dirs::home_dir()
.unwrap_or_else(|| panic!("Could not find home directory"))
.join(".config")
});
Ok(config_dir.join("traxor").join("config.toml"))
Ok(cfg_file.into())
}
}
@@ -83,3 +65,47 @@ impl From<ConfigFile> for Config {
}
}
}
#[tracing::instrument(name = "Getting config path")]
fn get_config_path() -> Result<PathBuf> {
let config_dir =
dirs::config_dir().context("Could not determine user configuration directory")?;
Ok(config_dir.join("traxor").join("config.toml"))
}
#[tracing::instrument(name = "Merging config", skip(cfg_file, path))]
fn merge_config(cfg_file: &mut ConfigFile, label: &str, path: &Path) -> Result<()> {
if !exists_and_log(label, path) {
return Ok(());
}
info!("Loading {} config from: {:?}", label, path);
let s = read_config_str(label, path)?;
let other = parse_config_toml(label, &s)?;
cfg_file.merge(other);
info!("Successfully loaded {} config.", label);
Ok(())
}
fn exists_and_log(label: &str, path: &Path) -> bool {
if !path.exists() {
warn!("{} config not found at: {:?}", label, path);
return false;
}
true
}
fn read_config_str(label: &str, path: &Path) -> Result<String> {
read_to_string(path).with_context(|| {
format!(
"Failed to read {label} config file at {}",
path.to_string_lossy()
)
})
}
fn parse_config_toml(label: &str, s: &str) -> Result<ConfigFile> {
toml::from_str::<ConfigFile>(s)
.with_context(|| format!("Failed to parse TOML in {label} config"))
}