Use context_map macro in lib.rs documentation as well

Should have used `cargo sync-readme` to begin with, but that was not
documented at the time of this pull request.
This commit is contained in:
Sebastian Schmidt 2019-04-26 17:33:59 +02:00
parent 3d1b0696cb
commit bc826e5bf2
2 changed files with 58 additions and 54 deletions

View File

@ -86,7 +86,7 @@ let context = context_map! {
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(); }.unwrap(); // Do proper error handling here
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,
@ -107,7 +107,7 @@ let mut context = context_map! {
"a" => 6, "a" => 6,
"b" => 2, "b" => 2,
"c" => 3 "c" => 3
}.unwrap(); }.unwrap(); // Do proper error handling here
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
@ -345,8 +345,9 @@ Example parsing with [ron format](docs.rs/ron):
extern crate ron; extern crate ron;
use evalexpr::*; use evalexpr::*;
let mut context = HashMapContext::new(); let mut context = context_map!{
context.set_value("five".into(), 5.into()).unwrap(); // Do proper error handling here "five" => 5
}.unwrap(); // Do proper error handling here
// In ron format, strings are surrounded by " // In ron format, strings are surrounded by "
let serialized_free = "\"five * five\""; let serialized_free = "\"five * five\"";

View File

@ -51,10 +51,10 @@
//! 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] {
@ -62,8 +62,8 @@
//! } 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])?;
//! //!
@ -72,7 +72,8 @@
//! } 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(); // Do proper error handling here
//! //!
//! 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,
@ -89,10 +90,11 @@
//! //!
//! 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(); // Do proper error handling here
//! 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
@ -330,8 +332,9 @@
//! extern crate ron; //! extern crate ron;
//! use evalexpr::*; //! use evalexpr::*;
//! //!
//! let mut context = HashMapContext::new(); //! let mut context = context_map!{
//! context.set_value("five".into(), 5.into()).unwrap(); // Do proper error handling here //! "five" => 5
//! }.unwrap(); // Do proper error handling here
//! //!
//! // In ron format, strings are surrounded by " //! // In ron format, strings are surrounded by "
//! let serialized_free = "\"five * five\""; //! let serialized_free = "\"five * five\"";