Skip to main content

copperlace/render/
error.rs

1use std::fmt;
2
3/// Error returned while compiling or rendering Copperlace rules.
4#[derive(Debug, PartialEq, Eq)]
5pub enum RenderError {
6    /// A template referenced a name that is neither bound nor defined as a rule.
7    UnknownRule(String),
8    /// A template pipeline referenced a processor that is not registered.
9    UnknownProcessor(String),
10    /// A registered processor rejected the rendered value.
11    ProcessorError { processor: String, message: String },
12    /// A `{...}` template expression could not be parsed.
13    InvalidExpression(String),
14    /// An array-backed choice rule had no alternatives.
15    EmptyChoice,
16    /// A weighted choice config entry is malformed.
17    InvalidWeightedChoice(String),
18    /// Rendering detected a recursive rule cycle.
19    CircularRuleReference(Vec<String>),
20    /// A config value type was parsed but is not renderable.
21    UnsupportedValue(String),
22    /// The root configuration value was not an object.
23    InvalidConfigRoot,
24    /// A structured render was requested for a non-object path.
25    UnsupportedStructuredTarget(String),
26    /// A structured value could not be serialized to JSON.
27    JsonSerialization(String),
28}
29
30impl fmt::Display for RenderError {
31    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            RenderError::UnknownRule(rule) => write!(formatter, "unknown rule: {rule}"),
34            RenderError::UnknownProcessor(processor) => {
35                write!(formatter, "unknown processor: {processor}")
36            }
37            RenderError::ProcessorError { processor, message } => {
38                write!(formatter, "processor {processor} failed: {message}")
39            }
40            RenderError::InvalidExpression(expression) => {
41                write!(formatter, "invalid template expression: {expression}")
42            }
43            RenderError::EmptyChoice => write!(formatter, "cannot render an empty choice"),
44            RenderError::InvalidWeightedChoice(message) => {
45                write!(formatter, "invalid weighted choice: {message}")
46            }
47            RenderError::CircularRuleReference(cycle) => {
48                write!(formatter, "circular rule reference: {}", cycle.join(" -> "))
49            }
50            RenderError::UnsupportedValue(value_type) => {
51                write!(formatter, "unsupported value type: {value_type}")
52            }
53            RenderError::InvalidConfigRoot => write!(formatter, "config root must be an object"),
54            RenderError::UnsupportedStructuredTarget(rule) => {
55                write!(
56                    formatter,
57                    "structured render target must be an object: {rule}"
58                )
59            }
60            RenderError::JsonSerialization(message) => {
61                write!(
62                    formatter,
63                    "failed to serialize structured value as JSON: {message}"
64                )
65            }
66        }
67    }
68}
69
70impl std::error::Error for RenderError {}