3 releases

0.1.2 Apr 24, 2023
0.1.1 Jul 26, 2022
0.1.0 Jul 16, 2022

#4 in #condition

Download history 12/week @ 2023-02-10 19/week @ 2023-02-17 11/week @ 2023-02-24 10/week @ 2023-03-03 9/week @ 2023-03-10 9/week @ 2023-03-17 5/week @ 2023-03-24 15/week @ 2023-03-31 20/week @ 2023-04-07 7/week @ 2023-04-14 27/week @ 2023-04-21 19/week @ 2023-04-28 19/week @ 2023-05-05 8/week @ 2023-05-12 14/week @ 2023-05-19 13/week @ 2023-05-26

56 downloads per month
Used in 2 crates

GPL-3.0-only

81KB
2K SLoC

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?

  • Template engine
  • ...

Usage

Add dependency to Cargo.toml

[dependencies]
resolver = "^0.1"

In your main.rs or lib.rs:

extern crate resolver;

Examples

You can do mathematical calculations with supported operators:

use resolver::{eval, to_value};

assert_eq!(eval("1 + 2 + 3"), Ok(to_value(6)));
assert_eq!(eval("2 * 2 + 3"), Ok(to_value(7)));
assert_eq!(eval("2 / 2 + 3"), Ok(to_value(4.0)));
assert_eq!(eval("2 / 2 + 3 / 3"), Ok(to_value(2.0)));

You can eval with context:

use resolver::{Expr, to_value};

assert_eq!(Expr::new("foo == bar")
               .value("foo", true)
               .value("bar", true)
               .exec(),
           Ok(to_value(true)));

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

use resolver::{Expr, to_value};
use std::collections::HashMap;

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

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

You can eval with function:

use resolver::{Expr, to_value};

assert_eq!(Expr::new("say_hello()")
               .function("say_hello", |_| Ok(to_value("Hello world!")))
               .exec(),
           Ok(to_value("Hello world!")));

You can create an array with array():

use resolver::{eval, to_value};

assert_eq!(eval("array(1, 2, 3, 4, 5)"), Ok(to_value(vec![1, 2, 3, 4, 5])));

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

use resolver::{eval, to_value};

assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));

License

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

See LICENSE for details.

Dependencies

~0.9–1.6MB
~37K SLoC