oxidize_pdf/templates/
error.rs1use std::fmt;
2
3#[derive(Debug, Clone)]
5pub enum TemplateError {
6 VariableNotFound(String),
8 InvalidPlaceholder(String),
10 CircularReference(String),
12 InvalidVariableName(String),
14 ParseError(String),
16 RenderError(String),
18}
19
20impl fmt::Display for TemplateError {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 Self::VariableNotFound(var) => {
24 write!(f, "Variable '{}' not found in template context", var)
25 }
26 Self::InvalidPlaceholder(placeholder) => {
27 write!(f, "Invalid placeholder syntax: '{}'", placeholder)
28 }
29 Self::CircularReference(var) => {
30 write!(f, "Circular reference detected for variable '{}'", var)
31 }
32 Self::InvalidVariableName(name) => {
33 write!(f, "Invalid variable name: '{}' (must contain only alphanumeric characters, underscores, and dots)", name)
34 }
35 Self::ParseError(msg) => {
36 write!(f, "Template parsing error: {}", msg)
37 }
38 Self::RenderError(msg) => {
39 write!(f, "Template rendering error: {}", msg)
40 }
41 }
42 }
43}
44
45impl std::error::Error for TemplateError {}
46
47pub type TemplateResult<T> = std::result::Result<T, TemplateError>;
49
50impl From<regex::Error> for TemplateError {
51 fn from(err: regex::Error) -> Self {
52 TemplateError::ParseError(format!("Regex error: {}", err))
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_error_display() {
62 let err = TemplateError::VariableNotFound("name".to_string());
63 assert_eq!(
64 format!("{}", err),
65 "Variable 'name' not found in template context"
66 );
67
68 let err = TemplateError::InvalidPlaceholder("{{invalid}}".to_string());
69 assert!(format!("{}", err).contains("Invalid placeholder syntax"));
70 }
71
72 #[test]
73 fn test_error_debug() {
74 let err = TemplateError::CircularReference("var1".to_string());
75 assert!(format!("{:?}", err).contains("CircularReference"));
76 }
77}