Skip to main content

taino_edit_core/
error.rs

1//! Error types for schema construction and document operations.
2
3use std::fmt;
4
5/// Error raised while building or using a [`Schema`](crate::Schema).
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum SchemaError {
8    /// A node or mark name was declared more than once.
9    DuplicateType(String),
10    /// The declared top node name has no matching node type.
11    UnknownTopNode(String),
12    /// A content expression referenced an unknown node name or group.
13    UnknownContentRef {
14        /// The node type whose content expression is invalid.
15        in_type: String,
16        /// The unresolved name or group token.
17        reference: String,
18    },
19    /// A content expression failed to parse.
20    BadContentExpression {
21        /// The node type whose content expression is invalid.
22        in_type: String,
23        /// Human-readable parser message.
24        message: String,
25    },
26    /// No node type was registered at all.
27    Empty,
28}
29
30impl fmt::Display for SchemaError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            SchemaError::DuplicateType(n) => write!(f, "duplicate type name `{n}`"),
34            SchemaError::UnknownTopNode(n) => {
35                write!(f, "top node `{n}` is not a declared node type")
36            }
37            SchemaError::UnknownContentRef { in_type, reference } => {
38                write!(f, "content of `{in_type}` references unknown `{reference}`")
39            }
40            SchemaError::BadContentExpression { in_type, message } => {
41                write!(f, "content expression of `{in_type}` is invalid: {message}")
42            }
43            SchemaError::Empty => write!(f, "schema declares no node types"),
44        }
45    }
46}
47
48impl std::error::Error for SchemaError {}
49
50/// Error raised while constructing or deserializing a document.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub enum DocError {
53    /// A referenced node type name is not in the schema.
54    UnknownNodeType(String),
55    /// A referenced mark type name is not in the schema.
56    UnknownMarkType(String),
57    /// Children did not satisfy the parent node type's content expression.
58    InvalidContent {
59        /// Parent node type name.
60        parent: String,
61    },
62    /// A position was out of range for the document it was resolved against.
63    PositionOutOfRange {
64        /// The offending position.
65        pos: usize,
66        /// The maximum valid position.
67        max: usize,
68    },
69    /// JSON did not match the expected document shape.
70    MalformedJson(String),
71    /// HTML input was malformed or exceeded a safety limit (e.g. nesting
72    /// depth).
73    HtmlParse(String),
74}
75
76impl fmt::Display for DocError {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        match self {
79            DocError::UnknownNodeType(n) => write!(f, "unknown node type `{n}`"),
80            DocError::UnknownMarkType(n) => write!(f, "unknown mark type `{n}`"),
81            DocError::InvalidContent { parent } => {
82                write!(f, "content does not match the schema for `{parent}`")
83            }
84            DocError::PositionOutOfRange { pos, max } => {
85                write!(f, "position {pos} out of range (max {max})")
86            }
87            DocError::MalformedJson(m) => write!(f, "malformed document JSON: {m}"),
88            DocError::HtmlParse(m) => write!(f, "could not parse HTML: {m}"),
89        }
90    }
91}
92
93impl std::error::Error for DocError {}