resolver

Source

This work is a derivative of this repository: https://github.com/fengcen/eval

The aforementioned repository has been abandoned, hence the reason for this repository/crate.

Features

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

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

Where can eval be used?

Usage

Add dependency to Cargo.toml

toml [dependencies] resolver = "^0.1"

In your main.rs or lib.rs:

rust extern crate resolver;

Examples

You can do mathematical calculations with supported operators:

```rust use resolver::{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:

```rust use resolver::{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.

```rust use resolver::{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:

```rust use resolver::{Expr, to_value};

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

You can create an array with array():

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

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

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

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

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

License

resolver is under the terms of the GPL-3.0-only license.

See LICENSE for details.