• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ISibboI / evalexpr / 11381099505

17 Oct 2024 08:20AM UTC coverage: 90.154% (-2.4%) from 92.543%
11381099505

push

github

web-flow
Make int and float types generic and change license to AGPLv3. (#171)

326 of 342 new or added lines in 13 files covered. (95.32%)

40 existing lines in 8 files now uncovered.

1346 of 1493 relevant lines covered (90.15%)

8.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.92
/src/interface/mod.rs
1
use crate::{
2
    error::EvalexprResultValue,
3
    token, tree,
4
    value::{
5
        numeric_types::{DefaultNumericTypes, EvalexprNumericTypes},
6
        TupleType,
7
    },
8
    Context, ContextWithMutableVariables, EmptyType, EvalexprError, EvalexprResult, HashMapContext,
9
    Node, Value, EMPTY_VALUE,
10
};
11

12
/// Evaluate the given expression string.
13
///
14
/// # Examples
15
///
16
/// ```rust
17
/// use evalexpr::*;
18
///
19
/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from_int(6)));
20
/// ```
21
///
22
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
23
pub fn eval(string: &str) -> EvalexprResultValue {
11✔
24
    eval_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
11✔
25
}
26

27
/// Evaluate the given expression string with the given context.
28
///
29
/// # Examples
30
///
31
/// ```rust
32
/// use evalexpr::*;
33
///
34
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
35
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
36
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
37
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
38
/// assert_eq!(eval_with_context("one + two + three", &context), Ok(Value::from_int(6)));
39
/// ```
40
///
41
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
42
pub fn eval_with_context<C: Context>(
12✔
43
    string: &str,
44
    context: &C,
45
) -> EvalexprResultValue<C::NumericTypes> {
46
    tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
12✔
47
}
48

49
/// Evaluate the given expression string with the given mutable context.
50
///
51
/// # Examples
52
///
53
/// ```rust
54
/// use evalexpr::*;
55
///
56
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
57
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
58
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
59
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
60
/// assert_eq!(eval_with_context_mut("one + two + three", &mut context), Ok(Value::from_int(6)));
61
/// ```
62
///
63
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
64
pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
16✔
65
    string: &str,
66
    context: &mut C,
67
) -> EvalexprResultValue<C::NumericTypes> {
68
    tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
16✔
69
}
70

71
/// Build the operator tree for the given expression string.
72
///
73
/// The operator tree can later on be evaluated directly.
74
/// This saves runtime if a single expression should be evaluated multiple times, for example with differing contexts.
75
///
76
/// # Examples
77
///
78
/// ```rust
79
/// use evalexpr::*;
80
///
81
/// let precomputed = build_operator_tree("one + two + three").unwrap(); // Do proper error handling here
82
///
83
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
84
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
85
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
86
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
87
///
88
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from_int(6)));
89
///
90
/// context.set_value("three".into(), Value::from_int(5)).unwrap(); // Do proper error handling here
91
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from_int(8)));
92
/// ```
93
///
94
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
95
pub fn build_operator_tree<NumericTypes: EvalexprNumericTypes>(
16✔
96
    string: &str,
97
) -> EvalexprResult<Node<NumericTypes>, NumericTypes> {
98
    tree::tokens_to_operator_tree(token::tokenize(string)?)
16✔
99
}
100

101
/// Evaluate the given expression string into a string.
102
///
103
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
104
pub fn eval_string(string: &str) -> EvalexprResult<String> {
2✔
105
    eval_string_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
2✔
106
}
107

108
/// Evaluate the given expression string into an integer.
109
///
110
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
111
pub fn eval_int(
3✔
112
    string: &str,
113
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Int> {
114
    eval_int_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
3✔
115
}
116

117
/// Evaluate the given expression string into a float.
118
///
119
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
120
pub fn eval_float(
2✔
121
    string: &str,
122
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Float> {
123
    eval_float_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
2✔
124
}
125

126
/// Evaluate the given expression string into a float.
127
/// If the result of the expression is an integer, it is silently converted into a float.
128
///
129
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
130
pub fn eval_number(
1✔
131
    string: &str,
132
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Float> {
133
    eval_number_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
1✔
134
}
135

136
/// Evaluate the given expression string into a boolean.
137
///
138
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
139
pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
2✔
140
    eval_boolean_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
2✔
141
}
142

143
/// Evaluate the given expression string into a tuple.
144
///
145
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
146
pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
1✔
147
    eval_tuple_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
1✔
148
}
149

150
/// Evaluate the given expression string into an empty value.
151
///
152
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
153
pub fn eval_empty(string: &str) -> EvalexprResult<EmptyType> {
1✔
154
    eval_empty_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
1✔
155
}
156

157
/// Evaluate the given expression string into a string with the given context.
158
///
159
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
160
pub fn eval_string_with_context<C: Context>(
1✔
161
    string: &str,
162
    context: &C,
163
) -> EvalexprResult<String, C::NumericTypes> {
164
    match eval_with_context(string, context) {
1✔
165
        Ok(Value::String(string)) => Ok(string),
1✔
166
        Ok(value) => Err(EvalexprError::expected_string(value)),
1✔
167
        Err(error) => Err(error),
1✔
168
    }
169
}
170

171
/// Evaluate the given expression string into an integer with the given context.
172
///
173
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
174
pub fn eval_int_with_context<C: Context>(
4✔
175
    string: &str,
176
    context: &C,
177
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Int, C::NumericTypes> {
178
    match eval_with_context(string, context) {
4✔
179
        Ok(Value::Int(int)) => Ok(int),
4✔
180
        Ok(value) => Err(EvalexprError::expected_int(value)),
1✔
181
        Err(error) => Err(error),
1✔
182
    }
183
}
184

185
/// Evaluate the given expression string into a float with the given context.
186
///
187
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
188
pub fn eval_float_with_context<C: Context>(
1✔
189
    string: &str,
190
    context: &C,
191
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
192
    match eval_with_context(string, context) {
1✔
193
        Ok(Value::Float(float)) => Ok(float),
1✔
194
        Ok(value) => Err(EvalexprError::expected_float(value)),
1✔
195
        Err(error) => Err(error),
1✔
196
    }
197
}
198

199
/// Evaluate the given expression string into a float with the given context.
200
/// If the result of the expression is an integer, it is silently converted into a float.
201
///
202
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
203
pub fn eval_number_with_context<C: Context>(
1✔
204
    string: &str,
205
    context: &C,
206
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
207
    match eval_with_context(string, context) {
1✔
208
        Ok(Value::Float(float)) => Ok(float),
1✔
209
        Ok(Value::Int(int)) => Ok(<C::NumericTypes as EvalexprNumericTypes>::int_as_float(
2✔
NEW
210
            &int,
×
211
        )),
212
        Ok(value) => Err(EvalexprError::expected_number(value)),
2✔
213
        Err(error) => Err(error),
1✔
214
    }
215
}
216

217
/// Evaluate the given expression string into a boolean with the given context.
218
///
219
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
220
pub fn eval_boolean_with_context<C: Context>(
2✔
221
    string: &str,
222
    context: &C,
223
) -> EvalexprResult<bool, C::NumericTypes> {
224
    match eval_with_context(string, context) {
2✔
225
        Ok(Value::Boolean(boolean)) => Ok(boolean),
2✔
226
        Ok(value) => Err(EvalexprError::expected_boolean(value)),
1✔
227
        Err(error) => Err(error),
1✔
228
    }
229
}
230

231
/// Evaluate the given expression string into a tuple with the given context.
232
///
233
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
234
pub fn eval_tuple_with_context<C: Context>(
1✔
235
    string: &str,
236
    context: &C,
237
) -> EvalexprResult<TupleType<C::NumericTypes>, C::NumericTypes> {
238
    match eval_with_context(string, context) {
1✔
239
        Ok(Value::Tuple(tuple)) => Ok(tuple),
1✔
240
        Ok(value) => Err(EvalexprError::expected_tuple(value)),
1✔
241
        Err(error) => Err(error),
1✔
242
    }
243
}
244

245
/// Evaluate the given expression string into an empty value with the given context.
246
///
247
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
248
pub fn eval_empty_with_context<C: Context>(
1✔
249
    string: &str,
250
    context: &C,
251
) -> EvalexprResult<EmptyType, C::NumericTypes> {
252
    match eval_with_context(string, context) {
1✔
253
        Ok(Value::Empty) => Ok(EMPTY_VALUE),
1✔
254
        Ok(value) => Err(EvalexprError::expected_empty(value)),
1✔
255
        Err(error) => Err(error),
1✔
256
    }
257
}
258

259
/// Evaluate the given expression string into a string with the given mutable context.
260
///
261
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
262
pub fn eval_string_with_context_mut<C: ContextWithMutableVariables>(
2✔
263
    string: &str,
264
    context: &mut C,
265
) -> EvalexprResult<String, C::NumericTypes> {
266
    match eval_with_context_mut(string, context) {
2✔
267
        Ok(Value::String(string)) => Ok(string),
2✔
268
        Ok(value) => Err(EvalexprError::expected_string(value)),
1✔
269
        Err(error) => Err(error),
1✔
270
    }
271
}
272

273
/// Evaluate the given expression string into an integer with the given mutable context.
274
///
275
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
276
pub fn eval_int_with_context_mut<C: ContextWithMutableVariables>(
4✔
277
    string: &str,
278
    context: &mut C,
279
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Int, C::NumericTypes> {
280
    match eval_with_context_mut(string, context) {
4✔
281
        Ok(Value::Int(int)) => Ok(int),
4✔
282
        Ok(value) => Err(EvalexprError::expected_int(value)),
1✔
283
        Err(error) => Err(error),
1✔
284
    }
285
}
286

287
/// Evaluate the given expression string into a float with the given mutable context.
288
///
289
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
290
pub fn eval_float_with_context_mut<C: ContextWithMutableVariables>(
2✔
291
    string: &str,
292
    context: &mut C,
293
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
294
    match eval_with_context_mut(string, context) {
2✔
295
        Ok(Value::Float(float)) => Ok(float),
2✔
296
        Ok(value) => Err(EvalexprError::expected_float(value)),
1✔
297
        Err(error) => Err(error),
1✔
298
    }
299
}
300

301
/// Evaluate the given expression string into a float with the given mutable context.
302
/// If the result of the expression is an integer, it is silently converted into a float.
303
///
304
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
305
pub fn eval_number_with_context_mut<C: ContextWithMutableVariables>(
1✔
306
    string: &str,
307
    context: &mut C,
308
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
309
    match eval_with_context_mut(string, context) {
1✔
310
        Ok(Value::Float(float)) => Ok(float),
1✔
311
        Ok(Value::Int(int)) => Ok(<C::NumericTypes as EvalexprNumericTypes>::int_as_float(
2✔
NEW
312
            &int,
×
313
        )),
314
        Ok(value) => Err(EvalexprError::expected_number(value)),
2✔
315
        Err(error) => Err(error),
1✔
316
    }
317
}
318

319
/// Evaluate the given expression string into a boolean with the given mutable context.
320
///
321
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
322
pub fn eval_boolean_with_context_mut<C: ContextWithMutableVariables>(
2✔
323
    string: &str,
324
    context: &mut C,
325
) -> EvalexprResult<bool, C::NumericTypes> {
326
    match eval_with_context_mut(string, context) {
2✔
327
        Ok(Value::Boolean(boolean)) => Ok(boolean),
2✔
328
        Ok(value) => Err(EvalexprError::expected_boolean(value)),
1✔
329
        Err(error) => Err(error),
1✔
330
    }
331
}
332

333
/// Evaluate the given expression string into a tuple with the given mutable context.
334
///
335
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
336
pub fn eval_tuple_with_context_mut<C: ContextWithMutableVariables>(
1✔
337
    string: &str,
338
    context: &mut C,
339
) -> EvalexprResult<TupleType<C::NumericTypes>, C::NumericTypes> {
340
    match eval_with_context_mut(string, context) {
1✔
341
        Ok(Value::Tuple(tuple)) => Ok(tuple),
1✔
342
        Ok(value) => Err(EvalexprError::expected_tuple(value)),
1✔
343
        Err(error) => Err(error),
1✔
344
    }
345
}
346

347
/// Evaluate the given expression string into an empty value with the given mutable context.
348
///
349
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
350
pub fn eval_empty_with_context_mut<C: ContextWithMutableVariables>(
4✔
351
    string: &str,
352
    context: &mut C,
353
) -> EvalexprResult<EmptyType, C::NumericTypes> {
354
    match eval_with_context_mut(string, context) {
4✔
355
        Ok(Value::Empty) => Ok(EMPTY_VALUE),
4✔
356
        Ok(value) => Err(EvalexprError::expected_empty(value)),
1✔
357
        Err(error) => Err(error),
3✔
358
    }
359
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc