use anyhow::Result;
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use crate::cli::ThemeArg;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub theme: ThemeArg,
}
impl Default for Config {
fn default() -> Self {
Self {
theme: ThemeArg::Auto,
}
}
}
impl Config {
pub fn load() -> Self {
match Self::load_internal() {
Ok(config) => config,
Err(e) => {
log::debug!("Failed to load config, using defaults: {}", e);
Self::default()
}
}
}
fn load_internal() -> Result<Self> {
let path = Self::config_path()?;
if !path.exists() {
return Ok(Self::default());
}
let content = fs::read_to_string(path)?;
let config = serde_json::from_str(&content)?;
Ok(config)
}
pub fn save(&self) -> Result<()> {
let path = Self::config_path()?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let content = serde_json::to_string_pretty(self)?;
fs::write(path, content)?;
Ok(())
}
fn config_path() -> Result<PathBuf> {
let project_dirs = ProjectDirs::from("com", "rustdupe", "rustdupe")
.ok_or_else(|| anyhow::anyhow!("Failed to determine project directories"))?;
Ok(project_dirs.config_dir().join("config.json"))
}
}