use modelcards::assets::config::get_default;
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
use std::env;
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Input {
pub data: String,
pub schema: String,
pub validate: bool,
}
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Output {
pub target: String,
pub template: String,
pub validate: bool,
}
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Settings {
pub project_dir: String,
pub verbose: bool,
pub force: bool,
pub input: Input,
pub output: Output,
}
impl Settings {
pub fn new(config_name: &str) -> Result<Self, ConfigError> {
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
let s = Config::builder()
.add_source(File::from_str(get_default(), config::FileFormat::Toml))
.add_source(
File::with_name(&run_mode.to_string())
.required(false),
)
.add_source(File::with_name(config_name).required(false))
.add_source(Environment::with_prefix("mc"))
.build()?;
log::debug!("verbose: {:?}", s.get_bool("verbose"));
log::debug!("project_dir: {:?}", s.get::<String>("project_dir"));
s.try_deserialize()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_settings() {
let settings = Settings::new("config").expect("Could not load settings");
assert_eq!(settings.verbose, true);
}
}