From 6ace8291175258c1b19f4c57d5cd47042787f3a7 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 29 Aug 2019 09:44:14 +0300 Subject: [PATCH] Create mutable context when using eval functions without context While this is a tiny hit on performance, it is something that the user probably wants. It specifically prevents the user from seeing ContextNotManipulable errors when using the full power of evalexpr in the simplest eval calls. Implements #45 --- src/interface/mod.rs | 19 +++++++++---------- tests/integration.rs | 15 ++++++++++++--- tests/serde.rs | 2 ++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/interface/mod.rs b/src/interface/mod.rs index 2e59209..fe1b073 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -1,8 +1,7 @@ -use crate::token; +use crate::{token, HashMapContext}; use crate::tree; use crate::value::TupleType; use crate::Context; -use crate::EmptyContext; use crate::EmptyType; use crate::EvalexprError; use crate::EvalexprResult; @@ -24,7 +23,7 @@ use crate::EMPTY_VALUE; /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval(string: &str) -> EvalexprResult { - eval_with_context(string, &EmptyContext) + eval_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string with the given context. @@ -97,21 +96,21 @@ pub fn build_operator_tree(string: &str) -> EvalexprResult { /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_string(string: &str) -> EvalexprResult { - eval_string_with_context(string, &EmptyContext) + eval_string_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into an integer. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_int(string: &str) -> EvalexprResult { - eval_int_with_context(string, &EmptyContext) + eval_int_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into a float. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_float(string: &str) -> EvalexprResult { - eval_float_with_context(string, &EmptyContext) + eval_float_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into a float. @@ -119,28 +118,28 @@ pub fn eval_float(string: &str) -> EvalexprResult { /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_number(string: &str) -> EvalexprResult { - eval_number_with_context(string, &EmptyContext) + eval_number_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into a boolean. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_boolean(string: &str) -> EvalexprResult { - eval_boolean_with_context(string, &EmptyContext) + eval_boolean_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into a tuple. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_tuple(string: &str) -> EvalexprResult { - eval_tuple_with_context(string, &EmptyContext) + eval_tuple_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into an empty value. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* pub fn eval_empty(string: &str) -> EvalexprResult { - eval_empty_with_context(string, &EmptyContext) + eval_empty_with_context_mut(string, &mut HashMapContext::new()) } /// Evaluate the given expression string into a string with the given context. diff --git a/tests/integration.rs b/tests/integration.rs index 5d7012b..2e6d719 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -2,9 +2,6 @@ extern crate evalexpr; use evalexpr::{error::*, *}; -#[cfg(feature = "serde")] -mod serde; - #[test] fn test_unary_examples() { assert_eq!(eval("3"), Ok(Value::Int(3))); @@ -639,3 +636,15 @@ fn test_tuple_definitions() { Ok(vec![Value::from(1), Value::from(2)]) ); } + +#[test] +fn test_implicit_context() { + assert_eq!(eval("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(Value::from(true))); + assert_eq!(eval_boolean("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(true)); + assert_eq!(eval_int("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0)); + assert_eq!(eval_float("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b + 0.5"), Ok(0.5)); + assert_eq!(eval_number("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0.0)); + assert_eq!(eval_empty("a = 2 + 4 * 2; b = -5 + 3 * 5;"), Ok(())); + assert_eq!(eval_tuple("a = 2 + 4 * 2; b = -5 + 3 * 5; a, b + 0.5"), Ok(vec![Value::from(10), Value::from(10.5)])); + assert_eq!(eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"), Ok("xyzabc".to_string())); +} \ No newline at end of file diff --git a/tests/serde.rs b/tests/serde.rs index bb261d1..77af185 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "serde")] + use evalexpr::{build_operator_tree, Node}; #[test]