diff --git a/src/interface/mod.rs b/src/interface/mod.rs index c5acef2..9e57bb8 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -44,6 +44,25 @@ pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult EvalexprResult { + tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context) +} + /// Build the operator tree for the given expression string. /// /// The operator tree can later on be evaluated directly. @@ -76,33 +95,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 { - match eval(string) { - Ok(Value::String(string)) => Ok(string), - Ok(value) => Err(EvalexprError::expected_string(value)), - Err(error) => Err(error), - } + eval_string_with_context(string, &EmptyContext) } /// 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 { - match eval(string) { - Ok(Value::Int(int)) => Ok(int), - Ok(value) => Err(EvalexprError::expected_int(value)), - Err(error) => Err(error), - } + eval_int_with_context(string, &EmptyContext) } /// 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 { - match eval(string) { - Ok(Value::Float(float)) => Ok(float), - Ok(value) => Err(EvalexprError::expected_float(value)), - Err(error) => Err(error), - } + eval_float_with_context(string, &EmptyContext) } /// Evaluate the given expression string into a float. @@ -110,34 +117,21 @@ 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 { - match eval(string) { - Ok(Value::Float(float)) => Ok(float), - Ok(Value::Int(int)) => Ok(int as FloatType), - Ok(value) => Err(EvalexprError::expected_float(value)), - Err(error) => Err(error), - } + eval_number_with_context(string, &EmptyContext) } /// 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 { - match eval(string) { - Ok(Value::Boolean(boolean)) => Ok(boolean), - Ok(value) => Err(EvalexprError::expected_boolean(value)), - Err(error) => Err(error), - } + eval_boolean_with_context(string, &EmptyContext) } /// 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 { - match eval(string) { - Ok(Value::Tuple(tuple)) => Ok(tuple), - Ok(value) => Err(EvalexprError::expected_tuple(value)), - Err(error) => Err(error), - } + eval_tuple_with_context(string, &EmptyContext) } /// Evaluate the given expression string into a string with the given context. @@ -207,3 +201,80 @@ pub fn eval_tuple_with_context(string: &str, context: &Context) -> EvalexprResul Err(error) => Err(error), } } + +/// Evaluate the given expression string into a string with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_string_with_context_mut(string: &str, context: &mut Context) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::String(string)) => Ok(string), + Ok(value) => Err(EvalexprError::expected_string(value)), + Err(error) => Err(error), + } +} + +/// Evaluate the given expression string into an integer with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_int_with_context_mut(string: &str, context: &mut Context) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Int(int)) => Ok(int), + Ok(value) => Err(EvalexprError::expected_int(value)), + Err(error) => Err(error), + } +} + +/// Evaluate the given expression string into a float with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_float_with_context_mut( + string: &str, + context: &mut Context, +) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Float(float)) => Ok(float), + Ok(value) => Err(EvalexprError::expected_float(value)), + Err(error) => Err(error), + } +} + +/// Evaluate the given expression string into a float with the given mutable context. +/// If the result of the expression is an integer, it is silently converted into a float. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_number_with_context_mut( + string: &str, + context: &mut Context, +) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Float(float)) => Ok(float), + Ok(Value::Int(int)) => Ok(int as FloatType), + Ok(value) => Err(EvalexprError::expected_float(value)), + Err(error) => Err(error), + } +} + +/// Evaluate the given expression string into a boolean with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_boolean_with_context_mut(string: &str, context: &mut Context) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Boolean(boolean)) => Ok(boolean), + Ok(value) => Err(EvalexprError::expected_boolean(value)), + Err(error) => Err(error), + } +} + +/// Evaluate the given expression string into a tuple with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_tuple_with_context_mut( + string: &str, + context: &mut Context, +) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Tuple(tuple)) => Ok(tuple), + Ok(value) => Err(EvalexprError::expected_tuple(value)), + Err(error) => Err(error), + } +} diff --git a/tests/integration.rs b/tests/integration.rs index a979188..28e0dd9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -380,6 +380,7 @@ fn test_shortcut_functions() { .eval_int_with_context(&context), Ok(3) ); + assert_eq!(build_operator_tree("3").unwrap().eval_number(), Ok(3.0)); assert_eq!( build_operator_tree("3") .unwrap() @@ -406,6 +407,60 @@ fn test_shortcut_functions() { .eval_tuple_with_context(&context), Ok(vec![Value::Int(3), Value::Int(3)]) ); + + assert_eq!( + eval_string_with_context_mut("string", &mut context), + Ok("a string".to_string()) + ); + assert_eq!(eval_float_with_context_mut("3.3", &mut context), Ok(3.3)); + assert_eq!(eval_int_with_context_mut("3", &mut context), Ok(3)); + assert_eq!(eval_number_with_context_mut("3", &mut context), Ok(3.0)); + assert_eq!( + eval_boolean_with_context_mut("true", &mut context), + Ok(true) + ); + assert_eq!( + eval_tuple_with_context_mut("3,3", &mut context), + Ok(vec![Value::Int(3), Value::Int(3)]) + ); + + assert_eq!( + build_operator_tree("string") + .unwrap() + .eval_string_with_context_mut(&mut context), + Ok("a string".to_string()) + ); + ; + assert_eq!( + build_operator_tree("3.3") + .unwrap() + .eval_float_with_context_mut(&mut context), + Ok(3.3) + ); + assert_eq!( + build_operator_tree("3") + .unwrap() + .eval_int_with_context_mut(&mut context), + Ok(3) + ); + assert_eq!( + build_operator_tree("3") + .unwrap() + .eval_number_with_context_mut(&mut context), + Ok(3.0) + ); + assert_eq!( + build_operator_tree("true") + .unwrap() + .eval_boolean_with_context_mut(&mut context), + Ok(true) + ); + assert_eq!( + build_operator_tree("3,3") + .unwrap() + .eval_tuple_with_context(&mut context), + Ok(vec![Value::Int(3), Value::Int(3)]) + ); } #[cfg(feature = "serde")]