schematic/config/formats/
toml.rs1use crate::config::error::{ConfigError, HandlerError};
2use crate::config::parser::ParserError;
3use crate::config::source::*;
4use miette::NamedSource;
5use serde::de::DeserializeOwned;
6use std::path::Path;
7
8#[derive(Default)]
9pub struct TomlFormat {}
10
11impl<T: DeserializeOwned> SourceFormat<T> for TomlFormat {
12 fn should_parse(&self, source: &Source) -> bool {
13 source.get_file_ext() == Some("toml")
14 }
15
16 fn parse(
17 &self,
18 source: &Source,
19 content: &str,
20 _cache_path: Option<&Path>,
21 ) -> Result<T, ConfigError> {
22 let de = toml::Deserializer::parse(content).map_err(|error| {
23 ConfigError::Handler(Box::new(HandlerError::new(error.to_string())))
24 })?;
25
26 let result: T = serde_path_to_error::deserialize(de).map_err(|error| ParserError {
27 content: NamedSource::new(source.get_file_name(), content.to_owned()),
28 path: error.path().to_string(),
29 span: error.inner().span().map(|s| s.into()),
30 message: error.inner().message().to_owned(),
31 })?;
32
33 Ok(result)
34 }
35}