Use the new context macro in the README examples

This commit is contained in:
Ophir LOJKINE 2019-04-17 17:24:07 +02:00
parent 0eaeb3f6ab
commit 3d1b0696cb

View File

@ -64,28 +64,29 @@ And you can use **variables** and **functions** in expressions like this:
use evalexpr::*; use evalexpr::*;
use evalexpr::error::expect_number; use evalexpr::error::expect_number;
let mut context = HashMapContext::new(); let context = context_map! {
context.set_value("five".into(), 5.into()).unwrap(); // Do proper error handling here "five" => 5,
context.set_value("twelve".into(), 12.into()).unwrap(); // Do proper error handling here "twelve" => 12,
context.set_function("f".into(), Function::new(Some(1) /* argument amount */, Box::new(|arguments| { "f" => Function::new(Some(1) /* argument amount */, Box::new(|arguments| {
if let Value::Int(int) = arguments[0] { if let Value::Int(int) = arguments[0] {
Ok(Value::Int(int / 2)) Ok(Value::Int(int / 2))
} else if let Value::Float(float) = arguments[0] { } else if let Value::Float(float) = arguments[0] {
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(arguments[0].clone()))
} }
}))).unwrap(); // Do proper error handling here })),
context.set_function("avg".into(), Function::new(Some(2) /* argument amount */, Box::new(|arguments| { "avg" => Function::new(Some(2) /* argument amount */, Box::new(|arguments| {
expect_number(&arguments[0])?; expect_number(&arguments[0])?;
expect_number(&arguments[1])?; 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))
} else { } else {
Ok(Value::Float((arguments[0].as_number()? + arguments[1].as_number()?) / 2.0)) Ok(Value::Float((arguments[0].as_number()? + arguments[1].as_number()?) / 2.0))
} }
}))).unwrap(); // Do proper error handling here }))
}.unwrap();
assert_eq!(eval_with_context("five + 8 > f(twelve)", &context), Ok(Value::from(true))); assert_eq!(eval_with_context("five + 8 > f(twelve)", &context), Ok(Value::from(true)));
// `eval_with_context` returns a variant of the `Value` enum, // `eval_with_context` returns a variant of the `Value` enum,
@ -102,10 +103,11 @@ use evalexpr::*;
let precompiled = build_operator_tree("a * b - c > 5").unwrap(); // Do proper error handling here let precompiled = build_operator_tree("a * b - c > 5").unwrap(); // Do proper error handling here
let mut context = HashMapContext::new(); let mut context = context_map! {
context.set_value("a".into(), 6.into()).unwrap(); // Do proper error handling here "a" => 6,
context.set_value("b".into(), 2.into()).unwrap(); // Do proper error handling here "b" => 2,
context.set_value("c".into(), 3.into()).unwrap(); // Do proper error handling here "c" => 3
}.unwrap();
assert_eq!(precompiled.eval_with_context(&context), Ok(Value::from(true))); assert_eq!(precompiled.eval_with_context(&context), Ok(Value::from(true)));
context.set_value("c".into(), 8.into()).unwrap(); // Do proper error handling here context.set_value("c".into(), 8.into()).unwrap(); // Do proper error handling here