Run rustfmt

This commit is contained in:
Sebastian Schmidt 2019-08-29 10:02:05 +03:00
parent 6ace829117
commit 2399df16a1
14 changed files with 102 additions and 62 deletions

View File

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

View File

@ -35,7 +35,14 @@ impl fmt::Display for EvalexprError {
write!(f, "Expected a Value::Boolean, but got {:?}.", actual) write!(f, "Expected a Value::Boolean, but got {:?}.", actual)
}, },
ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual), ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual),
ExpectedFixedLenTuple { expected_len, actual } => write!(f, "Expected a Value::Tuple of len {}, but got {:?}.", expected_len, actual), ExpectedFixedLenTuple {
expected_len,
actual,
} => write!(
f,
"Expected a Value::Tuple of len {}, but got {:?}.",
expected_len, actual
),
ExpectedEmpty { actual } => write!(f, "Expected a Value::Empty, but got {:?}.", actual), ExpectedEmpty { actual } => write!(f, "Expected a Value::Empty, but got {:?}.", actual),
AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."), AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."),
PrecedenceViolation => write!( PrecedenceViolation => write!(

View File

@ -5,8 +5,10 @@
//! The module also contains some helper functions starting with `expect_` that check for a condition and return `Err(_)` if the condition is not fulfilled. //! 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. //! They are meant as shortcuts to not write the same error checking code everywhere.
use crate::token::PartialToken; use crate::{
use crate::value::{value_type::ValueType, TupleType}; token::PartialToken,
value::{value_type::ValueType, TupleType},
};
use crate::value::Value; use crate::value::Value;
@ -244,7 +246,10 @@ impl EvalexprError {
/// Constructs `Error::ExpectedFixedLenTuple{expected_len, actual}`. /// Constructs `Error::ExpectedFixedLenTuple{expected_len, actual}`.
pub fn expected_fixed_len_tuple(expected_len: usize, actual: Value) -> Self { pub fn expected_fixed_len_tuple(expected_len: usize, actual: Value) -> Self {
EvalexprError::ExpectedFixedLenTuple { expected_len, actual } EvalexprError::ExpectedFixedLenTuple {
expected_len,
actual,
}
} }
/// Constructs `Error::ExpectedEmpty{actual}`. /// Constructs `Error::ExpectedEmpty{actual}`.

View File

@ -1,7 +1,6 @@
use crate::interface::build_operator_tree; use crate::{interface::build_operator_tree, Node};
use serde::{de, Deserialize, Deserializer}; use serde::{de, Deserialize, Deserializer};
use std::fmt; use std::fmt;
use crate::Node;
impl<'de> Deserialize<'de> for Node { impl<'de> Deserialize<'de> for Node {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

View File

@ -1,11 +1,10 @@
#[cfg(feature = "regex_support")] #[cfg(feature = "regex_support")]
use regex::Regex; use regex::Regex;
use crate::{
use crate::value::{FloatType, IntType}; value::{FloatType, IntType},
use crate::EvalexprError; EvalexprError, Function, Value,
use crate::Function; };
use crate::Value;
pub fn builtin_function(identifier: &str) -> Option<Function> { pub fn builtin_function(identifier: &str) -> Option<Function> {
match identifier { match identifier {
@ -82,7 +81,9 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
let re_str = arguments[1].as_string()?; let re_str = arguments[1].as_string()?;
let repl = arguments[2].as_string()?; let repl = arguments[2].as_string()?;
match Regex::new(&re_str) { match Regex::new(&re_str) {
Ok(re) => Ok(Value::String(re.replace_all(&subject, repl.as_str()).to_string())), Ok(re) => Ok(Value::String(
re.replace_all(&subject, repl.as_str()).to_string(),
)),
Err(err) => Err(EvalexprError::invalid_regex( Err(err) => Err(EvalexprError::invalid_regex(
re_str.to_string(), re_str.to_string(),
format!("{}", err), format!("{}", err),

View File

@ -1,7 +1,6 @@
use std::fmt; use std::fmt;
use crate::error::EvalexprResult; use crate::{error::EvalexprResult, value::Value};
use crate::value::Value;
pub(crate) mod builtin; pub(crate) mod builtin;

View File

@ -1,15 +1,7 @@
use crate::{token, HashMapContext}; use crate::{
use crate::tree; token, tree, value::TupleType, Context, EmptyType, EvalexprError, EvalexprResult, FloatType,
use crate::value::TupleType; HashMapContext, IntType, Node, Value, EMPTY_VALUE,
use crate::Context; };
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. /// Evaluate the given expression string.
/// ///
@ -224,7 +216,10 @@ pub fn eval_empty_with_context(string: &str, context: &dyn Context) -> EvalexprR
/// 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.*
pub fn eval_string_with_context_mut(string: &str, context: &mut dyn 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) { match eval_with_context_mut(string, context) {
Ok(Value::String(string)) => Ok(string), Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)), Ok(value) => Err(EvalexprError::expected_string(value)),
@ -235,7 +230,10 @@ pub fn eval_string_with_context_mut(string: &str, context: &mut dyn Context) ->
/// Evaluate the given expression string into an integer with the given mutable context. /// 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.* /// *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 dyn 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) { match eval_with_context_mut(string, context) {
Ok(Value::Int(int)) => Ok(int), Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)), Ok(value) => Err(EvalexprError::expected_int(value)),
@ -276,7 +274,10 @@ pub fn eval_number_with_context_mut(
/// Evaluate the given expression string into a boolean with the given mutable context. /// 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.* /// *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 dyn 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) { match eval_with_context_mut(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean), Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)), Ok(value) => Err(EvalexprError::expected_boolean(value)),

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,8 @@
use crate::token::Token; use crate::{
use crate::value::{TupleType, EMPTY_VALUE}; token::Token,
use crate::EmptyContext; value::{TupleType, EMPTY_VALUE},
use crate::EmptyType; EmptyContext, EmptyType, FloatType, IntType,
use crate::FloatType; };
use crate::IntType;
use crate::{ use crate::{
context::Context, context::Context,
@ -228,7 +227,10 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a string with an the given mutable context. /// 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. /// Fails, if one of the operators in the expression tree fails.
pub fn eval_string_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<String> { pub fn eval_string_with_context_mut(
&self,
context: &mut dyn Context,
) -> EvalexprResult<String> {
match self.eval_with_context_mut(context) { match self.eval_with_context_mut(context) {
Ok(Value::String(string)) => Ok(string), Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)), Ok(value) => Err(EvalexprError::expected_string(value)),
@ -239,7 +241,10 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a float with an the given mutable context. /// 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. /// Fails, if one of the operators in the expression tree fails.
pub fn eval_float_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> { pub fn eval_float_with_context_mut(
&self,
context: &mut dyn Context,
) -> EvalexprResult<FloatType> {
match self.eval_with_context_mut(context) { match self.eval_with_context_mut(context) {
Ok(Value::Float(float)) => Ok(float), Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)), Ok(value) => Err(EvalexprError::expected_float(value)),
@ -262,7 +267,10 @@ impl Node {
/// If the result of the expression is an integer, it is silently converted into a float. /// 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. /// Fails, if one of the operators in the expression tree fails.
pub fn eval_number_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> { pub fn eval_number_with_context_mut(
&self,
context: &mut dyn Context,
) -> EvalexprResult<FloatType> {
match self.eval_with_context_mut(context) { match self.eval_with_context_mut(context) {
Ok(Value::Int(int)) => Ok(int as FloatType), Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(Value::Float(float)) => Ok(float), Ok(Value::Float(float)) => Ok(float),
@ -285,7 +293,10 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a tuple with an the given mutable context. /// 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. /// Fails, if one of the operators in the expression tree fails.
pub fn eval_tuple_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<TupleType> { pub fn eval_tuple_with_context_mut(
&self,
context: &mut dyn Context,
) -> EvalexprResult<TupleType> {
match self.eval_with_context_mut(context) { match self.eval_with_context_mut(context) {
Ok(Value::Tuple(tuple)) => Ok(tuple), Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)), Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -296,7 +307,10 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an empty value with an the given mutable context. /// 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. /// Fails, if one of the operators in the expression tree fails.
pub fn eval_empty_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<EmptyType> { pub fn eval_empty_with_context_mut(
&self,
context: &mut dyn Context,
) -> EvalexprResult<EmptyType> {
match self.eval_with_context_mut(context) { match self.eval_with_context_mut(context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE), Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)), Ok(value) => Err(EvalexprError::expected_empty(value)),

View File

@ -639,12 +639,27 @@ fn test_tuple_definitions() {
#[test] #[test]
fn test_implicit_context() { fn test_implicit_context() {
assert_eq!(eval("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(Value::from(true))); assert_eq!(
assert_eq!(eval_boolean("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(true)); eval("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"),
Ok(Value::from(true))
);
assert_eq!(
eval_boolean("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"),
Ok(true)
);
assert_eq!(eval_int("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0)); assert_eq!(eval_int("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0));
assert_eq!(eval_float("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b + 0.5"), Ok(0.5)); assert_eq!(
eval_float("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b + 0.5"),
Ok(0.5)
);
assert_eq!(eval_number("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0.0)); assert_eq!(eval_number("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0.0));
assert_eq!(eval_empty("a = 2 + 4 * 2; b = -5 + 3 * 5;"), Ok(())); assert_eq!(eval_empty("a = 2 + 4 * 2; b = -5 + 3 * 5;"), Ok(()));
assert_eq!(eval_tuple("a = 2 + 4 * 2; b = -5 + 3 * 5; a, b + 0.5"), Ok(vec![Value::from(10), Value::from(10.5)])); assert_eq!(
assert_eq!(eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"), Ok("xyzabc".to_string())); eval_tuple("a = 2 + 4 * 2; b = -5 + 3 * 5; a, b + 0.5"),
Ok(vec![Value::from(10), Value::from(10.5)])
);
assert_eq!(
eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"),
Ok("xyzabc".to_string())
);
} }