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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![deny(missing_docs)]
#![warn(private_intra_doc_links)]
#![warn(missing_crate_level_docs)]
#![warn(missing_doc_code_examples)]
#![warn(private_doc_tests)]
#![deny(missing_debug_implementations)]
mod calculator_float;
pub use calculator_float::CalculatorFloat;
mod calculator;
pub use calculator::Calculator;
mod calculator_complex;
pub use calculator_complex::CalculatorComplex;
use thiserror::Error;
#[derive(Error, Debug, PartialEq)]
pub enum CalculatorError {
#[error("Input cannot be converted to CalculatorFloat")]
NotConvertable,
#[error("Symbolic value {val:?} can not be converted to float")]
FloatSymbolicNotConvertable {
val: String,
},
#[error("Symbolic value {val:?} can not be converted to complex")]
ComplexSymbolicNotConvertable {
val: CalculatorComplex,
},
#[error("Imaginary part of CalculatorComplex {val:?} not zero")]
ComplexCanNotBeConvertedToFloat {
val: CalculatorComplex,
},
#[error("Parsing error: {msg:?}")]
ParsingError {
msg: &'static str,
},
#[error("Function {fct:?} not implemented.")]
NotImplementedError {
fct: &'static str,
},
#[error("Function {fct:?} not found.")]
FunctionNotFound {
fct: String,
},
#[error("Variable {name:?} not set.")]
VariableNotSet {
name: String,
},
#[error("Parsing error: Unexpected end of expression")]
UnexpectedEndOfExpression,
#[error("Division by zero error")]
DivisionByZero,
#[error("Parsing Expression did not return value as expected.")]
NoValueReturnedParsing,
#[error("Not enough function arguments.")]
NotEnoughFunctionArguments,
}
#[cfg(test)]
mod tests {
use super::CalculatorComplex;
use super::CalculatorError;
#[test]
fn test_debug() {
let not_conv = CalculatorError::NotConvertable;
assert_eq!(format!("{:?}", not_conv), "NotConvertable");
let float_sym = CalculatorError::FloatSymbolicNotConvertable {
val: String::from("2x"),
};
assert_eq!(
format!("{:?}", float_sym),
"FloatSymbolicNotConvertable { val: \"2x\" }"
);
let complex_sym = CalculatorError::ComplexSymbolicNotConvertable {
val: CalculatorComplex::from("2x"),
};
assert_eq!(
format!("{:?}", complex_sym),
"ComplexSymbolicNotConvertable { val: CalculatorComplex { re: Str(\"2x\"), im: Float(0.0) } }"
);
let complex_im_sym = CalculatorError::ComplexSymbolicNotConvertable {
val: CalculatorComplex::new(1, 3),
};
assert_eq!(
format!("{:?}", complex_im_sym),
"ComplexSymbolicNotConvertable { val: CalculatorComplex { re: Float(1.0), im: Float(3.0) } }"
);
let parse = CalculatorError::ParsingError { msg: "test" };
assert_eq!(format!("{:?}", parse), "ParsingError { msg: \"test\" }");
let not_impl = CalculatorError::NotImplementedError { fct: "Test" };
assert_eq!(
format!("{:?}", not_impl),
"NotImplementedError { fct: \"Test\" }"
);
let func_not_found = CalculatorError::FunctionNotFound {
fct: String::from("Test"),
};
assert_eq!(
format!("{:?}", func_not_found),
"FunctionNotFound { fct: \"Test\" }"
);
let var_not_set = CalculatorError::VariableNotSet {
name: String::from("Test"),
};
assert_eq!(
format!("{:?}", var_not_set),
"VariableNotSet { name: \"Test\" }"
);
let end_of_exp = CalculatorError::UnexpectedEndOfExpression;
assert_eq!(format!("{:?}", end_of_exp), "UnexpectedEndOfExpression");
let div_zero = CalculatorError::DivisionByZero;
assert_eq!(format!("{:?}", div_zero), "DivisionByZero");
let parsing_no_val = CalculatorError::NoValueReturnedParsing;
assert_eq!(format!("{:?}", parsing_no_val), "NoValueReturnedParsing");
let func_args = CalculatorError::NotEnoughFunctionArguments;
assert_eq!(format!("{:?}", func_args), "NotEnoughFunctionArguments");
}
}