2019-03-15 13:12:18 +02:00
evalexpr
========
2017-02-25 14:51:55 +08:00
2019-03-15 13:12:18 +02:00
**This is a fork of the eval crate originally created by fengcen and then abandoned.**
2019-01-19 09:16:10 +08:00
2019-03-15 13:12:18 +02:00
<!-- [](https://www.repostatus.org/ #abandoned ) -->
2016-11-17 00:31:52 +08:00
2019-03-15 13:12:18 +02:00
[](https://docs.rs/evalexpr)
2016-11-17 00:12:26 +08:00
2019-03-15 13:12:18 +02:00
Evalexpr is a powerful expression evaluator.
[Document ](https://docs.rs/evalexpr )
2017-02-25 14:51:55 +08:00
--------------------------------
Features
--------
2016-11-17 00:31:52 +08:00
2016-11-17 00:12:26 +08:00
Supported operators: `!` `!=` `""` `''` `()` `[]` `,` `>` `<` `>=` `<=` `==`
2019-03-15 13:42:18 +02:00
`+` unary/binary `-` `*` `/` `%` `&&` `||` `n..m` .
2016-11-17 00:12:26 +08:00
2019-03-15 12:04:01 +02:00
Built-in functions: `min()` `max()` `len()` `is_empty()` `array()` `converge()` .
See the `builtin` module for a detailed description of each.
2016-11-17 00:12:26 +08:00
2017-02-25 14:51:55 +08:00
Where can eval be used?
-----------------------
2016-11-17 00:12:26 +08:00
* Template engine
2019-03-15 13:12:18 +02:00
* Scripting language
2016-11-17 00:12:26 +08:00
* ...
2017-02-25 14:51:55 +08:00
Usage
-----
2016-11-17 00:12:26 +08:00
Add dependency to Cargo.toml
```toml
[dependencies]
2019-03-15 13:12:18 +02:00
evalexpr = "0.4"
2016-11-17 00:12:26 +08:00
```
In your `main.rs` or `lib.rs` :
```rust
2019-03-15 13:12:18 +02:00
extern crate evalexpr as eval;
2016-11-17 00:12:26 +08:00
```
2017-02-25 14:51:55 +08:00
Examples
--------
2016-11-17 00:12:26 +08:00
You can do mathematical calculations with supported operators:
2017-02-25 14:51:55 +08:00
```rust
2016-11-17 00:12:26 +08:00
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:
2017-02-25 14:51:55 +08:00
```rust
2016-11-20 16:04:06 +08:00
use eval::{Expr, to_value};
2016-11-17 00:12:26 +08:00
2016-11-20 16:04:06 +08:00
assert_eq!(Expr::new("foo == bar")
.value("foo", true)
.value("bar", true)
.exec(),
Ok(to_value(true)));
2016-11-17 00:12:26 +08:00
```
2016-11-21 09:14:40 +08:00
You can access data like javascript by using `.` and `[]` . `[]` supports expression.
2017-02-25 14:51:55 +08:00
```rust
2016-11-21 09:14:40 +08:00
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)));
```
2016-11-20 16:04:06 +08:00
You can eval with function:
2016-11-17 00:12:26 +08:00
2017-02-25 14:51:55 +08:00
```rust
2016-11-20 16:04:06 +08:00
use eval::{Expr, to_value};
2016-11-17 00:12:26 +08:00
2016-11-20 16:04:06 +08:00
assert_eq!(Expr::new("say_hello()")
.function("say_hello", |_| Ok(to_value("Hello world!")))
.exec(),
Ok(to_value("Hello world!")));
2016-11-17 00:12:26 +08:00
```
2016-11-25 20:37:07 +08:00
You can create an array with `array()` :
2016-11-17 00:12:26 +08:00
2017-02-25 14:51:55 +08:00
```rust
2016-11-17 00:12:26 +08:00
use eval::{eval, to_value};
2016-11-25 20:37:07 +08:00
assert_eq!(eval("array(1, 2, 3, 4, 5)"), Ok(to_value(vec![1, 2, 3, 4, 5])));
2016-11-17 00:12:26 +08:00
```
You can create an integer array with `n..m` :
2017-02-25 14:51:55 +08:00
```rust
2016-11-17 00:12:26 +08:00
use eval::{eval, to_value};
assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));
```
2017-02-25 14:51:55 +08:00
License
-------
2019-03-15 13:12:18 +02:00
evalexpr is primarily distributed under the terms of the MIT license.
2016-11-17 00:12:26 +08:00
See [LICENSE ](LICENSE ) for details.