Add crate shortcut methods to eval with context mut

Relates to #30
This commit is contained in:
Sebastian Schmidt 2019-03-28 09:37:33 +01:00
parent e266f4fc0d
commit 3c108d5960
2 changed files with 157 additions and 31 deletions

View File

@ -44,6 +44,25 @@ pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult<Valu
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context) tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
} }
/// Evaluate the given expression string with the given mutable context.
///
/// # Examples
///
/// ```rust
/// use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context_mut("one + two + three", &mut context), Ok(Value::from(6)));
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_with_context_mut(string: &str, context: &mut Context) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
}
/// Build the operator tree for the given expression string. /// Build the operator tree for the given expression string.
/// ///
/// The operator tree can later on be evaluated directly. /// The operator tree can later on be evaluated directly.
@ -76,33 +95,21 @@ pub fn build_operator_tree(string: &str) -> EvalexprResult<Node> {
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_string(string: &str) -> EvalexprResult<String> { pub fn eval_string(string: &str) -> EvalexprResult<String> {
match eval(string) { eval_string_with_context(string, &EmptyContext)
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. /// Evaluate the given expression string into an integer.
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_int(string: &str) -> EvalexprResult<IntType> { pub fn eval_int(string: &str) -> EvalexprResult<IntType> {
match eval(string) { eval_int_with_context(string, &EmptyContext)
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. /// Evaluate the given expression string into a float.
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_float(string: &str) -> EvalexprResult<FloatType> { pub fn eval_float(string: &str) -> EvalexprResult<FloatType> {
match eval(string) { eval_float_with_context(string, &EmptyContext)
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. /// Evaluate the given expression string into a float.
@ -110,34 +117,21 @@ pub fn eval_float(string: &str) -> EvalexprResult<FloatType> {
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_number(string: &str) -> EvalexprResult<FloatType> { pub fn eval_number(string: &str) -> EvalexprResult<FloatType> {
match eval(string) { eval_number_with_context(string, &EmptyContext)
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. /// Evaluate the given expression string into a boolean.
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_boolean(string: &str) -> EvalexprResult<bool> { pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
match eval(string) { eval_boolean_with_context(string, &EmptyContext)
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. /// Evaluate the given expression string into a tuple.
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> { pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
match eval(string) { eval_tuple_with_context(string, &EmptyContext)
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
} }
/// Evaluate the given expression string into a string with the given context. /// 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), 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<String> {
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<IntType> {
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<FloatType> {
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<FloatType> {
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<bool> {
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<TupleType> {
match eval_with_context_mut(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
}

View File

@ -380,6 +380,7 @@ fn test_shortcut_functions() {
.eval_int_with_context(&context), .eval_int_with_context(&context),
Ok(3) Ok(3)
); );
assert_eq!(build_operator_tree("3").unwrap().eval_number(), Ok(3.0));
assert_eq!( assert_eq!(
build_operator_tree("3") build_operator_tree("3")
.unwrap() .unwrap()
@ -406,6 +407,60 @@ fn test_shortcut_functions() {
.eval_tuple_with_context(&context), .eval_tuple_with_context(&context),
Ok(vec![Value::Int(3), Value::Int(3)]) 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")] #[cfg(feature = "serde")]