use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Config {
}
pub fn config_path() -> PathBuf {
dirs_next().join("config.json")
}
fn dirs_next() -> PathBuf {
let base = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
PathBuf::from(base).join(".config/hygiea")
}
pub fn load() -> Result<Config> {
let path = config_path();
if !path.exists() {
return Ok(Config::default());
}
let content = std::fs::read_to_string(&path)
.with_context(|| format!("cannot read {}", path.display()))?;
serde_json::from_str(&content)
.with_context(|| format!("invalid JSON in {}", path.display()))
}
pub fn save(cfg: &Config) -> Result<()> {
let path = config_path();
let dir = path.parent().with_context(|| format!("invalid config path: {}", path.display()))?;
std::fs::create_dir_all(dir)
.with_context(|| format!("cannot create {}", dir.display()))?;
let json = serde_json::to_string_pretty(cfg)?;
std::fs::write(&path, json)
.with_context(|| format!("cannot write {}", path.display()))
}