oneline_template/template/
build_error.rs

1use crate::template::syntax::syntax_parse_error::SyntaxParseError as ParseError;
2use crate::template::argument_types_differ_error::ArgumentTypesDifferError;
3use std::error::Error;
4use std::fmt;
5
6/// Error reason when building template.
7#[derive(Debug)]
8pub enum BuildError {
9    /// Error when parsing template string.
10    Parse(ParseError),
11    /// Error when function not registered withing template engine.
12    FunctionNotFound(String),
13    /// Error when number of arguments that passed into function is differ from number of required arguments.
14    ArgumentsLengthDiffer(String),
15    /// Error when passed argument type is differ from required. 
16    ArgumentTypeNotMatch(ArgumentTypesDifferError),
17}
18
19impl From<ParseError> for BuildError {
20    fn from(error: ParseError) -> Self {
21        return BuildError::Parse(error);
22    }
23}
24
25impl From<ArgumentTypesDifferError> for BuildError {
26    fn from(error: ArgumentTypesDifferError) -> Self {
27        return BuildError::ArgumentTypeNotMatch(error);
28    }
29}
30
31impl fmt::Display for BuildError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            BuildError::Parse(ref error) => {
35                write!(f, "{}", error)
36            },
37            BuildError::FunctionNotFound(ref function_name) => {
38                write!(f, "Function with name `{}` not found", function_name)
39            },
40            BuildError::ArgumentsLengthDiffer(ref function_name) => {
41                write!(f, "Into function `{}` passed arguments with wrong length", function_name)
42            },
43            BuildError::ArgumentTypeNotMatch(ref error) => {
44                write!(f, "Argument with index `{}` at function `{}` has wrong type", error.get_argument_position(), error.get_function_name())
45            },
46        }
47    }
48}
49
50impl Error for BuildError {
51    fn source(&self) -> Option<&(dyn Error + 'static)> {
52        match self {
53            BuildError::Parse(ref error) => {
54                Some(error)
55            },
56            BuildError::FunctionNotFound(..) => {
57                None
58            },
59            BuildError::ArgumentsLengthDiffer(..) => {
60                None
61            },
62            BuildError::ArgumentTypeNotMatch(..) => {
63                None
64            },
65        }
66    }
67}