1use std::{error, fmt, io};
11
12#[derive(Debug)]
14pub enum TemplateError {
15 Io(io::Error),
17
18 StackOverflow,
20
21 UnclosedSection(String),
24
25 UnopenedSection(String),
28
29 UnclosedTag,
31
32 PartialsDisabled,
34
35 IllegalPartial(String),
37
38 NotFound(String),
40}
41
42impl error::Error for TemplateError {}
43
44impl From<io::Error> for TemplateError {
45 fn from(err: io::Error) -> Self {
46 TemplateError::Io(err)
47 }
48}
49
50impl<T> From<arrayvec::CapacityError<T>> for TemplateError {
51 fn from(_: arrayvec::CapacityError<T>) -> Self {
52 TemplateError::StackOverflow
53 }
54}
55
56impl fmt::Display for TemplateError {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 match self {
59 TemplateError::Io(err) => err.fmt(f),
60 TemplateError::StackOverflow => write!(
61 f,
62 "Ramhorns has overflown its stack when parsing nested sections",
63 ),
64 TemplateError::UnclosedSection(name) => write!(
65 f,
66 "Section not closed properly, was expecting {{{{/{}}}}}",
67 name
68 ),
69 TemplateError::UnopenedSection(name) => {
70 write!(f, "Unexpected closing section {{{{/{}}}}}", name)
71 }
72 TemplateError::UnclosedTag => write!(f, "Couldn't find closing braces matching opening braces"),
73 TemplateError::PartialsDisabled => write!(f, "Partials are not allowed in the current context"),
74 TemplateError::IllegalPartial(name) => write!(
75 f,
76 "Attempted to load {}; partials can only be loaded from the template directory",
77 name
78 ),
79 TemplateError::NotFound(name) => write!(f, "Template file {} not found", name),
80 }
81 }
82}
83
84#[cfg(test)]
85mod test {
86 use super::*;
87
88 #[test]
89 fn displays_properly() {
90 assert_eq!(
91 TemplateError::UnclosedSection("foo".into()).to_string(),
92 "Section not closed properly, was expecting {{/foo}}"
93 );
94 assert_eq!(
95 TemplateError::UnclosedTag.to_string(),
96 "Couldn't find closing braces matching opening braces"
97 );
98 }
99}