use crate::config::{ConfigLoader, ConfigResolver};
use anyhow::Result;
use std::path::PathBuf;
pub struct Settings {
pub config_resolver: ConfigResolver,
}
impl Settings {
pub fn load<P: AsRef<std::path::Path>>(config_path: P) -> Result<Self> {
let config_path = config_path.as_ref().to_path_buf();
if config_path.is_file() {
let config_dir = config_path.parent().unwrap_or(&PathBuf::from(".")).to_path_buf();
let loader = ConfigLoader::new(&config_dir);
let config = loader.load_from_path(&config_path)?;
loader.validate_config(&config)?;
let config_resolver = ConfigResolver::new(Some(config), config_dir);
Ok(Self { config_resolver })
} else {
let loader = ConfigLoader::new(&config_path);
let config = loader.load()?;
if let Some(ref config_data) = config {
loader.validate_config(config_data)?;
}
let config_resolver = ConfigResolver::new(config, config_path);
Ok(Self { config_resolver })
}
}
pub fn load_from_current_dir() -> Result<Self> {
let loader = ConfigLoader::from_current_dir()?;
let config_dir = loader.search_dir.clone();
let config = loader.load()?;
if let Some(ref config_data) = config {
loader.validate_config(config_data)?;
}
let config_resolver = ConfigResolver::new(config, config_dir);
Ok(Self { config_resolver })
}
pub fn resolver(&self) -> &ConfigResolver {
&self.config_resolver
}
}