use cdumay_core::ErrorConverter;
pub struct TomlManager {
path: String,
}
impl crate::Manager for TomlManager {
fn new(path: String) -> TomlManager {
TomlManager { path }
}
fn path(&self) -> String {
self.path.clone()
}
fn read<R: std::io::Read, C: serde::de::DeserializeOwned>(
&self,
mut reader: R,
context: &std::collections::BTreeMap<String, serde_value::Value>,
) -> cdumay_core::Result<C> {
let mut buffer = String::new();
reader.read_to_string(&mut buffer).map_err(|err| {
crate::ConfigurationFileError::new()
.with_message(format!("Failed to write TOML file: {}", err))
.with_details({
let mut ctx = context.clone();
ctx.insert("path".to_string(), serde_value::Value::String(self.path()));
ctx
})
})?;
Self::read_str(&buffer, context)
}
fn write<D: serde::Serialize, W: std::io::Write>(
&self,
mut writer: W,
data: D,
context: &std::collections::BTreeMap<String, serde_value::Value>,
) -> cdumay_core::Result<()> {
let mut ctx = context.clone();
ctx.insert("path".to_string(), serde_value::Value::String(self.path()));
let content = cdumay_toml::convert_serialize_result!(toml::to_string_pretty(&data), ctx.clone())?;
Ok(writer.write_all(content.as_bytes()).map_err(|err| {
crate::ConfigurationFileError::new()
.with_message(format!("Failed to write TOML file: {}", err))
.with_details(ctx)
})?)
}
fn read_str<C: serde::de::DeserializeOwned>(
content: &str,
context: &std::collections::BTreeMap<String, serde_value::Value>,
) -> cdumay_core::Result<C> {
cdumay_toml::convert_deserialize_result!(toml::from_str(content), context.clone())
}
}