[−][src]Crate v_eval
v_eval
Evaluate some expresions with context
All are option by default
use v_eval::{Value, Eval}; let e = Eval::default() .insert("foo", "true")? .insert("string", "\"foo\"")? .insert("opt", "true")? .insert("bar", "false")?; assert_eq!(e.eval("foo != bar").unwrap(), Value::Bool(true)); assert_eq!( e.eval("true && foo != bar && true").unwrap(), Value::Bool(true) ); assert_eq!(e.eval("1.5.trunc()").unwrap(), Value::Int(1)); assert_eq!(e.eval("50.log10().trunc() == 1").unwrap(), Value::Bool(true)); assert_eq!(e.eval("1.log10()").unwrap(), Value::Float(1.0f64.log10()));
Methods
By default
Option
and
assert_eq!(e.eval("foo.and(bar)").unwrap(), Value::Bool(false)); assert_eq!(e.eval("not_exist.and(bar)"), None); assert_eq!(e.eval("1.and(2.0)").unwrap(), Value::Float(2.0));
is_none
assert_eq!(e.eval("foo.is_none()").unwrap(), Value::Bool(false)); assert_eq!(e.eval("not_exist.is_none()").unwrap(), Value::Bool(true));
is_some
assert_eq!(e.eval("foo.is_some()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("not_exist.is_some()").unwrap(), Value::Bool(false));
or
assert_eq!(e.eval("foo.or(bar)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("None.or(bar)").unwrap(), Value::Bool(false)); assert_eq!(e.eval("None.or(not_exist)"), None);
xor
assert_eq!(e.eval("not_exist.xor(opt)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("not_exist.xor(foo)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("bar.xor(None)").unwrap(), Value::Bool(false)); assert_eq!(e.eval("bar.xor(foo)"), None);
Dynamic type
is_bool
assert_eq!(e.eval("foo.is_bool()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("string.is_bool()").unwrap(), Value::Bool(false));
is_float
assert_eq!(e.eval("1.0.is_float()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("bar.is_float()").unwrap(), Value::Bool(false));
is_int
assert_eq!(e.eval("1.is_int()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("foo.is_int()").unwrap(), Value::Bool(false));
is_range
assert_eq!(e.eval("(0..10).is_range()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("bar.is_range()").unwrap(), Value::Bool(false));
is_str
assert_eq!(e.eval("foo.is_str()").unwrap(), Value::Bool(false)); assert_eq!(e.eval("string.is_str()").unwrap(), Value::Bool(true));
is_vec
assert_eq!(e.eval("[1, 3, 4.0, true, foo].is_vec()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("foo.is_vec()").unwrap(), Value::Bool(false));
is_same
assert_eq!(e.eval("foo.is_same(bar)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("foo.is_same(false)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("foo.is_same(1)").unwrap(), Value::Bool(false));
Slice (Str and Vec)
len
assert_eq!(e.eval("string.len()").unwrap(), Value::Int(3)); assert_eq!(e.eval("[1, 2, 3, 4].len()").unwrap(), Value::Int(4));
is_empty
assert_eq!(e.eval("string.is_empty()").unwrap(), Value::Bool(false)); assert_eq!(e.eval("[].is_empty()").unwrap(), Value::Bool(true)); assert_eq!(e.eval("(0..0).is_empty()").unwrap(), Value::Bool(true));
contains
assert_eq!(e.eval("string.contains(\"oo\")").unwrap(), Value::Bool(true)); assert_eq!(e.eval("[4, 1.0, true, \"foo\"].contains(1)").unwrap(), Value::Bool(true)); assert_eq!(e.eval("(0..20).contains(0)").unwrap(), Value::Bool(true));
starts_with
assert_eq!(e.eval("string.starts_with(\"fo\")").unwrap(), Value::Bool(true)); assert_eq!(e.eval("[1, 2, foo].starts_with([1])").unwrap(), Value::Bool(true)); assert_eq!(e.eval("[not_exist, bar, foo].starts_with([None, false])").unwrap(), Value::Bool(true));
ends_with
assert_eq!(e.eval("string.ends_with(\"oo\")").unwrap(), Value::Bool(true)); assert_eq!(e.eval("[1, 2, foo].ends_with([true])").unwrap(), Value::Bool(true)); assert_eq!(e.eval("[not_exist, bar, foo].ends_with([false, true])").unwrap(), Value::Bool(true));
Str
eq_ignore_ascii_case
assert_eq!(e.eval(r#"string.eq_ignore_ascii_case("FOO")"#).unwrap(), Value::Bool(true));
find
assert_eq!(e.eval(r#"string.find("o")"#).unwrap(), Value::Int(1));
is_ascii
assert_eq!(e.eval("string.is_ascii()").unwrap(), Value::Bool(true)); assert_eq!(e.eval(r#""Grüße, Jürgen ❤".is_ascii()"#).unwrap(), Value::Bool(false));
is_match
assert_eq!(e.eval(r#""2020-04-28".is_match(r"^\d{4}-\d{2}-\d{2}$")"#).unwrap(), Value::Bool(true)); assert_eq!(e.eval(r#"string.is_match(r"^\d{4}-\d{2}-\d{2}$")"#).unwrap(), Value::Bool(false));
to_ascii_lowercase
assert_eq!(e.eval(r#""FOO".to_ascii_lowercase() == string"#).unwrap(), Value::Bool(true));
to_ascii_uppercase
assert_eq!(e.eval(r#"string.to_ascii_uppercase() == "FOO""#).unwrap(), Value::Bool(true));
to_lowercase
assert_eq!(e.eval(r#""FOO".to_lowercase() == string"#).unwrap(), Value::Bool(true));
to_uppercase
assert_eq!(e.eval(r#"string.to_uppercase() == "FOO""#).unwrap(), Value::Bool(true));
trim
assert_eq!(e.eval(r#"" foo ".trim() == string"#).unwrap(), Value::Bool(true));
trim_end
assert_eq!(e.eval(r#""foo ".trim_end() == string"#).unwrap(), Value::Bool(true));
trim_start
assert_eq!(e.eval(r#"" foo".trim_start() == string"#).unwrap(), Value::Bool(true));
rfind
assert_eq!(e.eval(r#"string.rfind("o")"#).unwrap(), Value::Int(2));
Vec
first
assert_eq!(e.eval(r#"[1, 2, 3].first()"#).unwrap(), Value::Int(1));
get
assert_eq!(e.eval(r#"[1, 2].get(1)"#).unwrap(), Value::Int(2)); assert_eq!(e.eval(r#"[1, 2].get(2)"#), None);
last
assert_eq!(e.eval(r#"[1, 2].last()"#).unwrap(), Value::Int(2));
Number (i64 and f64)
See f64 Rust
abs
acos
acosh
asin
asinh
atan2
atan
atanh
cbrt
ceil
cos
cosh
exp2
exp_m1
exp
floor
fract
hypot
ln_1p
ln
log10
log2
log
max
min
powf
powi
recip
round
signum
sin
sinh
sqrt
tan
tanh
to_degrees
to_radians
trunc
assert_eq!(e.eval("1.5.trunc()").unwrap(), Value::Int(1));
Structs
Eval | Evaluator with context |
Enums
Value | Wrapper for value implements simple operations and check types |
Functions
eval |