evalx 0.5.0

Expression evaluator
Documentation
evalx
=====

[![CI](https://github.com/nlippke/evalx/actions/workflows/ci.yml/badge.svg)](https://github.com/nlippke/evalx/actions/workflows/ci.yml)

[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)

[![docs](https://docs.rs/evalx/badge.svg "docs")](https://docs.rs/evalx)

Evalx is a powerful expression evaluator.

This crate is a maintained fork and further development of the original
[eval by fengcen](https://github.com/fengcen/eval). The continuation mainly
includes bugfixes and updating the Rust edition to 2021, while keeping the
public API compatible.

[Documentation](https://docs.rs/evalx)
--------------------------------------

Features
--------

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

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

Where can evalx be used?
------------------------

* Template engine
* ...

Usage
-----

Add dependency to Cargo.toml

```toml
[dependencies]
evalx = "^0.5"
```

In your code:

```rust
use evalx::{eval, Expr, to_value};
```

Examples
--------

You can do mathematical calculations with supported operators:

```rust
use evalx::{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 / 3"), Ok(to_value(2.0)));
```

You can eval with context:

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

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

```rust
use evalx::{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()`:

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

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

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

Linting (Clippy)
----------------

This repository is set up with Clippy for linting.

- Install Clippy (if needed):
  - `rustup component add clippy`
- Run Clippy normally:
  - `cargo clippy`
- Strict mode (treat all warnings as errors):
  - `cargo clippy-strict`
- Pedantic mode (more lints, as warnings):
  - `cargo clippy-pedantic`
- CI: Clippy runs in GitHub Actions (see `.github/workflows/ci.yml`).

License
-------

evalx is primarily distributed under the terms of the MIT license.
See [LICENSE](LICENSE) for details.