Merge remote-tracking branch 'origin/master'

This commit is contained in:
Sebastian Schmidt 2019-08-29 08:57:07 +03:00
commit 0c5d032b37
18 changed files with 85 additions and 84 deletions

View File

@ -10,6 +10,7 @@ homepage = "https://github.com/ISibboI/evalexpr"
documentation = "https://docs.rs/evalexpr"
readme = "README.md"
license = "MIT"
edition = "2018"
[lib]
name = "evalexpr"

View File

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

View File

@ -1,10 +1,10 @@
use std::fmt;
use EvalexprError;
use crate::EvalexprError;
impl fmt::Display for EvalexprError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use EvalexprError::*;
use crate::EvalexprError::*;
match self {
WrongOperatorArgumentAmount { expected, actual } => write!(
f,

View File

@ -5,8 +5,8 @@
//! The module also contains some helper functions starting with `expect_` that check for a condition and return `Err(_)` if the condition is not fulfilled.
//! They are meant as shortcuts to not write the same error checking code everywhere.
use token::PartialToken;
use value::{value_type::ValueType, TupleType};
use crate::token::PartialToken;
use crate::value::{value_type::ValueType, TupleType};
use crate::value::Value;

View File

@ -2,10 +2,10 @@
use regex::Regex;
use crate::error::*;
use value::{FloatType, IntType};
use EvalexprError;
use Function;
use Value;
use crate::value::{FloatType, IntType};
use crate::EvalexprError;
use crate::Function;
use crate::Value;
pub fn builtin_function(identifier: &str) -> Option<Function> {
match identifier {

View File

@ -1,7 +1,7 @@
use std::fmt;
use error::EvalexprResult;
use value::Value;
use crate::error::EvalexprResult;
use crate::value::Value;
pub(crate) mod builtin;
@ -20,14 +20,14 @@ pub(crate) mod builtin;
/// assert_eq!(eval_with_context("id(4)", &context), Ok(Value::from(4)));
/// ```
pub struct Function {
function: Box<Fn(&Value) -> EvalexprResult<Value>>,
function: Box<dyn Fn(&Value) -> EvalexprResult<Value>>,
}
impl Function {
/// Creates a user-defined function.
///
/// The `function` is a boxed function that takes a `Value` and returns a `EvalexprResult<Value, Error>`.
pub fn new(function: Box<Fn(&Value) -> EvalexprResult<Value>>) -> Self {
pub fn new(function: Box<dyn Fn(&Value) -> EvalexprResult<Value>>) -> Self {
Self { function }
}

View File

@ -1,16 +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 Value;
use EMPTY_VALUE;
use crate::token;
use crate::tree;
use crate::value::TupleType;
use crate::Context;
use crate::EmptyContext;
use crate::EmptyType;
use crate::EvalexprError;
use crate::EvalexprResult;
use crate::FloatType;
use crate::IntType;
use crate::Node;
use crate::Value;
use crate::EMPTY_VALUE;
/// Evaluate the given expression string.
///
@ -42,7 +42,7 @@ pub fn eval(string: &str) -> EvalexprResult<Value> {
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult<Value> {
pub fn eval_with_context(string: &str, context: &dyn Context) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
}
@ -61,7 +61,7 @@ pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult<Valu
/// ```
///
/// *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> {
pub fn eval_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
}
@ -146,7 +146,7 @@ pub fn eval_empty(string: &str) -> EvalexprResult<EmptyType> {
/// 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.*
pub fn eval_string_with_context(string: &str, context: &Context) -> EvalexprResult<String> {
pub fn eval_string_with_context(string: &str, context: &dyn Context) -> EvalexprResult<String> {
match eval_with_context(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -157,7 +157,7 @@ pub fn eval_string_with_context(string: &str, context: &Context) -> EvalexprResu
/// Evaluate the given expression string into an integer with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_int_with_context(string: &str, context: &Context) -> EvalexprResult<IntType> {
pub fn eval_int_with_context(string: &str, context: &dyn Context) -> EvalexprResult<IntType> {
match eval_with_context(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -168,7 +168,7 @@ pub fn eval_int_with_context(string: &str, context: &Context) -> EvalexprResult<
/// Evaluate the given expression string into a float with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_float_with_context(string: &str, context: &Context) -> EvalexprResult<FloatType> {
pub fn eval_float_with_context(string: &str, context: &dyn Context) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -180,7 +180,7 @@ pub fn eval_float_with_context(string: &str, context: &Context) -> EvalexprResul
/// 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(string: &str, context: &Context) -> EvalexprResult<FloatType> {
pub fn eval_number_with_context(string: &str, context: &dyn Context) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(int as FloatType),
@ -192,7 +192,7 @@ pub fn eval_number_with_context(string: &str, context: &Context) -> EvalexprResu
/// Evaluate the given expression string into a boolean with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_boolean_with_context(string: &str, context: &Context) -> EvalexprResult<bool> {
pub fn eval_boolean_with_context(string: &str, context: &dyn Context) -> EvalexprResult<bool> {
match eval_with_context(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -203,7 +203,7 @@ pub fn eval_boolean_with_context(string: &str, context: &Context) -> EvalexprRes
/// Evaluate the given expression string into a tuple with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_tuple_with_context(string: &str, context: &Context) -> EvalexprResult<TupleType> {
pub fn eval_tuple_with_context(string: &str, context: &dyn Context) -> EvalexprResult<TupleType> {
match eval_with_context(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -214,7 +214,7 @@ 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> {
pub fn eval_empty_with_context(string: &str, context: &dyn Context) -> EvalexprResult<EmptyType> {
match eval_with_context(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
@ -225,7 +225,7 @@ pub fn eval_empty_with_context(string: &str, context: &Context) -> EvalexprResul
/// 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> {
pub fn eval_string_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<String> {
match eval_with_context_mut(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -236,7 +236,7 @@ pub fn eval_string_with_context_mut(string: &str, context: &mut Context) -> Eval
/// 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> {
pub fn eval_int_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<IntType> {
match eval_with_context_mut(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -249,7 +249,7 @@ pub fn eval_int_with_context_mut(string: &str, context: &mut Context) -> Evalexp
/// *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,
context: &mut dyn Context,
) -> EvalexprResult<FloatType> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
@ -264,7 +264,7 @@ pub fn eval_float_with_context_mut(
/// *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,
context: &mut dyn Context,
) -> EvalexprResult<FloatType> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
@ -277,7 +277,7 @@ pub fn eval_number_with_context_mut(
/// 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> {
pub fn eval_boolean_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<bool> {
match eval_with_context_mut(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -290,7 +290,7 @@ pub fn eval_boolean_with_context_mut(string: &str, context: &mut Context) -> Eva
/// *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,
context: &mut dyn Context,
) -> EvalexprResult<TupleType> {
match eval_with_context_mut(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
@ -304,7 +304,7 @@ pub fn eval_tuple_with_context_mut(
/// *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,
context: &mut dyn Context,
) -> EvalexprResult<EmptyType> {
match eval_with_context_mut(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),

View File

@ -369,12 +369,12 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
pub use context::{Context, EmptyContext, HashMapContext};
pub use error::{EvalexprError, EvalexprResult};
pub use function::Function;
pub use interface::*;
pub use tree::Node;
pub use value::{
pub use crate::context::{Context, EmptyContext, HashMapContext};
pub use crate::error::{EvalexprError, EvalexprResult};
pub use crate::function::Function;
pub use crate::interface::*;
pub use crate::tree::Node;
pub use crate::value::{
value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE,
};

View File

@ -1,6 +1,6 @@
use std::fmt::{Display, Error, Formatter};
use operator::*;
use crate::operator::*;
impl Display for Operator {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {

View File

@ -1,4 +1,4 @@
use function::builtin::builtin_function;
use crate::function::builtin::builtin_function;
use crate::{context::Context, error::*, value::Value};

View File

@ -1,6 +1,6 @@
use std::fmt;
use token::{PartialToken, Token};
use crate::token::{PartialToken, Token};
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {

View File

@ -1,5 +1,5 @@
use error::{EvalexprError, EvalexprResult};
use value::{FloatType, IntType};
use crate::error::{EvalexprError, EvalexprResult};
use crate::value::{FloatType, IntType};
mod display;

View File

@ -1,5 +1,5 @@
use std::fmt::{Display, Error, Formatter};
use Node;
use crate::Node;
impl Display for Node {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {

View File

@ -1,5 +1,5 @@
use std::slice::Iter;
use Node;
use crate::Node;
/// An iterator that traverses an operator tree in pre-order.
pub struct NodeIter<'a> {

View File

@ -1,9 +1,9 @@
use token::Token;
use value::{TupleType, EMPTY_VALUE};
use EmptyContext;
use EmptyType;
use FloatType;
use IntType;
use crate::token::Token;
use crate::value::{TupleType, EMPTY_VALUE};
use crate::EmptyContext;
use crate::EmptyType;
use crate::FloatType;
use crate::IntType;
use crate::{
context::Context,
@ -120,7 +120,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node with the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_with_context(&self, context: &Context) -> EvalexprResult<Value> {
pub fn eval_with_context(&self, context: &dyn Context) -> EvalexprResult<Value> {
let mut arguments = Vec::new();
for child in self.children() {
arguments.push(child.eval_with_context(context)?);
@ -131,7 +131,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node with the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_with_context_mut(&self, context: &mut Context) -> EvalexprResult<Value> {
pub fn eval_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<Value> {
let mut arguments = Vec::new();
for child in self.children() {
arguments.push(child.eval_with_context_mut(context)?);
@ -149,7 +149,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a string with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_string_with_context(&self, context: &Context) -> EvalexprResult<String> {
pub fn eval_string_with_context(&self, context: &dyn Context) -> EvalexprResult<String> {
match self.eval_with_context(context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -160,7 +160,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a float with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_float_with_context(&self, context: &Context) -> EvalexprResult<FloatType> {
pub fn eval_float_with_context(&self, context: &dyn Context) -> EvalexprResult<FloatType> {
match self.eval_with_context(context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -171,7 +171,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an integer with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_int_with_context(&self, context: &Context) -> EvalexprResult<IntType> {
pub fn eval_int_with_context(&self, context: &dyn Context) -> EvalexprResult<IntType> {
match self.eval_with_context(context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -183,7 +183,7 @@ impl Node {
/// If the result of the expression is an integer, it is silently converted into a float.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_number_with_context(&self, context: &Context) -> EvalexprResult<FloatType> {
pub fn eval_number_with_context(&self, context: &dyn Context) -> EvalexprResult<FloatType> {
match self.eval_with_context(context) {
Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(Value::Float(float)) => Ok(float),
@ -195,7 +195,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a boolean with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_boolean_with_context(&self, context: &Context) -> EvalexprResult<bool> {
pub fn eval_boolean_with_context(&self, context: &dyn Context) -> EvalexprResult<bool> {
match self.eval_with_context(context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -206,7 +206,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a tuple with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_tuple_with_context(&self, context: &Context) -> EvalexprResult<TupleType> {
pub fn eval_tuple_with_context(&self, context: &dyn Context) -> EvalexprResult<TupleType> {
match self.eval_with_context(context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -217,7 +217,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an empty value with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_empty_with_context(&self, context: &Context) -> EvalexprResult<EmptyType> {
pub fn eval_empty_with_context(&self, context: &dyn Context) -> EvalexprResult<EmptyType> {
match self.eval_with_context(context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
@ -228,7 +228,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a string with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_string_with_context_mut(&self, context: &mut Context) -> EvalexprResult<String> {
pub fn eval_string_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<String> {
match self.eval_with_context_mut(context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -239,7 +239,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a float with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_float_with_context_mut(&self, context: &mut Context) -> EvalexprResult<FloatType> {
pub fn eval_float_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> {
match self.eval_with_context_mut(context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -250,7 +250,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an integer with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_int_with_context_mut(&self, context: &mut Context) -> EvalexprResult<IntType> {
pub fn eval_int_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<IntType> {
match self.eval_with_context_mut(context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -262,7 +262,7 @@ impl Node {
/// If the result of the expression is an integer, it is silently converted into a float.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_number_with_context_mut(&self, context: &mut Context) -> EvalexprResult<FloatType> {
pub fn eval_number_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> {
match self.eval_with_context_mut(context) {
Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(Value::Float(float)) => Ok(float),
@ -274,7 +274,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a boolean with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_boolean_with_context_mut(&self, context: &mut Context) -> EvalexprResult<bool> {
pub fn eval_boolean_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<bool> {
match self.eval_with_context_mut(context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -285,7 +285,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a tuple with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_tuple_with_context_mut(&self, context: &mut Context) -> EvalexprResult<TupleType> {
pub fn eval_tuple_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<TupleType> {
match self.eval_with_context_mut(context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -296,7 +296,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an empty value with an the given mutable context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_empty_with_context_mut(&self, context: &mut Context) -> EvalexprResult<EmptyType> {
pub fn eval_empty_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<EmptyType> {
match self.eval_with_context_mut(context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),

View File

@ -1,6 +1,6 @@
use std::fmt::{Display, Error, Formatter};
use Value;
use crate::Value;
impl Display for Value {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {

View File

@ -1,4 +1,4 @@
use error::{EvalexprError, EvalexprResult};
use crate::error::{EvalexprError, EvalexprResult};
mod display;
pub mod value_type;
@ -216,7 +216,7 @@ impl From<()> for Value {
#[cfg(test)]
mod tests {
use value::{TupleType, Value};
use crate::value::{TupleType, Value};
#[test]
fn test_value_conversions() {

View File

@ -1,4 +1,4 @@
use Value;
use crate::Value;
/// The type of a `Value`.
#[derive(Clone, Copy, Eq, PartialEq)]