lithos_gotmpl_engine/
error.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2use crate::ast::Span;
3use thiserror::Error;
4
5/// Unified error type for the template engine.
6///
7/// Errors carry the message, optional source error, and – when available – the
8/// `Span` pointing to the offending location in the template. Prefer the
9/// `*_with_span` constructors when propagating parse or render failures that
10/// originate from a concrete region of the template.
11#[derive(Debug, Error)]
12pub enum Error {
13    #[error("parse error: {message}")]
14    Parse {
15        message: String,
16        #[source]
17        source: Option<Box<dyn std::error::Error + Send + Sync>>,
18        span: Option<Span>,
19    },
20    #[error("render error: {message}")]
21    Render {
22        message: String,
23        #[source]
24        source: Option<Box<dyn std::error::Error + Send + Sync>>,
25        span: Option<Span>,
26    },
27}
28
29impl Error {
30    pub fn parse(message: impl Into<String>, span: Option<Span>) -> Self {
31        Error::Parse {
32            message: message.into(),
33            source: None,
34            span,
35        }
36    }
37
38    pub fn parse_with_span(message: impl Into<String>, span: Span) -> Self {
39        Self::parse(message, Some(span))
40    }
41
42    pub fn render(message: impl Into<String>, span: Option<Span>) -> Self {
43        Error::Render {
44            message: message.into(),
45            source: None,
46            span,
47        }
48    }
49
50    pub fn render_with_span(message: impl Into<String>, span: Span) -> Self {
51        Self::render(message, Some(span))
52    }
53}