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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::{error::Error, fmt};
use crate::error::parse::{expression::ExpressionError, variable::VariableError};
#[derive(Debug)]
pub struct ConditionError {
pub content: String,
pub kind: ConditionErrorKind,
}
#[derive(Debug)]
pub enum ConditionErrorKind {
BadLink,
BadValue,
CouldNotParse,
InvalidExpression(ExpressionError),
InvalidVariable(VariableError),
MultipleElseStatements,
NoCondition,
UnmatchedParenthesis,
}
impl Error for ConditionError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for ConditionErrorKind {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self {
ConditionErrorKind::InvalidVariable(err) => Some(err),
_ => None,
}
}
}
impl_from_error![
ConditionErrorKind;
[InvalidExpression, ExpressionError],
[InvalidVariable, VariableError]
];
impl fmt::Display for ConditionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} (condition string: '{}')", &self.kind, &self.content)
}
}
impl fmt::Display for ConditionErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ConditionErrorKind::*;
match &self {
BadLink => write!(
f,
"internal error: did not correctly partition conditions into parts separated \
by `and`/`or` markers"
),
BadValue => write!(f, "could not parse a number from the condition value"),
CouldNotParse => write!(f, "incorrectly formatted condition"),
InvalidExpression(err) => write!(
f,
"could not parse left or right hand side expression for a comparison: {}",
err
),
InvalidVariable(err) => write!(f, "could not parse variable in condition: {}", err),
MultipleElseStatements => write!(f, "found multiple else statements in condition"),
NoCondition => write!(f, "condition string was empty"),
UnmatchedParenthesis => write!(f, "contained unmatched parenthesis"),
}
}
}
impl ConditionError {
pub(crate) fn from_kind<T: Into<String>>(content: T, kind: ConditionErrorKind) -> Self {
ConditionError {
content: content.into(),
kind,
}
}
}