use crate::error::Error;
use crate::reader::Reader;
use crate::source::Format;
use crate::value::Value;
#[derive(Debug)]
pub(crate) struct ConfigRs;
impl Reader for ConfigRs {
fn name(&self) -> &str {
"config-rs"
}
fn reads(&self, format: Format) -> bool {
dialect(format).is_some()
}
fn parse(&self, text: &str, format: Format) -> Result<Value, Error> {
let Some(dialect) = dialect(format) else {
return Err(crate::reader::unread(format));
};
let source = config_rs::File::from_str(text, dialect);
let collected =
config_rs::Source::collect(&source).map_err(|error| super::error::unparsed(&error))?;
Ok(Value::Table(
collected
.into_iter()
.map(|(key, value)| (key, super::from_config_rs(&value)))
.collect(),
))
}
}
#[allow(unreachable_patterns, unused_variables)]
fn dialect(format: Format) -> Option<config_rs::FileFormat> {
match format {
#[cfg(feature = "json")]
Format::Json => Some(config_rs::FileFormat::Json),
#[cfg(feature = "toml")]
Format::Toml => Some(config_rs::FileFormat::Toml),
#[cfg(feature = "yaml")]
Format::Yaml => Some(config_rs::FileFormat::Yaml),
#[cfg(feature = "ini")]
Format::Ini => Some(config_rs::FileFormat::Ini),
#[cfg(feature = "ron")]
Format::Ron => Some(config_rs::FileFormat::Ron),
#[cfg(feature = "json5")]
Format::Json5 => Some(config_rs::FileFormat::Json5),
_ => None,
}
}