From 5bc4bcb7020d07ca5ec6ac8e368290b0df18e2c9 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 28 Mar 2019 10:30:58 +0100 Subject: [PATCH] Add crate level shortcut evaluations for `Value::Empty` Relates to #28 --- src/context/mod.rs | 4 ++-- src/error/mod.rs | 2 +- src/interface/mod.rs | 40 +++++++++++++++++++++++++++++++++++++--- src/lib.rs | 2 +- src/tree/mod.rs | 4 ++-- tests/integration.rs | 8 +++++++- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 860c51c..9edc734 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,9 +1,9 @@ use std::collections::HashMap; -use EvalexprError; -use EvalexprResult; use function::Function; use value::value_type::ValueType; +use EvalexprError; +use EvalexprResult; use crate::value::Value; diff --git a/src/error/mod.rs b/src/error/mod.rs index 84042fa..675ce75 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -6,7 +6,7 @@ //! They are meant as shortcuts to not write the same error checking code everywhere. use token::PartialToken; -use value::{TupleType, value_type::ValueType}; +use value::{value_type::ValueType, TupleType}; use crate::value::Value; diff --git a/src/interface/mod.rs b/src/interface/mod.rs index 9e57bb8..d6fa5e8 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -1,14 +1,16 @@ +use token; +use tree; +use value::TupleType; use Context; use EmptyContext; +use EmptyType; use EvalexprError; use EvalexprResult; use FloatType; use IntType; use Node; -use token; -use tree; use Value; -use value::TupleType; +use EMPTY_VALUE; /// Evaluate the given expression string. /// @@ -134,6 +136,13 @@ pub fn eval_tuple(string: &str) -> EvalexprResult { eval_tuple_with_context(string, &EmptyContext) } +/// 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) +} + /// Evaluate the given expression string into a string with the given context. /// /// *See the [crate doc](index.html) for more examples and explanations of the expression format.* @@ -202,6 +211,17 @@ pub fn eval_tuple_with_context(string: &str, context: &Context) -> EvalexprResul } } +/// Evaluate the given expression string into an empty value with the given context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_empty_with_context(string: &str, context: &Context) -> EvalexprResult { + match eval_with_context(string, context) { + Ok(Value::Empty) => Ok(EMPTY_VALUE), + Ok(value) => Err(EvalexprError::expected_empty(value)), + 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.* @@ -278,3 +298,17 @@ pub fn eval_tuple_with_context_mut( Err(error) => Err(error), } } + +/// Evaluate the given expression string into an empty value with the given mutable context. +/// +/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* +pub fn eval_empty_with_context_mut( + string: &str, + context: &mut Context, +) -> EvalexprResult { + match eval_with_context_mut(string, context) { + Ok(Value::Empty) => Ok(EMPTY_VALUE), + Ok(value) => Err(EvalexprError::expected_empty(value)), + Err(error) => Err(error), + } +} diff --git a/src/lib.rs b/src/lib.rs index b0a6ef6..32a4b03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -283,7 +283,7 @@ pub use function::Function; pub use interface::*; pub use tree::Node; pub use value::{ - EMPTY_VALUE, EmptyType, FloatType, IntType, TupleType, Value, value_type::ValueType, + value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE, }; mod context; diff --git a/src/tree/mod.rs b/src/tree/mod.rs index e2bba9b..27e780a 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -1,9 +1,9 @@ +use token::Token; +use value::{TupleType, EMPTY_VALUE}; use EmptyContext; use EmptyType; use FloatType; use IntType; -use token::Token; -use value::{EMPTY_VALUE, TupleType}; use crate::{ context::Context, diff --git a/tests/integration.rs b/tests/integration.rs index febff0d..3a27ab9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,6 +1,6 @@ extern crate evalexpr; -use evalexpr::{*, error::*}; +use evalexpr::{error::*, *}; #[test] fn test_unary_examples() { @@ -362,6 +362,8 @@ fn test_shortcut_functions() { eval_tuple_with_context("3,3", &context), Ok(vec![Value::Int(3), Value::Int(3)]) ); + assert_eq!(eval_empty(""), Ok(EMPTY_VALUE)); + assert_eq!(eval_empty_with_context("", &context), Ok(EMPTY_VALUE)); // assert_eq!(build_operator_tree("???").unwrap().eval_string()); assert_eq!( @@ -433,6 +435,10 @@ fn test_shortcut_functions() { eval_tuple_with_context_mut("3,3", &mut context), Ok(vec![Value::Int(3), Value::Int(3)]) ); + assert_eq!( + eval_empty_with_context_mut("", &mut context), + Ok(EMPTY_VALUE) + ); assert_eq!( build_operator_tree("string")