dysql_tpl/
error.rs

1// Ramhorns  Copyright (C) 2019  Maciej Hirsz
2//
3// This file is part of Ramhorns. This program comes with ABSOLUTELY NO WARRANTY;
4// This is free software, and you are welcome to redistribute it under the
5// conditions of the GNU General Public License version 3.0.
6//
7// You should have received a copy of the GNU General Public License
8// along with Ramhorns.  If not, see <http://www.gnu.org/licenses/>
9
10use std::{error, fmt, io};
11
12/// Error type used that can be emitted during template parsing.
13#[derive(Debug)]
14pub enum TemplateError {
15    /// There was an error with the IO (only happens when parsing a file)
16    Io(io::Error),
17
18    /// Stack overflow when parsing nested sections
19    StackOverflow,
20
21    /// Parser was expecting a tag closing a section `{{/foo}}`,
22    /// but never found it or found a different one.
23    UnclosedSection(String),
24
25    /// Similar to above, but happens if `{{/foo}}` happens while
26    /// no section was open
27    UnopenedSection(String),
28
29    /// Parser was expecting to find the closing braces of a tag `}}`, but never found it.
30    UnclosedTag,
31
32    /// Partials are not allowed in the given context (e.g. parsing a template from string)
33    PartialsDisabled,
34
35    /// Attempted to load a partial outside of the templates folder
36    IllegalPartial(String),
37
38    /// The template file with the given name was not found
39    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}