Skip to main content

ephemeris_documents/spec/
text.rs

1//! The payload of a plain text document.
2
3use facet::Facet;
4
5use super::Payload;
6use crate::error::DocumentError;
7use crate::line_ending::LineEndingSpec;
8use crate::source::Source;
9use crate::text::{CommentSyntax, DocumentNode, TextDocument};
10
11/// A plain text document, rendered node by node.
12#[derive(Debug, Clone, Facet)]
13#[facet(deny_unknown_fields)]
14pub struct TextSpec {
15    /// The comment syntax used to render comment nodes, if any.
16    #[facet(default)]
17    pub comment_syntax: Option<CommentSyntax>,
18    /// The document's ordered nodes.
19    pub nodes: Vec<DocumentNode>,
20    /// The line ending of the written document.
21    #[facet(default)]
22    pub line_ending: LineEndingSpec,
23}
24
25impl Payload for TextSpec {
26    fn line_ending(&self) -> LineEndingSpec {
27        self.line_ending
28    }
29
30    fn reconcile(&self, source: &Source<'_>) -> Result<Vec<u8>, DocumentError> {
31        let document = TextDocument::new(self.comment_syntax, &self.nodes)?;
32        Ok(source.write(&document.generated(self.comment_syntax)))
33    }
34
35    fn validate(&self) -> Result<(), DocumentError> {
36        TextDocument::new(self.comment_syntax, &self.nodes)?;
37        Ok(())
38    }
39}