oneline_template/template/syntax/
syntax_parse_error.rs

1use crate::template::syntax::expected_token_error::ExpectedTokenError;
2use crate::template::syntax::invalid_argument_template::InvalidArgumentTemplate;
3use crate::template::syntax::unexpected_input_error::UnexpectedInputError;
4
5use std::num::ParseIntError;
6use std::error::Error;
7use std::fmt;
8
9#[derive(Debug)]
10/// Template format parsing error.
11pub enum SyntaxParseError {
12    /// Parsing integer error.
13    ParseInt(ParseIntError),
14    /// Function name contains invalid chars.
15    WrongFunctionName,
16    /// Field name contains invalid chars.
17    WrongFieldName,
18    /// Wrong input was passed when some token is expected.
19    ExpectedToken(ExpectedTokenError),
20    /// Wrong input was passed.
21    UnexpectedInput(UnexpectedInputError),
22    /// Template is empty. Nothing to format.
23    TemplateIsEmpty,
24    /// Unable to parse argument passed into function.
25    InvalidArgumentTemplate(InvalidArgumentTemplate),
26}
27
28impl fmt::Display for SyntaxParseError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            SyntaxParseError::ParseInt(ref error) => {
32                write!(f, "{}", error)
33            },
34            SyntaxParseError::WrongFunctionName => {
35                write!(f, "Wrong function name")
36            },
37            SyntaxParseError::WrongFieldName => {
38                write!(f, "Wrong field name")
39            },
40            SyntaxParseError::ExpectedToken(ref error) => {
41                write!(f, "{}", error)
42            },
43            SyntaxParseError::UnexpectedInput(ref error) => {
44                write!(f, "{}", error)
45            },
46            SyntaxParseError::TemplateIsEmpty => {
47                write!(f, "Template is empty")
48            },
49            SyntaxParseError::InvalidArgumentTemplate(ref error) => {
50                write!(f, "{}", error)
51            },
52        }
53    }
54}
55
56impl From<ParseIntError> for SyntaxParseError {
57    fn from(error: ParseIntError) -> Self {
58        return SyntaxParseError::ParseInt(error);
59    }
60}
61
62impl From<ExpectedTokenError> for SyntaxParseError {
63    fn from(error: ExpectedTokenError) -> Self {
64        return SyntaxParseError::ExpectedToken(error);
65    }
66}
67
68impl From<UnexpectedInputError> for SyntaxParseError {
69    fn from(error: UnexpectedInputError) -> Self {
70        return SyntaxParseError::UnexpectedInput(error);
71    }
72}
73
74impl From<InvalidArgumentTemplate> for SyntaxParseError {
75    fn from(error: InvalidArgumentTemplate) -> Self {
76        return SyntaxParseError::InvalidArgumentTemplate(error);
77    }
78}
79
80impl Error for SyntaxParseError {
81    fn source(&self) -> Option<&(dyn Error + 'static)> {
82        match self {
83            SyntaxParseError::ParseInt(ref error) => {
84                Some(error)
85            },
86            SyntaxParseError::WrongFunctionName => {
87                None
88            },
89            SyntaxParseError::WrongFieldName => {
90                None
91            },
92            SyntaxParseError::ExpectedToken(ref error) => {
93                Some(error)
94            },
95            SyntaxParseError::UnexpectedInput(ref error) => {
96                Some(error)
97            },
98            SyntaxParseError::TemplateIsEmpty => {
99                None
100            },
101            SyntaxParseError::InvalidArgumentTemplate(ref error) => {
102                Some(error)
103            },
104        }
105    }
106}