Skip to main content

rskit_util/template/
error.rs

1use std::fmt;
2
3/// Errors returned by template parsing and rendering.
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum TemplateError {
7    /// The template has an unclosed placeholder (missing closing brace).
8    UnclosedPlaceholder(String),
9    /// The template has an unmatched closing brace.
10    UnmatchedClosingBrace(String),
11    /// A placeholder inside the template has an empty name.
12    EmptyPlaceholder,
13    /// A placeholder inside the template is not recognized.
14    UnknownPlaceholder(String),
15    /// A custom error returned by the renderer callback.
16    Render(String),
17    /// A dynamic variable required by the template had no value.
18    MissingVariable(String),
19}
20
21impl fmt::Display for TemplateError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::UnclosedPlaceholder(s) => write!(f, "unclosed placeholder in '{s}'"),
25            Self::UnmatchedClosingBrace(s) => {
26                write!(f, "unmatched closing placeholder brace in '{s}'")
27            }
28            Self::EmptyPlaceholder => write!(f, "placeholder cannot be empty"),
29            Self::UnknownPlaceholder(p) => write!(f, "unknown placeholder '{p}'"),
30            Self::Render(err) => write!(f, "template render failed: {err}"),
31            Self::MissingVariable(name) => write!(f, "missing template variable {name:?}"),
32        }
33    }
34}
35
36impl std::error::Error for TemplateError {}