10 releases

Uses old Rust 2015

0.4.3 Aug 24, 2018
0.4.2 Jan 17, 2018
0.4.1 Dec 6, 2017
0.4.0 Feb 13, 2017
0.1.1 Nov 16, 2016

#22 in #expression-evaluator

Download history 274/week @ 2023-12-16 181/week @ 2023-12-23 160/week @ 2023-12-30 282/week @ 2024-01-06 330/week @ 2024-01-13 239/week @ 2024-01-20 234/week @ 2024-01-27 181/week @ 2024-02-03 219/week @ 2024-02-10 340/week @ 2024-02-17 202/week @ 2024-02-24 254/week @ 2024-03-02 281/week @ 2024-03-09 355/week @ 2024-03-16 294/week @ 2024-03-23 393/week @ 2024-03-30

1,352 downloads per month
Used in 13 crates (12 directly)

MIT license

76KB
2K SLoC

eval

Project Status: Active - The project has reached a stable, usable state and is being actively developed. docs

Eval is a powerful expression evaluator.

Document

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]
eval = "^0.4"

In your main.rs or lib.rs:

extern crate eval;

Examples

You can do mathematical calculations with supported operators:

use eval::{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 eval::{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 eval::{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 eval::{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 eval::{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 eval::{eval, to_value};

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

License

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

Dependencies

~0.4–0.8MB
~18K SLoC