string_template_plus/
errors.rs

1use std::{error::Error, fmt};
2
3/// Errors for the render template
4#[derive(Debug)]
5pub enum RenderTemplateError {
6    /// The Template is not correctly formatted,
7    InvalidFormat(String, String),
8    /// Variable not found
9    VariableNotFound(String),
10    /// Any of the multiple Variables not found
11    AllVariablesNotFound(Vec<String>),
12    /// Error from Transformers
13    TransformerError(TransformerError),
14}
15
16/// Errors for the transformers
17#[derive(Debug)]
18pub enum TransformerError {
19    /// the Syntax is invalid
20    InvalidSyntax(String, String),
21    /// The transformer with the name doesn't exist
22    UnknownTranformer(String, String),
23    /// Number of arguments is more than required
24    TooManyArguments(&'static str, usize, usize),
25    /// Not enough arguments for the transformer
26    TooFewArguments(&'static str, usize, usize),
27    /// The transformer cannot transform the given type
28    InvalidValueType(&'static str, &'static str),
29    /// The argument provided is not the correct type
30    InvalidArgumentType(&'static str, String, &'static str),
31}
32
33impl Error for RenderTemplateError {}
34impl Error for TransformerError {}
35
36impl From<TransformerError> for RenderTemplateError {
37    fn from(item: TransformerError) -> Self {
38        Self::TransformerError(item)
39    }
40}
41
42impl fmt::Display for TransformerError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            Self::InvalidSyntax(syn, msg) => {
46                write!(f, "{syn}: {msg}")
47            }
48            Self::UnknownTranformer(fun, val) => {
49                write!(f, "{fun} transformer not found for value {val}")
50            }
51            Self::TooManyArguments(fun, r, g) => {
52                write!(f, "{fun} takes at max {r} arguments {g} given")
53            }
54            Self::TooFewArguments(fun, r, g) => {
55                write!(f, "{fun} needs at least {r} arguments {g} given")
56            }
57            Self::InvalidValueType(fun, t) => write!(f, "{fun} can only tranform {t} type values"),
58            Self::InvalidArgumentType(fun, g, t) => {
59                write!(f, "{fun} argument {g} needs to be of {t} type")
60            }
61        }
62    }
63}
64
65impl fmt::Display for RenderTemplateError {
66    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67        match self {
68            Self::InvalidFormat(fstr, msg) => {
69                write!(f, "Invalid Template: {fstr} => {msg}")
70            }
71            Self::VariableNotFound(var) => {
72                write!(f, "Variable {var} not found")
73            }
74            Self::AllVariablesNotFound(vars) => {
75                write!(f, "None of the variables {vars:?} found")
76            }
77            Self::TransformerError(e) => e.fmt(f),
78        }
79    }
80}