Document the shortcut functions for direct evaluation into types

Related to #15
This commit is contained in:
Sebastian Schmidt 2019-03-20 17:06:47 +02:00
parent c980e49014
commit d4359d71bf
2 changed files with 35 additions and 0 deletions

View File

@ -20,6 +20,10 @@
//! use evalexpr::*; //! use evalexpr::*;
//! //!
//! assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6))); //! assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
//! // `eval` returns a variant of the `Value` enum,
//! // while `eval_[type]` returns the respective type directly.
//! // Both can be used interchangeably.
//! assert_eq!(eval_int("1 + 2 + 3"), Ok(6));
//! assert_eq!(eval("1 - 2 * 3"), Ok(Value::from(-5))); //! assert_eq!(eval("1 - 2 * 3"), Ok(Value::from(-5)));
//! assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from(7.0))); //! assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from(7.0)));
//! assert_eq!(eval("true && 4 > 2"), Ok(Value::from(true))); //! assert_eq!(eval("true && 4 > 2"), Ok(Value::from(true)));
@ -55,6 +59,10 @@
//! }))); //! })));
//! //!
//! assert_eq!(eval_with_configuration("five + 8 > f(twelve)", &configuration), Ok(Value::from(true))); //! assert_eq!(eval_with_configuration("five + 8 > f(twelve)", &configuration), Ok(Value::from(true)));
//! // `eval_with_configuration` returns a variant of the `Value` enum,
//! // while `eval_[type]_with_configuration` returns the respective type directly.
//! // Both can be used interchangeably.
//! assert_eq!(eval_boolean_with_configuration("five + 8 > f(twelve)", &configuration), Ok(true));
//! assert_eq!(eval_with_configuration("avg(2, 4) == 3", &configuration), Ok(Value::from(true))); //! assert_eq!(eval_with_configuration("avg(2, 4) == 3", &configuration), Ok(Value::from(true)));
//! ``` //! ```
//! //!
@ -73,6 +81,10 @@
//! //!
//! configuration.insert_variable("c", 8); //! configuration.insert_variable("c", 8);
//! assert_eq!(precompiled.eval_with_configuration(&configuration), Ok(Value::from(false))); //! assert_eq!(precompiled.eval_with_configuration(&configuration), Ok(Value::from(false)));
//! // `Node::eval_with_configuration` returns a variant of the `Value` enum,
//! // while `Node::eval_[type]_with_configuration` returns the respective type directly.
//! // Both can be used interchangeably.
//! assert_eq!(precompiled.eval_boolean_with_configuration(&configuration), Ok(false));
//! ``` //! ```
//! //!
//! ## Features //! ## Features

View File

@ -364,4 +364,27 @@ fn test_shortcut_functions() {
eval_tuple_with_configuration("3,3", &configuration), eval_tuple_with_configuration("3,3", &configuration),
Ok(vec![Value::Int(3), Value::Int(3)]) Ok(vec![Value::Int(3), Value::Int(3)])
); );
// assert_eq!(build_operator_tree("???").unwrap().eval_string());
assert_eq!(
build_operator_tree("string").unwrap().eval_string_with_configuration(&configuration),
Ok("a string".to_string())
);
assert_eq!(build_operator_tree("3.3").unwrap().eval_float(), Ok(3.3));
assert_eq!(
build_operator_tree("3.3").unwrap().eval_float_with_configuration(&configuration),
Ok(3.3)
);
assert_eq!(build_operator_tree("3").unwrap().eval_int(), Ok(3));
assert_eq!(build_operator_tree("3").unwrap().eval_int_with_configuration(&configuration), Ok(3));
assert_eq!(build_operator_tree("true").unwrap().eval_boolean(), Ok(true));
assert_eq!(
build_operator_tree("true").unwrap().eval_boolean_with_configuration(&configuration),
Ok(true)
);
assert_eq!(build_operator_tree("3,3").unwrap().eval_tuple(), Ok(vec![Value::Int(3), Value::Int(3)]));
assert_eq!(
build_operator_tree("3,3").unwrap().eval_tuple_with_configuration(&configuration),
Ok(vec![Value::Int(3), Value::Int(3)])
);
} }