Add crate level shortcut evaluations for Value::Empty

Relates to #28
This commit is contained in:
Sebastian Schmidt 2019-03-28 10:30:58 +01:00
parent 7d36ebe8df
commit 5bc4bcb702
6 changed files with 50 additions and 10 deletions

View File

@ -1,9 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use EvalexprError;
use EvalexprResult;
use function::Function; use function::Function;
use value::value_type::ValueType; use value::value_type::ValueType;
use EvalexprError;
use EvalexprResult;
use crate::value::Value; use crate::value::Value;

View File

@ -6,7 +6,7 @@
//! They are meant as shortcuts to not write the same error checking code everywhere. //! They are meant as shortcuts to not write the same error checking code everywhere.
use token::PartialToken; use token::PartialToken;
use value::{TupleType, value_type::ValueType}; use value::{value_type::ValueType, TupleType};
use crate::value::Value; use crate::value::Value;

View File

@ -1,14 +1,16 @@
use token;
use tree;
use value::TupleType;
use Context; use Context;
use EmptyContext; use EmptyContext;
use EmptyType;
use EvalexprError; use EvalexprError;
use EvalexprResult; use EvalexprResult;
use FloatType; use FloatType;
use IntType; use IntType;
use Node; use Node;
use token;
use tree;
use Value; use Value;
use value::TupleType; use EMPTY_VALUE;
/// Evaluate the given expression string. /// Evaluate the given expression string.
/// ///
@ -134,6 +136,13 @@ pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
eval_tuple_with_context(string, &EmptyContext) 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<EmptyType> {
eval_empty_with_context(string, &EmptyContext)
}
/// Evaluate the given expression string into a string with the given context. /// 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.* /// *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<EmptyType> {
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. /// 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.* /// *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), 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<EmptyType> {
match eval_with_context_mut(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
Err(error) => Err(error),
}
}

View File

@ -283,7 +283,7 @@ pub use function::Function;
pub use interface::*; pub use interface::*;
pub use tree::Node; pub use tree::Node;
pub use value::{ 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; mod context;

View File

@ -1,9 +1,9 @@
use token::Token;
use value::{TupleType, EMPTY_VALUE};
use EmptyContext; use EmptyContext;
use EmptyType; use EmptyType;
use FloatType; use FloatType;
use IntType; use IntType;
use token::Token;
use value::{EMPTY_VALUE, TupleType};
use crate::{ use crate::{
context::Context, context::Context,

View File

@ -1,6 +1,6 @@
extern crate evalexpr; extern crate evalexpr;
use evalexpr::{*, error::*}; use evalexpr::{error::*, *};
#[test] #[test]
fn test_unary_examples() { fn test_unary_examples() {
@ -362,6 +362,8 @@ fn test_shortcut_functions() {
eval_tuple_with_context("3,3", &context), eval_tuple_with_context("3,3", &context),
Ok(vec![Value::Int(3), Value::Int(3)]) 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!(build_operator_tree("???").unwrap().eval_string());
assert_eq!( assert_eq!(
@ -433,6 +435,10 @@ fn test_shortcut_functions() {
eval_tuple_with_context_mut("3,3", &mut context), eval_tuple_with_context_mut("3,3", &mut context),
Ok(vec![Value::Int(3), Value::Int(3)]) Ok(vec![Value::Int(3), Value::Int(3)])
); );
assert_eq!(
eval_empty_with_context_mut("", &mut context),
Ok(EMPTY_VALUE)
);
assert_eq!( assert_eq!(
build_operator_tree("string") build_operator_tree("string")