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 EvalexprError;
use EvalexprResult;
use function::Function;
use value::value_type::ValueType;
use EvalexprError;
use EvalexprResult;
use crate::value::Value;

View File

@ -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;

View File

@ -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<TupleType> {
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.
///
/// *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.
///
/// *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<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 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;

View File

@ -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,

View File

@ -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")