eval

Expression evaluator for Rust.

Features

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

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

Where can eval be used?

Usage

Add dependency to Cargo.toml

toml [dependencies] eval = "^0.1"

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::{evalwithcontext, Context, to_value};

let mut context = Context::new(); context.insert("foo".toowned(), tovalue(true)); context.insert("bar".toowned(), tovalue(true)); asserteq!(evalwithcontext("foo == bar", &context), Ok(tovalue(true))); ```

You can eval with functions:

``` use eval::{evalwithfunctions, Functions, Function, to_value};

let mut functions = Functions::new(); functions.insert("sayhello".toowned(), Function::new(|| Ok(tovalue("Hello world!")))); asserteq!(evalwithfunctions("sayhello()", &functions), Ok(to_value("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.