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