Skip to main content

ephemeris_documents/error/
internal.rs

1//! Errors that are bugs in this crate.
2
3use comrak::nodes::ValidationError;
4
5use super::{DocumentError, Feature};
6
7/// A failure that is a bug in this crate, not a problem with the input.
8#[derive(Debug, thiserror::Error)]
9pub enum InternalError {
10    /// A byte range computed for splicing fell outside the Markdown source.
11    #[error("Markdown splice range is outside the source")]
12    MarkdownSpliceOutOfBounds,
13    /// The rendered Markdown tree has a node its parent cannot hold.
14    #[error("Markdown tree has a {child} node inside a {parent} node")]
15    MarkdownTreeInvalid {
16        /// The XML name of the parent node kind.
17        parent: &'static str,
18        /// The XML name of the child node kind.
19        child: &'static str,
20    },
21    /// A value reached a renderer whose format lacks a feature it needs. The
22    /// validation before rendering should have rejected it.
23    #[error("value needs {feature}, which validation did not reject")]
24    FeatureNotValidated {
25        /// The feature the value needs.
26        feature: Feature,
27    },
28    /// The renderer produced TOML text that does not parse.
29    #[error("rendered TOML is invalid")]
30    TomlRenderedInvalid {
31        /// The underlying parse error.
32        #[source]
33        source: Box<toml_edit::TomlError>,
34    },
35    /// The renderer produced YAML text that does not read back.
36    #[error("rendered YAML is invalid")]
37    YamlRenderedInvalid {
38        /// The error from reading the rendered text.
39        #[source]
40        source: Box<DocumentError>,
41    },
42    /// The YAML parser did not return a node it reported.
43    #[error("YAML node is missing from the parsed document")]
44    YamlNodeMissing,
45    /// The YAML parser reported a span that is not a range of the source.
46    #[error("YAML node span {start}..{end} is outside the source")]
47    YamlSpanInvalid {
48        /// The start byte offset.
49        start: usize,
50        /// The end byte offset.
51        end: usize,
52    },
53}
54
55impl InternalError {
56    pub(crate) fn markdown_tree(error: &ValidationError<'_>) -> Self {
57        match error {
58            ValidationError::InvalidChildType { parent, child } => Self::MarkdownTreeInvalid {
59                parent: parent.data().value.xml_node_name(),
60                child: child.data().value.xml_node_name(),
61            },
62        }
63    }
64
65    pub(crate) fn toml_rendered(source: toml_edit::TomlError) -> Self {
66        Self::TomlRenderedInvalid {
67            source: Box::new(source),
68        }
69    }
70
71    pub(crate) fn yaml_rendered(source: DocumentError) -> Self {
72        Self::YamlRenderedInvalid {
73            source: Box::new(source),
74        }
75    }
76}