use figment::{
providers::{Format, Serialized, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
use std::{env, path::Path};
#[derive(Debug, Serialize, Deserialize)]
pub struct GlobalConfig {
pub ninja: String,
pub keep_build_dir: bool,
pub verbose: bool,
}
impl Default for GlobalConfig {
fn default() -> Self {
Self {
ninja: "ninja".to_string(),
keep_build_dir: false,
verbose: false,
}
}
}
pub(crate) fn config_path(name: &str) -> std::path::PathBuf {
let config_base = env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| {
let home = env::var("HOME").expect("$HOME not set");
home + "/.config"
});
let config_path = Path::new(&config_base).join(name).with_extension("toml");
log::info!("Loading config from {}", config_path.display());
config_path
}
pub(crate) fn load_config(name: &str) -> Figment {
let config_path = config_path(name);
Figment::from(Serialized::defaults(GlobalConfig::default()))
.merge(Toml::file(config_path))
}