expressive/interface/
mod.rs

1use crate::{
2    token, tree, value::TupleType, Context, ContextWithMutableVariables, EmptyType, EvalexprError,
3    EvalexprResult, FloatType, IntType, Node, Value, VariableMap, EMPTY_VALUE,
4};
5
6/// Evaluate the given expression string.
7///
8/// # Examples
9///
10/// ```rust
11/// use evalexpr::*;
12///
13/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
14/// ```
15///
16/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
17pub fn eval(string: &str) -> EvalexprResult<Value> {
18    eval_with_context_mut(string, &mut VariableMap::new())
19}
20
21/// Evaluate the given expression string with the given context.
22///
23/// # Examples
24///
25/// ```rust
26/// use evalexpr::*;
27///
28/// let mut context = HashMapContext::new();
29/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
30/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
31/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
32/// assert_eq!(eval_with_context("one + two + three", &context), Ok(Value::from(6)));
33/// ```
34///
35/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
36pub fn eval_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<Value> {
37    tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
38}
39
40/// Evaluate the given expression string with the given mutable context.
41///
42/// # Examples
43///
44/// ```rust
45/// use evalexpr::*;
46///
47/// let mut context = HashMapContext::new();
48/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
49/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
50/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
51/// assert_eq!(eval_with_context_mut("one + two + three", &mut context), Ok(Value::from(6)));
52/// ```
53///
54/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
55pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
56    string: &str,
57    context: &mut C,
58) -> EvalexprResult<Value> {
59    tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
60}
61
62/// Build the operator tree for the given expression string.
63///
64/// The operator tree can later on be evaluated directly.
65/// This saves runtime if a single expression should be evaluated multiple times, for example with differing contexts.
66///
67/// # Examples
68///
69/// ```rust
70/// use evalexpr::*;
71///
72/// let precomputed = build_operator_tree("one + two + three").unwrap(); // Do proper error handling here
73///
74/// let mut context = HashMapContext::new();
75/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
76/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
77/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
78///
79/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from(6)));
80///
81/// context.set_value("three".into(), 5.into()).unwrap(); // Do proper error handling here
82/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from(8)));
83/// ```
84///
85/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
86pub fn build_operator_tree(string: &str) -> EvalexprResult<Node> {
87    tree::tokens_to_operator_tree(token::tokenize(string)?)
88}
89
90/// Evaluate the given expression string into a string.
91///
92/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
93pub fn eval_string(string: &str) -> EvalexprResult<String> {
94    eval_string_with_context_mut(string, &mut VariableMap::new())
95}
96
97/// Evaluate the given expression string into an integer.
98///
99/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
100pub fn eval_int(string: &str) -> EvalexprResult<IntType> {
101    eval_int_with_context_mut(string, &mut VariableMap::new())
102}
103
104/// Evaluate the given expression string into a float.
105///
106/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
107pub fn eval_float(string: &str) -> EvalexprResult<FloatType> {
108    eval_float_with_context_mut(string, &mut VariableMap::new())
109}
110
111/// Evaluate the given expression string into a float.
112/// If the result of the expression is an integer, it is silently converted into a float.
113///
114/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
115pub fn eval_number(string: &str) -> EvalexprResult<FloatType> {
116    eval_number_with_context_mut(string, &mut VariableMap::new())
117}
118
119/// Evaluate the given expression string into a boolean.
120///
121/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
122pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
123    eval_boolean_with_context_mut(string, &mut VariableMap::new())
124}
125
126/// Evaluate the given expression string into a tuple.
127///
128/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
129pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
130    eval_tuple_with_context_mut(string, &mut VariableMap::new())
131}
132
133/// Evaluate the given expression string into an empty value.
134///
135/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
136pub fn eval_empty(string: &str) -> EvalexprResult<EmptyType> {
137    eval_empty_with_context_mut(string, &mut VariableMap::new())
138}
139
140/// Evaluate the given expression string into a string with the given context.
141///
142/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
143pub fn eval_string_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<String> {
144    match eval_with_context(string, context) {
145        Ok(Value::String(string)) => Ok(string),
146        Ok(value) => Err(EvalexprError::expected_string(value)),
147        Err(error) => Err(error),
148    }
149}
150
151/// Evaluate the given expression string into an integer with the given context.
152///
153/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
154pub fn eval_int_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<IntType> {
155    match eval_with_context(string, context) {
156        Ok(Value::Int(int)) => Ok(int),
157        Ok(value) => Err(EvalexprError::expected_int(value)),
158        Err(error) => Err(error),
159    }
160}
161
162/// Evaluate the given expression string into a float with the given context.
163///
164/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
165pub fn eval_float_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<FloatType> {
166    match eval_with_context(string, context) {
167        Ok(Value::Float(float)) => Ok(float),
168        Ok(value) => Err(EvalexprError::expected_float(value)),
169        Err(error) => Err(error),
170    }
171}
172
173/// Evaluate the given expression string into a float with the given context.
174/// If the result of the expression is an integer, it is silently converted into a float.
175///
176/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
177pub fn eval_number_with_context<C: Context>(
178    string: &str,
179    context: &C,
180) -> EvalexprResult<FloatType> {
181    match eval_with_context(string, context) {
182        Ok(Value::Float(float)) => Ok(float),
183        Ok(Value::Int(int)) => Ok(int as FloatType),
184        Ok(value) => Err(EvalexprError::expected_number(value)),
185        Err(error) => Err(error),
186    }
187}
188
189/// Evaluate the given expression string into a boolean with the given context.
190///
191/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
192pub fn eval_boolean_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<bool> {
193    match eval_with_context(string, context) {
194        Ok(Value::Boolean(boolean)) => Ok(boolean),
195        Ok(value) => Err(EvalexprError::expected_boolean(value)),
196        Err(error) => Err(error),
197    }
198}
199
200/// Evaluate the given expression string into a tuple with the given context.
201///
202/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
203pub fn eval_tuple_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<TupleType> {
204    match eval_with_context(string, context) {
205        Ok(Value::Tuple(tuple)) => Ok(tuple),
206        Ok(value) => Err(EvalexprError::expected_tuple(value)),
207        Err(error) => Err(error),
208    }
209}
210
211/// Evaluate the given expression string into an empty value with the given context.
212///
213/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
214pub fn eval_empty_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<EmptyType> {
215    match eval_with_context(string, context) {
216        Ok(Value::Empty) => Ok(EMPTY_VALUE),
217        Ok(value) => Err(EvalexprError::expected_empty(value)),
218        Err(error) => Err(error),
219    }
220}
221
222/// Evaluate the given expression string into a string with the given mutable context.
223///
224/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
225pub fn eval_string_with_context_mut<C: ContextWithMutableVariables>(
226    string: &str,
227    context: &mut C,
228) -> EvalexprResult<String> {
229    match eval_with_context_mut(string, context) {
230        Ok(Value::String(string)) => Ok(string),
231        Ok(value) => Err(EvalexprError::expected_string(value)),
232        Err(error) => Err(error),
233    }
234}
235
236/// Evaluate the given expression string into an integer with the given mutable context.
237///
238/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
239pub fn eval_int_with_context_mut<C: ContextWithMutableVariables>(
240    string: &str,
241    context: &mut C,
242) -> EvalexprResult<IntType> {
243    match eval_with_context_mut(string, context) {
244        Ok(Value::Int(int)) => Ok(int),
245        Ok(value) => Err(EvalexprError::expected_int(value)),
246        Err(error) => Err(error),
247    }
248}
249
250/// Evaluate the given expression string into a float with the given mutable context.
251///
252/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
253pub fn eval_float_with_context_mut<C: ContextWithMutableVariables>(
254    string: &str,
255    context: &mut C,
256) -> EvalexprResult<FloatType> {
257    match eval_with_context_mut(string, context) {
258        Ok(Value::Float(float)) => Ok(float),
259        Ok(value) => Err(EvalexprError::expected_float(value)),
260        Err(error) => Err(error),
261    }
262}
263
264/// Evaluate the given expression string into a float with the given mutable context.
265/// If the result of the expression is an integer, it is silently converted into a float.
266///
267/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
268pub fn eval_number_with_context_mut<C: ContextWithMutableVariables>(
269    string: &str,
270    context: &mut C,
271) -> EvalexprResult<FloatType> {
272    match eval_with_context_mut(string, context) {
273        Ok(Value::Float(float)) => Ok(float),
274        Ok(Value::Int(int)) => Ok(int as FloatType),
275        Ok(value) => Err(EvalexprError::expected_number(value)),
276        Err(error) => Err(error),
277    }
278}
279
280/// Evaluate the given expression string into a boolean with the given mutable context.
281///
282/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
283pub fn eval_boolean_with_context_mut<C: ContextWithMutableVariables>(
284    string: &str,
285    context: &mut C,
286) -> EvalexprResult<bool> {
287    match eval_with_context_mut(string, context) {
288        Ok(Value::Boolean(boolean)) => Ok(boolean),
289        Ok(value) => Err(EvalexprError::expected_boolean(value)),
290        Err(error) => Err(error),
291    }
292}
293
294/// Evaluate the given expression string into a tuple with the given mutable context.
295///
296/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
297pub fn eval_tuple_with_context_mut<C: ContextWithMutableVariables>(
298    string: &str,
299    context: &mut C,
300) -> EvalexprResult<TupleType> {
301    match eval_with_context_mut(string, context) {
302        Ok(Value::Tuple(tuple)) => Ok(tuple),
303        Ok(value) => Err(EvalexprError::expected_tuple(value)),
304        Err(error) => Err(error),
305    }
306}
307
308/// Evaluate the given expression string into an empty value with the given mutable context.
309///
310/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
311pub fn eval_empty_with_context_mut<C: ContextWithMutableVariables>(
312    string: &str,
313    context: &mut C,
314) -> EvalexprResult<EmptyType> {
315    match eval_with_context_mut(string, context) {
316        Ok(Value::Empty) => Ok(EMPTY_VALUE),
317        Ok(value) => Err(EvalexprError::expected_empty(value)),
318        Err(error) => Err(error),
319    }
320}