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
use std::{error::Error, fmt};

/// Errors for the render template
#[derive(Debug)]
pub enum RenderTemplateError {
    /// The Template is not correctly formatted,
    InvalidFormat(String, String),
    /// Variable not found
    VariableNotFound(String),
    /// Any of the multiple Variables not found
    AllVariablesNotFound(Vec<String>),
    /// Error from Transformers
    TransformerError(TransformerError),
}

/// Errors for the transformers
#[derive(Debug)]
pub enum TransformerError {
    /// The transformer with the name doesn't exist
    UnknownTranformer(String, String),
    /// Number of arguments is more than required
    TooManyArguments(&'static str, usize, usize),
    /// Not enough arguments for the transformer
    TooFewArguments(&'static str, usize, usize),
    /// The transformer cannot transform the given type
    InvalidValueType(&'static str, &'static str),
    /// The argument provided is not the correct type
    InvalidArgumentType(&'static str, String, &'static str),
}

impl Error for RenderTemplateError {}
impl Error for TransformerError {}

impl From<TransformerError> for RenderTemplateError {
    fn from(item: TransformerError) -> Self {
        Self::TransformerError(item)
    }
}

impl fmt::Display for TransformerError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::UnknownTranformer(fun, val) => {
                write!(f, "{fun} transformer not found for value {val}")
            }
            Self::TooManyArguments(fun, r, g) => {
                write!(f, "{fun} takes at max {r} arguments {g} given")
            }
            Self::TooFewArguments(fun, r, g) => {
                write!(f, "{fun} needs at least {r} arguments {g} given")
            }
            Self::InvalidValueType(fun, t) => write!(f, "{fun} can only tranform {t} type values"),
            Self::InvalidArgumentType(fun, g, t) => {
                write!(f, "{fun} argument {g} needs to be of {t} type")
            }
        }
    }
}

impl fmt::Display for RenderTemplateError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::InvalidFormat(fstr, msg) => {
                write!(f, "Invalid Template: {fstr} => {msg}")
            }
            Self::VariableNotFound(var) => {
                write!(f, "Variable {var} not found")
            }
            Self::AllVariablesNotFound(vars) => {
                write!(f, "None of the variables {vars:?} found")
            }
            Self::TransformerError(e) => e.fmt(f),
        }
    }
}