use serde_json::Value as Json;
use toml::Value as Toml;
use crate::python_sources::_helpers::json_values::toml_value;
use crate::python_sources::models::PythonSource;
pub fn declared_sources(python: &[PythonSource]) -> Result<Toml, Vec<String>> {
let mut sources = toml::Table::new();
let mut problems: Vec<String> = Vec::new();
for source in python {
let place = format!("Python source {} ({})", source.name, source.module);
match toml_value(&place, &Json::Object(source.settings.clone())) {
Ok(Toml::Table(mut settings)) => {
settings.insert("kind".to_owned(), Toml::String("python".to_owned()));
sources.insert(source.name.clone(), Toml::Table(settings));
}
Ok(_) => problems.push(format!("{place}: settings are not a mapping")),
Err(problem) => problems.push(problem),
}
}
let mut value = toml::Table::new();
value.insert("sources".to_owned(), Toml::Table(sources));
match problems.is_empty() {
true => Ok(Toml::Table(value)),
false => Err(problems),
}
}