eval

docs

Eval is a powerful expression evaluator.

Document

Features

Supported operators: ! != "" '' () [] , > < >= <= == + - * / % && || n..m.

Built-in functions: min() max() len() is_empty().

Where can eval be used?

Usage

Add dependency to Cargo.toml

toml [dependencies] eval = "^0.3"

In your main.rs or lib.rs:

rust extern crate eval;

Examples

You can do mathematical calculations with supported operators:

``` use eval::{eval, to_value};

asserteq!(eval("1 + 2 + 3"), Ok(tovalue(6))); asserteq!(eval("2 * 2 + 3"), Ok(tovalue(7))); asserteq!(eval("2 / 2 + 3"), Ok(tovalue(4.0))); asserteq!(eval("2 / 2 + 3 / 3"), Ok(tovalue(2.0))); ```

You can eval with context:

``` use eval::{Expr, to_value};

asserteq!(Expr::new("foo == bar") .value("foo", true) .value("bar", true) .exec(), Ok(tovalue(true))); ```

You can access data like javascript by using . and []. [] supports expression.

``` use eval::{Expr, to_value}; use std::collections::HashMap;

let mut object = HashMap::new(); object.insert("foos", vec!["Hello", "world", "!"]);

asserteq!(Expr::new("object.foos[1-1] == 'Hello'") .value("object", object) .exec(), Ok(tovalue(true))); ```

You can eval with function:

``` use eval::{Expr, to_value};

asserteq!(Expr::new("sayhello()") .function("sayhello", || Ok(tovalue("Hello world!"))) .exec(), Ok(tovalue("Hello world!"))); ```

You can create an array with []:

``` use eval::{eval, to_value};

asserteq!(eval("[1, 2, 3, 4, 5]"), Ok(tovalue(vec![1, 2, 3, 4, 5]))); ```

You can create an integer array with n..m:

``` use eval::{eval, to_value};

asserteq!(eval("0..5"), Ok(tovalue(vec![0, 1, 2, 3, 4]))); ```

License

eval is primarily distributed under the terms of the MIT license. See LICENSE for details.