[][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", "Some(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("Some(1.log10()).unwrap()").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);
  • unwrap_or
assert_eq!(e.eval("not_exist.unwrap_or(bar)").unwrap(), Value::Bool(false));
assert_eq!(e.eval("foo.unwrap_or(bar)").unwrap(), Value::Bool(true));
assert_eq!(e.eval("None.unwrap_or(opt)").unwrap(), Value::Bool(true));
  • unwrap
assert_eq!(e.eval("foo.unwrap()").unwrap(), Value::Bool(true));
assert_eq!(e.eval("not_exist.unwrap()"), None);
assert_eq!(e.eval("None.unwrap()"), 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_option
assert_eq!(e.eval("None.is_option()").unwrap(), Value::Bool(true));
assert_eq!(e.eval("not_exist.is_option()").unwrap(), Value::Bool(true));
assert_eq!(e.eval("bar.is_option()").unwrap(), Value::Bool(false));
assert_eq!(e.eval("opt.is_option()").unwrap(), Value::Bool(true));
  • 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));

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