string_template_plus/
errors.rs1use std::{error::Error, fmt};
2
3#[derive(Debug)]
5pub enum RenderTemplateError {
6 InvalidFormat(String, String),
8 VariableNotFound(String),
10 AllVariablesNotFound(Vec<String>),
12 TransformerError(TransformerError),
14}
15
16#[derive(Debug)]
18pub enum TransformerError {
19 InvalidSyntax(String, String),
21 UnknownTranformer(String, String),
23 TooManyArguments(&'static str, usize, usize),
25 TooFewArguments(&'static str, usize, usize),
27 InvalidValueType(&'static str, &'static str),
29 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}