1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::error::Error;
use std::fmt;
pub type TeraResult<T> = Result<T, TeraError>;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TeraErrorType {
TemplateNotFound,
FieldNotFound,
NotANumber,
NotAnArray
}
#[derive(Debug)]
pub struct TeraError {
pub error: String,
pub error_type: TeraErrorType
}
impl Error for TeraError {
fn description(&self) -> &str {
&*self.error
}
fn cause(&self) -> Option<&Error> {
match self.error_type {
_ => None,
}
}
}
impl fmt::Display for TeraError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{}", self.error)
}
}
pub fn template_not_found(name: &str) -> TeraError {
TeraError {
error: format!("Template `{}` not found", name),
error_type: TeraErrorType::TemplateNotFound
}
}
pub fn field_not_found(key: &str) -> TeraError {
TeraError {
error: format!("Field `{}` not found in context", key),
error_type: TeraErrorType::FieldNotFound
}
}
pub fn not_a_number(key: &str) -> TeraError {
TeraError {
error: format!("Field `{}` was used in a math operation but is not a number", key),
error_type: TeraErrorType::NotANumber
}
}
pub fn not_an_array(key: &str) -> TeraError {
TeraError {
error: format!("Field `{}` is not an array but was used as iterator in forloop", key),
error_type: TeraErrorType::NotAnArray
}
}