2019-03-20 13:47:33 +00:00
|
|
|
use Configuration;
|
2019-03-27 15:38:59 +00:00
|
|
|
use EmptyContext;
|
2019-03-27 15:33:46 +00:00
|
|
|
use EvalexprError;
|
2019-03-20 14:29:50 +00:00
|
|
|
use FloatType;
|
|
|
|
use IntType;
|
2019-03-20 13:47:33 +00:00
|
|
|
use Node;
|
2019-03-27 15:14:27 +00:00
|
|
|
use token;
|
|
|
|
use tree;
|
2019-03-20 13:47:33 +00:00
|
|
|
use Value;
|
2019-03-27 15:14:27 +00:00
|
|
|
use value::TupleType;
|
2019-03-20 13:47:33 +00:00
|
|
|
|
|
|
|
/// Evaluate the given expression string.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use evalexpr::*;
|
|
|
|
///
|
|
|
|
/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval(string: &str) -> Result<Value, EvalexprError> {
|
2019-03-27 15:38:59 +00:00
|
|
|
eval_with_configuration(string, &EmptyContext)
|
2019-03-20 13:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the given expression string with the given configuration.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use evalexpr::*;
|
|
|
|
///
|
2019-03-27 15:38:59 +00:00
|
|
|
/// let mut configuration = HashMapContext::new();
|
|
|
|
/// configuration.set_value("one", 1).unwrap(); // Do proper error handling here
|
|
|
|
/// configuration.set_value("two", 2).unwrap(); // Do proper error handling here
|
|
|
|
/// configuration.set_value("three", 3).unwrap(); // Do proper error handling here
|
2019-03-20 13:47:33 +00:00
|
|
|
/// assert_eq!(eval_with_configuration("one + two + three", &configuration), Ok(Value::from(6)));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<Value, EvalexprError> {
|
2019-03-20 13:47:33 +00:00
|
|
|
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_configuration(configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build the operator tree for the given expression string.
|
|
|
|
///
|
|
|
|
/// The operator tree can later on be evaluated directly.
|
|
|
|
/// This saves runtime if a single expression should be evaluated multiple times, for example with differing configurations.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use evalexpr::*;
|
|
|
|
///
|
2019-03-27 15:14:27 +00:00
|
|
|
/// let precomputed = build_operator_tree("one + two + three").unwrap(); // Do proper error handling here
|
2019-03-20 13:47:33 +00:00
|
|
|
///
|
2019-03-27 15:38:59 +00:00
|
|
|
/// let mut configuration = HashMapContext::new();
|
|
|
|
/// configuration.set_value("one", 1).unwrap(); // Do proper error handling here
|
|
|
|
/// configuration.set_value("two", 2).unwrap(); // Do proper error handling here
|
|
|
|
/// configuration.set_value("three", 3).unwrap(); // Do proper error handling here
|
2019-03-20 13:47:33 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(precomputed.eval_with_configuration(&configuration), Ok(Value::from(6)));
|
|
|
|
///
|
2019-03-27 15:38:59 +00:00
|
|
|
/// configuration.set_value("three", 5).unwrap(); // Do proper error handling here
|
2019-03-20 13:47:33 +00:00
|
|
|
/// assert_eq!(precomputed.eval_with_configuration(&configuration), Ok(Value::from(8)));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn build_operator_tree(string: &str) -> Result<Node, EvalexprError> {
|
2019-03-20 13:47:33 +00:00
|
|
|
tree::tokens_to_operator_tree(token::tokenize(string)?)
|
|
|
|
}
|
2019-03-20 14:29:50 +00:00
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a string.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval_string(string: &str) -> Result<String, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval(string) {
|
|
|
|
Ok(Value::String(string)) => Ok(string),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_string(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into an integer.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval_int(string: &str) -> Result<IntType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval(string) {
|
|
|
|
Ok(Value::Int(int)) => Ok(int),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_int(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a float.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval_float(string: &str) -> Result<FloatType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval(string) {
|
|
|
|
Ok(Value::Float(float)) => Ok(float),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_float(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a boolean.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval_boolean(string: &str) -> Result<bool, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval(string) {
|
|
|
|
Ok(Value::Boolean(boolean)) => Ok(boolean),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_boolean(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a tuple.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
2019-03-27 15:33:46 +00:00
|
|
|
pub fn eval_tuple(string: &str) -> Result<TupleType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval(string) {
|
|
|
|
Ok(Value::Tuple(tuple)) => Ok(tuple),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_tuple(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a string with the given configuration.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_string_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<String, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval_with_configuration(string, configuration) {
|
|
|
|
Ok(Value::String(string)) => Ok(string),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_string(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into an integer with the given configuration.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_int_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<IntType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval_with_configuration(string, configuration) {
|
|
|
|
Ok(Value::Int(int)) => Ok(int),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_int(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a float with the given configuration.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_float_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<FloatType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval_with_configuration(string, configuration) {
|
|
|
|
Ok(Value::Float(float)) => Ok(float),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_float(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a boolean with the given configuration.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_boolean_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<bool, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval_with_configuration(string, configuration) {
|
|
|
|
Ok(Value::Boolean(boolean)) => Ok(boolean),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_boolean(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:23 +00:00
|
|
|
/// Evaluate the given expression string into a tuple with the given configuration.
|
2019-03-20 14:29:50 +00:00
|
|
|
///
|
|
|
|
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
|
|
|
pub fn eval_tuple_with_configuration(
|
|
|
|
string: &str,
|
|
|
|
configuration: &Configuration,
|
2019-03-27 15:33:46 +00:00
|
|
|
) -> Result<TupleType, EvalexprError> {
|
2019-03-20 14:29:50 +00:00
|
|
|
match eval_with_configuration(string, configuration) {
|
|
|
|
Ok(Value::Tuple(tuple)) => Ok(tuple),
|
2019-03-27 15:33:46 +00:00
|
|
|
Ok(value) => Err(EvalexprError::expected_tuple(value)),
|
2019-03-20 14:29:50 +00:00
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|