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
use std::collections::HashMap;
use rubble_templates_core::evaluator::{Function, Context, SyntaxError, EvaluationError};
use rubble_templates_core::functions::{SimpleFunction, FunctionWithContext};
use std::num::ParseFloatError;
use crate::std_fun::strings::EMPTY_STRING;
pub fn math_functions() -> HashMap<String, Box<dyn Function>> {
let mut functions: HashMap<String, Box<dyn Function>> = HashMap::new();
functions.insert("+".to_string(), SimpleFunction::new(plus_function));
functions.insert("-".to_string(), FunctionWithContext::new(minus_function));
functions.insert("*".to_string(), FunctionWithContext::new(multiply_function));
functions.insert("/".to_string(), FunctionWithContext::new(divide_function));
functions.insert("mod".to_string(), FunctionWithContext::new(modulo_function));
functions
}
pub fn plus_function(parameters: &[String]) -> String {
let mut result: String = EMPTY_STRING.to_string();
let mut floating_result: Option<f64> = None;
parameters.iter().for_each(|param| {
if result.is_empty() {
if let Result::Ok(value) = param.parse::<f64>() {
floating_result = Some(floating_result.unwrap_or(0 as f64) + value);
} else {
if let Some(number) = floating_result
.map(|number| number.to_string()) {
result += &number
}
result.push_str(param);
}
} else {
result.push_str(param);
};
});
if result.is_empty() && floating_result.is_some() {
floating_result.map(|number| number.to_string()).unwrap()
} else {
result
}
}
pub fn minus_function(parameters: &[String], _context: &mut Context) -> Result<String, SyntaxError> {
reduce_numbers(parameters, |a, b| a - b)
}
pub fn multiply_function(parameters: &[String], _context: &mut Context) -> Result<String, SyntaxError> {
reduce_numbers(parameters, |a, b| a * b)
}
pub fn divide_function(parameters: &[String], _context: &mut Context) -> Result<String, SyntaxError> {
reduce_numbers(parameters, |a, b| a / b)
}
pub fn modulo_function(parameters: &[String], _context: &mut Context) -> Result<String, SyntaxError> {
reduce_numbers(parameters, |a, b| a % b)
}
pub fn reduce_numbers<F>(parameters: &[String], f: F) -> Result<String, SyntaxError>
where F: Fn(f64, f64) -> f64 {
let (index, numbers) = as_numbers(parameters);
if let Result::Err(error) = numbers {
Err(SyntaxError::new(EvaluationError::InvalidValues {
description: Some(error.to_string()),
values: vec![parameters[index - 1].clone()],
})
)
} else {
Ok(numbers.unwrap()
.into_iter()
.reduce(f)
.unwrap_or(0 as f64)
.to_string()
)
}
}
pub fn as_numbers(parameters: &[String]) -> (usize, Result<Vec<f64>, ParseFloatError>) {
let mut index: usize = 0;
let numbers: Result<Vec<f64>, ParseFloatError> = parameters.iter()
.map(|number| {
index += 1;
number.parse::<f64>()
})
.collect();
(index, numbers)
}