use serde::Deserialize;
use std::path::{Path, PathBuf};
#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub defaults: Defaults,
#[serde(default)]
pub strip: Strip,
}
#[derive(Debug, Default, Deserialize)]
pub struct Strip {
#[serde(default)]
pub patterns: Vec<String>,
}
#[derive(Debug, Default, Deserialize)]
pub struct Defaults {
#[serde(default)]
pub ast: bool,
#[serde(default)]
pub strip_docs: bool,
#[serde(default)]
pub smart: bool,
pub budget: Option<usize>,
}
impl Config {
pub fn load() -> Self {
if let Some(cfg) = Self::load_from(Path::new(".tersify.toml")) {
return cfg;
}
if let Ok(home) = std::env::var("HOME") {
let global = PathBuf::from(home).join(".tersify").join("config.toml");
if let Some(cfg) = Self::load_from(&global) {
return cfg;
}
}
Self::default()
}
fn load_from(path: &Path) -> Option<Self> {
let content = std::fs::read_to_string(path).ok()?;
toml::from_str(&content).ok()
}
}