From 2399df16a18af1ce1ba4bd50e3f2896b8346251a Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 29 Aug 2019 10:02:05 +0300 Subject: [PATCH] Run rustfmt --- src/context/mod.rs | 5 +---- src/error/display.rs | 9 ++++++++- src/error/mod.rs | 11 ++++++++--- src/feature_serde/mod.rs | 3 +-- src/function/builtin.rs | 13 +++++++------ src/function/mod.rs | 3 +-- src/interface/mod.rs | 31 ++++++++++++++++--------------- src/lib.rs | 14 +++++++------- src/token/mod.rs | 6 ++++-- src/tree/display.rs | 2 +- src/tree/iter.rs | 2 +- src/tree/mod.rs | 36 +++++++++++++++++++++++++----------- tests/integration.rs | 27 +++++++++++++++++++++------ tests/serde.rs | 2 +- 14 files changed, 102 insertions(+), 62 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 092d064..7a062ac 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,9 +1,6 @@ use std::collections::HashMap; -use crate::function::Function; -use crate::value::value_type::ValueType; -use crate::EvalexprError; -use crate::EvalexprResult; +use crate::{function::Function, value::value_type::ValueType, EvalexprError, EvalexprResult}; use crate::value::Value; diff --git a/src/error/display.rs b/src/error/display.rs index 864ffdd..836ff4b 100644 --- a/src/error/display.rs +++ b/src/error/display.rs @@ -35,7 +35,14 @@ impl fmt::Display for EvalexprError { write!(f, "Expected a Value::Boolean, 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), AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."), PrecedenceViolation => write!( diff --git a/src/error/mod.rs b/src/error/mod.rs index c73b64c..7baef0d 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -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. //! They are meant as shortcuts to not write the same error checking code everywhere. -use crate::token::PartialToken; -use crate::value::{value_type::ValueType, TupleType}; +use crate::{ + token::PartialToken, + value::{value_type::ValueType, TupleType}, +}; use crate::value::Value; @@ -244,7 +246,10 @@ impl EvalexprError { /// Constructs `Error::ExpectedFixedLenTuple{expected_len, actual}`. 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}`. diff --git a/src/feature_serde/mod.rs b/src/feature_serde/mod.rs index 59392c0..9cb2789 100644 --- a/src/feature_serde/mod.rs +++ b/src/feature_serde/mod.rs @@ -1,7 +1,6 @@ -use crate::interface::build_operator_tree; +use crate::{interface::build_operator_tree, Node}; use serde::{de, Deserialize, Deserializer}; use std::fmt; -use crate::Node; impl<'de> Deserialize<'de> for Node { fn deserialize(deserializer: D) -> Result diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 5ac81fc..4d61d6c 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -1,11 +1,10 @@ #[cfg(feature = "regex_support")] use regex::Regex; - -use crate::value::{FloatType, IntType}; -use crate::EvalexprError; -use crate::Function; -use crate::Value; +use crate::{ + value::{FloatType, IntType}, + EvalexprError, Function, Value, +}; pub fn builtin_function(identifier: &str) -> Option { match identifier { @@ -82,7 +81,9 @@ pub fn builtin_function(identifier: &str) -> Option { let re_str = arguments[1].as_string()?; let repl = arguments[2].as_string()?; 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( re_str.to_string(), format!("{}", err), diff --git a/src/function/mod.rs b/src/function/mod.rs index bd5b290..6a2b501 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -1,7 +1,6 @@ use std::fmt; -use crate::error::EvalexprResult; -use crate::value::Value; +use crate::{error::EvalexprResult, value::Value}; pub(crate) mod builtin; diff --git a/src/interface/mod.rs b/src/interface/mod.rs index fe1b073..73dfefa 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -1,15 +1,7 @@ -use crate::{token, HashMapContext}; -use crate::tree; -use crate::value::TupleType; -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; +use crate::{ + token, tree, value::TupleType, Context, EmptyType, EvalexprError, EvalexprResult, FloatType, + HashMapContext, IntType, Node, Value, EMPTY_VALUE, +}; /// 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. /// /// *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 { +pub fn eval_string_with_context_mut( + string: &str, + context: &mut dyn Context, +) -> EvalexprResult { match eval_with_context_mut(string, context) { Ok(Value::String(string)) => Ok(string), 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. /// /// *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 { +pub fn eval_int_with_context_mut( + string: &str, + context: &mut dyn Context, +) -> EvalexprResult { match eval_with_context_mut(string, context) { Ok(Value::Int(int)) => Ok(int), 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. /// /// *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 { +pub fn eval_boolean_with_context_mut( + string: &str, + context: &mut dyn Context, +) -> EvalexprResult { match eval_with_context_mut(string, context) { Ok(Value::Boolean(boolean)) => Ok(boolean), Ok(value) => Err(EvalexprError::expected_boolean(value)), diff --git a/src/lib.rs b/src/lib.rs index 68b0873..5fff42e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -369,13 +369,13 @@ extern crate serde; #[macro_use] extern crate serde_derive; -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, +pub use crate::{ + context::{Context, EmptyContext, HashMapContext}, + error::{EvalexprError, EvalexprResult}, + function::Function, + interface::*, + tree::Node, + value::{value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE}, }; mod context; diff --git a/src/token/mod.rs b/src/token/mod.rs index 66662dc..05dc458 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -1,5 +1,7 @@ -use crate::error::{EvalexprError, EvalexprResult}; -use crate::value::{FloatType, IntType}; +use crate::{ + error::{EvalexprError, EvalexprResult}, + value::{FloatType, IntType}, +}; mod display; diff --git a/src/tree/display.rs b/src/tree/display.rs index 6b7152a..fbfff95 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -1,5 +1,5 @@ -use std::fmt::{Display, Error, Formatter}; use crate::Node; +use std::fmt::{Display, Error, Formatter}; impl Display for Node { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { diff --git a/src/tree/iter.rs b/src/tree/iter.rs index 6648005..1bbe8ce 100644 --- a/src/tree/iter.rs +++ b/src/tree/iter.rs @@ -1,5 +1,5 @@ -use std::slice::Iter; use crate::Node; +use std::slice::Iter; /// An iterator that traverses an operator tree in pre-order. pub struct NodeIter<'a> { diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 6724ad8..0793584 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -1,9 +1,8 @@ -use crate::token::Token; -use crate::value::{TupleType, EMPTY_VALUE}; -use crate::EmptyContext; -use crate::EmptyType; -use crate::FloatType; -use crate::IntType; +use crate::{ + token::Token, + value::{TupleType, EMPTY_VALUE}, + EmptyContext, EmptyType, FloatType, IntType, +}; use crate::{ 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. /// /// Fails, if one of the operators in the expression tree fails. - pub fn eval_string_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult { + pub fn eval_string_with_context_mut( + &self, + context: &mut dyn Context, + ) -> EvalexprResult { match self.eval_with_context_mut(context) { Ok(Value::String(string)) => Ok(string), 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. /// /// Fails, if one of the operators in the expression tree fails. - pub fn eval_float_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult { + pub fn eval_float_with_context_mut( + &self, + context: &mut dyn Context, + ) -> EvalexprResult { match self.eval_with_context_mut(context) { Ok(Value::Float(float)) => Ok(float), 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. /// /// Fails, if one of the operators in the expression tree fails. - pub fn eval_number_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult { + pub fn eval_number_with_context_mut( + &self, + context: &mut dyn Context, + ) -> EvalexprResult { match self.eval_with_context_mut(context) { Ok(Value::Int(int)) => Ok(int as FloatType), 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. /// /// Fails, if one of the operators in the expression tree fails. - pub fn eval_tuple_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult { + pub fn eval_tuple_with_context_mut( + &self, + context: &mut dyn Context, + ) -> EvalexprResult { match self.eval_with_context_mut(context) { Ok(Value::Tuple(tuple)) => Ok(tuple), 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. /// /// Fails, if one of the operators in the expression tree fails. - pub fn eval_empty_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult { + pub fn eval_empty_with_context_mut( + &self, + context: &mut dyn Context, + ) -> EvalexprResult { match self.eval_with_context_mut(context) { Ok(Value::Empty) => Ok(EMPTY_VALUE), Ok(value) => Err(EvalexprError::expected_empty(value)), diff --git a/tests/integration.rs b/tests/integration.rs index 2e6d719..5300a61 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -639,12 +639,27 @@ fn test_tuple_definitions() { #[test] fn test_implicit_context() { - assert_eq!(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("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_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_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!(eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"), Ok("xyzabc".to_string())); -} \ No newline at end of file + 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!( + eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"), + Ok("xyzabc".to_string()) + ); +} diff --git a/tests/serde.rs b/tests/serde.rs index 77af185..ecf7414 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -11,4 +11,4 @@ fn test_serde() { let serde_tree: Node = ron::de::from_str(&format!("\"{}\"", string)).unwrap(); assert_eq!(manual_tree.eval(), serde_tree.eval()); } -} \ No newline at end of file +}