Crate eval [−] [src]
Eval is a powerful expression evaluator.
Supported operators: !
!=
""
''
()
[]
,
>
<
>=
<=
==
+
-
*
/
%
&&
||
n..m
.
Built-in functions: min()
max()
len()
is_empty()
.
Examples
You can do mathematical calculations with supported operators:
use eval::{eval, to_value}; assert_eq!(eval("1 + 2 + 3"), Ok(to_value(6))); assert_eq!(eval("2 * 2 + 3"), Ok(to_value(7))); assert_eq!(eval("2 / 2 + 3"), Ok(to_value(4.0))); assert_eq!(eval("2 / 2 + 3 / 3"), Ok(to_value(2.0)));
You can eval with context:
use eval::{Expr, to_value}; assert_eq!(Expr::new("foo == bar") .value("foo", true) .value("bar", true) .exec(), Ok(to_value(true)));
You can eval with function:
use eval::{Expr, to_value}; assert_eq!(Expr::new("say_hello()") .function("say_hello", |_| Ok(to_value("Hello world!"))) .exec(), Ok(to_value("Hello world!")));
You can create an array with []
:
use eval::{eval, to_value}; assert_eq!(eval("[1, 2, 3, 4, 5]"), Ok(to_value(vec![1, 2, 3, 4, 5])));
You can create an integer array with n..m
:
use eval::{eval, to_value}; assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));
Built-in functions
min()
Accept multiple arguments and return the minimum value.
max()
Accept multiple arguments and return the maximum value.
len()
Accept single arguments and return the length of value. Only accept String, Array, Object and Null.
is_empty()
Accept single arguments and return the a boolean. Check whether the value is empty or not.
Structs
ExecOptions |
Execute options |
Expr |
Expression builder |
Function |
Custom function |
Enums
Error |
Expression parsing error |
Value |
Represents a JSON value |
Functions
eval |
Evaluates the value of an expression. |
to_value |
Shortcut function to encode a |
Type Definitions
Context |
Custom context. |
Contexts |
Custom contexts. The value of the last context is searched first. |
Functions |
Custom functions. |