Skip to main content

ephemeris_documents/toml/
backend.rs

1//! The TOML backend of the generic merge.
2
3use toml_edit::Item;
4
5use super::tree::TomlTree;
6use crate::annotation::Annotated;
7use crate::backend::Backend;
8use crate::error::{DocumentError, Feature};
9use crate::format::{Format, InlineWidth};
10use crate::header::Existing;
11
12/// Reconciles TOML documents, rendering inline tables up to a fixed width.
13#[derive(Debug, Clone, Copy)]
14pub(crate) struct Toml {
15    pub(super) inline_width: InlineWidth,
16    format: Format,
17}
18
19impl Toml {
20    pub(crate) const fn new(inline_width: InlineWidth) -> Self {
21        Self {
22            inline_width,
23            format: Format::Toml { inline_width },
24        }
25    }
26
27    pub(super) const fn unsupported(self, feature: Feature) -> DocumentError {
28        DocumentError::value(self.format.reject(feature))
29    }
30}
31
32impl Backend for Toml {
33    type Document = TomlTree;
34
35    fn parse(&self, source: &str) -> Result<TomlTree, DocumentError> {
36        Ok(TomlTree::parse(source)?)
37    }
38
39    fn format(&self) -> Format {
40        self.format
41    }
42
43    fn build(&self, desired: &Annotated) -> Result<TomlTree, DocumentError> {
44        self.build_document(desired)
45    }
46
47    fn string_leaf(&self, node: &Item) -> Result<Option<String>, DocumentError> {
48        Ok(node.as_str().map(str::to_owned))
49    }
50
51    fn finish(&self, current: &mut TomlTree, desired: &TomlTree, existing: Existing) {
52        current.align_with(desired, existing);
53    }
54}
55
56#[cfg(test)]
57mod tests;