use crate::OrthoResult;
use figment::{
Figment,
providers::{Format, Toml},
};
#[cfg(feature = "json5")]
use figment_json5::Json5;
use std::path::Path;
use super::error::file_error;
#[cfg(feature = "yaml")]
use super::yaml::SaphyrYaml;
pub(super) fn parse_config_by_format(path: &Path, data: &str) -> OrthoResult<Figment> {
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase);
let figment = match ext.as_deref() {
Some("json" | "json5") => {
#[cfg(feature = "json5")]
{
Figment::from(Json5::string(data))
}
#[cfg(not(feature = "json5"))]
{
return Err(file_error(
path,
std::io::Error::other(
"json5 feature disabled: enable the 'json5' feature to support this file format",
),
));
}
}
Some("yaml" | "yml") => {
#[cfg(feature = "yaml")]
{
let utf8_path = camino::Utf8PathBuf::from_path_buf(path.to_path_buf())
.unwrap_or_else(|pathbuf| {
camino::Utf8PathBuf::from(pathbuf.to_string_lossy().into_owned())
});
Figment::from(SaphyrYaml::string(utf8_path, data.to_owned()))
}
#[cfg(not(feature = "yaml"))]
{
return Err(file_error(
path,
std::io::Error::other(
"yaml feature disabled: enable the 'yaml' feature to support this file format",
),
));
}
}
_ => {
toml::from_str::<toml::Value>(data).map_err(|e| file_error(path, e))?;
Figment::from(Toml::string(data))
}
};
Ok(figment)
}