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
use super::*;
use polynomial::Polynomial;
#[derive(Debug, PartialEq)]
pub enum CalculationError {
ParsingError(ParsingError),
EvaluationError(EvaluationError)
}
impl From<ParsingError> for CalculationError {
fn from(e: ParsingError) -> CalculationError {
CalculationError::ParsingError(e)
}
}
impl From<EvaluationError> for CalculationError {
fn from(e: EvaluationError) -> CalculationError {
CalculationError::EvaluationError(e)
}
}
pub trait Calculator<T, P, E>
where T: StringProcessor + Default,
P: TokensProcessor + Default,
E: TokensReducer + Default {
fn process(&self, line: &str) -> Result<Polynomial, CalculationError> {
let mut tokenizer = T::default();
let mut parser = P::default();
let evaluator = E::default();
let tokens = tokenizer.process(line);
let parsed_tokens = parser.process(tokens)?;
let evaluated = evaluator.process(parsed_tokens)?;
Ok(evaluated)
}
}