use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub enum RenderError {
UnknownRule(String),
UnknownProcessor(String),
ProcessorError { processor: String, message: String },
InvalidExpression(String),
EmptyChoice,
ExhaustedUniqueChoice(String),
UnsupportedUniqueChoice(String),
InvalidWeightedChoice(String),
CircularRuleReference(Vec<String>),
UnsupportedValue(String),
UnsupportedIterationSource {
source: String,
value_type: String,
},
ImmutableLoopBinding(String),
InvalidConfigRoot,
UnsupportedStructuredTarget(String),
JsonSerialization(String),
}
impl fmt::Display for RenderError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RenderError::UnknownRule(rule) => write!(formatter, "unknown rule: {rule}"),
RenderError::UnknownProcessor(processor) => {
write!(formatter, "unknown processor: {processor}")
}
RenderError::ProcessorError { processor, message } => {
write!(formatter, "processor {processor} failed: {message}")
}
RenderError::InvalidExpression(expression) => {
write!(formatter, "invalid template expression: {expression}")
}
RenderError::EmptyChoice => write!(formatter, "cannot render an empty choice"),
RenderError::ExhaustedUniqueChoice(rule) => {
write!(formatter, "exhausted unique choice: {rule}")
}
RenderError::UnsupportedUniqueChoice(rule) => {
write!(formatter, "unique choice target is not a choice: {rule}")
}
RenderError::InvalidWeightedChoice(message) => {
write!(formatter, "invalid weighted choice: {message}")
}
RenderError::CircularRuleReference(cycle) => {
write!(formatter, "circular rule reference: {}", cycle.join(" -> "))
}
RenderError::UnsupportedValue(value_type) => {
write!(formatter, "unsupported value type: {value_type}")
}
RenderError::UnsupportedIterationSource { source, value_type } => {
write!(
formatter,
"iteration source is not iterable: {source} ({value_type})"
)
}
RenderError::ImmutableLoopBinding(name) => {
write!(formatter, "cannot overwrite immutable loop binding: {name}")
}
RenderError::InvalidConfigRoot => write!(formatter, "config root must be an object"),
RenderError::UnsupportedStructuredTarget(rule) => {
write!(
formatter,
"structured render target must be an object: {rule}"
)
}
RenderError::JsonSerialization(message) => {
write!(
formatter,
"failed to serialize structured value as JSON: {message}"
)
}
}
}
}
impl std::error::Error for RenderError {}