use config::builder::DefaultState;
use config::ConfigBuilder;
use config::ConfigError;
use config::File;
use std::path::PathBuf;
use tracing::warn;
pub fn global_config_path() -> Option<PathBuf> {
std::env::var("HOME").ok().map(|home| {
PathBuf::from(home)
.join(".config")
.join("meld")
.join("config.toml")
})
}
pub fn add_to_builder(
mut builder: ConfigBuilder<DefaultState>,
) -> Result<ConfigBuilder<DefaultState>, ConfigError> {
if let Some(xdg_config_path) = global_config_path() {
if xdg_config_path.exists() {
let canonical_xdg_path = xdg_config_path
.canonicalize()
.unwrap_or_else(|_| xdg_config_path.clone());
builder = builder
.add_source(File::with_name(canonical_xdg_path.to_str().unwrap()).required(false));
} else {
warn!(
config_path = %xdg_config_path.display(),
"Default configuration file not found at ~/.config/meld/config.toml. \
Consider creating it for user-level defaults."
);
}
}
Ok(builder)
}