use std::collections::HashMap;
use super::cli::{CliArgs, Commands};
use super::config_file::ConfigFile;
use super::errors::ConfigError;
use super::global_config::GlobalConfig;
use super::loader::ConfigLoader;
use crate::codegen::GeneratorType;
#[derive(Debug, Clone)]
pub struct Config {
pub input: String,
pub global: GlobalConfig,
pub generators: HashMap<GeneratorType, toml::value::Table>,
}
impl Config {
pub fn load(cli_args: &CliArgs) -> Result<Self, ConfigError> {
let config_file = match &cli_args.command {
Commands::Generate {
config: Some(path), ..
} => Some(ConfigLoader::load_from_file(path)?),
Commands::Generate { .. } => ConfigLoader::discover_config_file()
.and_then(|path| ConfigLoader::load_from_file(&path).ok()),
};
Self::merge(config_file.as_ref(), cli_args)
}
fn merge(config_file: Option<&ConfigFile>, cli_args: &CliArgs) -> Result<Self, ConfigError> {
let mut config = Config {
input: String::new(),
global: GlobalConfig::default(),
generators: HashMap::new(),
};
if let Some(config_file) = config_file {
config.merge_file(config_file);
}
let (input, cli_global) = match &cli_args.command {
Commands::Generate { input, global, .. } => (input, global),
};
if input.is_empty() {
return Err(ConfigError::Validation(
"Input is required and cannot be empty".to_string(),
));
}
let generator_overrides = cli_args.command.parse_generator_overrides()?;
config.input = input.clone();
config.merge_global(cli_global);
config.merge_generator_configs(&generator_overrides);
Ok(config)
}
fn merge_file(&mut self, config_file: &ConfigFile) {
self.merge_global(&config_file.global);
self.merge_generator_configs(&config_file.generators);
}
fn merge_global(&mut self, global: &GlobalConfig) {
if global.output.is_some() {
self.global.output = global.output.clone();
}
if global.generators.is_some() {
self.global.generators = global.generators.clone();
}
}
fn merge_generator_configs(
&mut self,
generator_configs: &HashMap<GeneratorType, toml::value::Table>,
) {
for (generator, table_rhs) in generator_configs {
if let Some(table_lhs) = self.generators.get_mut(generator) {
for (key, value) in table_rhs.iter() {
table_lhs.insert(key.clone(), value.clone());
}
} else {
self.generators.insert(*generator, table_rhs.clone());
}
}
}
}