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
}
}