1use std::path::Path;
2
3use crate::{error::SourceError, value::ConfigValue};
4
5pub trait Format {
7 fn name(&self) -> &'static str;
8
9 fn parse(&self, path: &Path, bytes: &[u8]) -> Result<ConfigValue, SourceError>;
14
15 fn serialize(&self, value: &ConfigValue) -> Result<Vec<u8>, SourceError>;
20}
21
22#[cfg(feature = "toml")]
24pub struct TomlFormat;
25
26#[cfg(feature = "toml")]
27impl Format for TomlFormat {
28 fn name(&self) -> &'static str {
29 "toml"
30 }
31
32 fn parse(&self, path: &Path, bytes: &[u8]) -> Result<ConfigValue, SourceError> {
33 let s = std::str::from_utf8(bytes).map_err(|e| SourceError::Parse {
34 path: path.to_owned(),
35 message: e.to_string(),
36 })?;
37 let toml_val: toml::Value = toml::from_str(s).map_err(|e| SourceError::Parse {
38 path: path.to_owned(),
39 message: e.to_string(),
40 })?;
41 Ok(toml_val.into())
42 }
43
44 fn serialize(&self, value: &ConfigValue) -> Result<Vec<u8>, SourceError> {
45 let toml_val: toml::Value = value.clone().into();
46 toml::to_string(&toml_val)
47 .map(String::into_bytes)
48 .map_err(|e| SourceError::Serialize(e.to_string()))
49 }
50}