Skip to main content

schematic/config/formats/
ron.rs

1use super::create_span;
2use crate::config::error::ConfigError;
3use crate::config::parser::ParserError;
4use crate::config::source::*;
5use miette::NamedSource;
6use serde::de::DeserializeOwned;
7use std::path::Path;
8
9#[derive(Default)]
10pub struct RonFormat {}
11
12impl<T: DeserializeOwned> SourceFormat<T> for RonFormat {
13    fn should_parse(&self, source: &Source) -> bool {
14        source.get_file_ext() == Some("ron")
15    }
16
17    fn parse(
18        &self,
19        source: &Source,
20        content: &str,
21        _cache_path: Option<&Path>,
22    ) -> Result<T, ConfigError> {
23        let de = &mut ron::Deserializer::from_str(content).map_err(|error| ParserError {
24            content: NamedSource::new(source.get_file_name(), content.to_owned()),
25            path: String::new(),
26            span: Some(create_span(
27                content,
28                error.span.start.line,
29                error.span.start.col,
30            )),
31            message: error.to_string(),
32        })?;
33
34        let result: T = serde_path_to_error::deserialize(de).map_err(|error| ParserError {
35            content: NamedSource::new(source.get_file_name(), content.to_owned()),
36            path: error.path().to_string(),
37            span: None,
38            message: error.inner().to_string(),
39        })?;
40
41        Ok(result)
42    }
43}