Skip to main content

opys_engine/mdprism/
error.rs

1//! Error types: schema-parse errors (located) and document-validation problems.
2
3/// A failure while parsing the DSL schema source. Carries a 1-based line/column.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5#[error("schema:{line}:{col}: {message}")]
6pub struct SchemaError {
7    pub line: usize,
8    pub col: usize,
9    pub message: String,
10}
11
12impl SchemaError {
13    pub(crate) fn new(line: usize, col: usize, message: impl Into<String>) -> Self {
14        SchemaError {
15            line,
16            col,
17            message: message.into(),
18        }
19    }
20}
21
22/// One way a document failed to conform, addressed by the breadcrumb `path`
23/// (alias chain) of the schema node that was unmet.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct Problem {
26    pub path: Vec<String>,
27    pub message: String,
28}
29
30impl std::fmt::Display for Problem {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        if self.path.is_empty() {
33            write!(f, "{}", self.message)
34        } else {
35            write!(f, "{}: {}", self.path.join(" › "), self.message)
36        }
37    }
38}
39
40/// A failure while rendering markdown from data via [`Schema::render`].
41#[derive(Debug, Clone, thiserror::Error)]
42pub enum RenderError {
43    #[error("required field '{0}' is missing from the data")]
44    MissingField(String),
45    #[error("field '{field}' has wrong type: expected {expected}")]
46    WrongType {
47        field: String,
48        expected: &'static str,
49    },
50}
51
52/// A failure while editing a document in-place via [`Schema::edit`].
53#[derive(Debug, Clone, thiserror::Error)]
54pub enum EditError {
55    #[error("target path does not resolve to an editable node")]
56    TargetNotFound,
57    #[error("index {index} is out of range (list has {len} items)")]
58    IndexOutOfRange { index: usize, len: usize },
59}