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
use std::{error::Error, fmt};
use crate::error::parse::variable::VariableError;
#[derive(Debug)]
pub struct ExpressionError {
pub content: String,
pub kind: ExpressionErrorKind,
}
#[derive(Debug)]
pub enum ExpressionErrorKind {
Empty,
InvalidHead { head: String },
InvalidVariable(VariableError),
NoOperator { content: String },
UnmatchedParenthesis,
}
impl Error for ExpressionError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for ExpressionErrorKind {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self {
ExpressionErrorKind::InvalidVariable(err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for ExpressionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} (expression string: '{}')", self.kind, self.content)
}
}
impl fmt::Display for ExpressionErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ExpressionErrorKind::*;
match &self {
Empty => write!(f, "empty string"),
InvalidHead { head } => write!(f, "no left hand side value before '{}'", head),
InvalidVariable(err) => write!(f, "invalid variable: {}", err),
NoOperator { content } => {
write!(f, "no mathematical operator before operand '{}'", content)
}
UnmatchedParenthesis => write!(f, "found unmatched parenthesis",),
}
}
}