Update readme

Relates to #50
This commit is contained in:
Sebastian Schmidt 2019-08-29 09:12:42 +03:00
parent 6b1706f68f
commit 8c6228c3d9

View File

@ -62,23 +62,21 @@ And you can use **variables** and **functions** in expressions like this:
```rust ```rust
use evalexpr::*; use evalexpr::*;
use evalexpr::error::expect_number;
let context = context_map! { let context = context_map! {
"five" => 5, "five" => 5,
"twelve" => 12, "twelve" => 12,
"f" => Function::new(Some(1) /* argument amount */, Box::new(|arguments| { "f" => Function::new(Box::new(|argument| {
if let Value::Int(int) = arguments[0] { if let Ok(int) = argument.as_int() {
Ok(Value::Int(int / 2)) Ok(Value::Int(int / 2))
} else if let Value::Float(float) = arguments[0] { } else if let Ok(float) = argument.as_float() {
Ok(Value::Float(float / 2.0)) Ok(Value::Float(float / 2.0))
} else { } else {
Err(EvalexprError::expected_number(arguments[0].clone())) Err(EvalexprError::expected_number(argument.clone()))
} }
})), })),
"avg" => Function::new(Some(2) /* argument amount */, Box::new(|arguments| { "avg" => Function::new(Box::new(|argument| {
expect_number(&arguments[0])?; let arguments = argument.as_tuple()?;
expect_number(&arguments[1])?;
if let (Value::Int(a), Value::Int(b)) = (&arguments[0], &arguments[1]) { if let (Value::Int(a), Value::Int(b)) = (&arguments[0], &arguments[1]) {
Ok(Value::Int((a + b) / 2)) Ok(Value::Int((a + b) / 2))
@ -342,7 +340,6 @@ The implementation expects a [serde `string`](https://serde.rs/data-model.html)
Example parsing with [ron format](docs.rs/ron): Example parsing with [ron format](docs.rs/ron):
```rust ```rust
# #[cfg(feature = "serde_support")] {
extern crate ron; extern crate ron;
use evalexpr::*; use evalexpr::*;
@ -356,9 +353,8 @@ match ron::de::from_str::<Node>(serialized_free) {
Ok(free) => assert_eq!(free.eval_with_context(&context), Ok(Value::from(25))), Ok(free) => assert_eq!(free.eval_with_context(&context), Ok(Value::from(25))),
Err(error) => { Err(error) => {
() // Handle error () // Handle error
}, }
} }
# }
``` ```
With `serde`, expressions can be integrated into arbitrarily complex data. With `serde`, expressions can be integrated into arbitrarily complex data.