From 406bfe0e0524630a7bfcc28281969c40e169b758 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 15 Mar 2019 19:22:14 +0200 Subject: [PATCH] rustfmt --- src/error/mod.rs | 4 +-- src/lib.rs | 5 +++- src/operator/mod.rs | 10 ++++---- src/token/mod.rs | 60 ++++++++++++++++++++++----------------------- src/tree/mod.rs | 9 ++++--- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/error/mod.rs b/src/error/mod.rs index ac6a4ab..5749afa 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -60,7 +60,7 @@ impl Error { } pub fn unmatched_partial_token(first: PartialToken, second: Option) -> Self { - Error::UnmatchedPartialToken {first, second} + Error::UnmatchedPartialToken { first, second } } } @@ -84,4 +84,4 @@ pub fn expect_boolean(actual: &Value) -> Result { Value::Boolean(boolean) => Ok(*boolean), _ => Err(Error::expected_boolean(actual.clone())), } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 4af0b7e..7dc5eb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,10 @@ mod test { #[test] fn test_boolean_examples() { assert_eq!(eval("true && false"), Ok(Value::Boolean(false))); - assert_eq!(eval("true && false || true && true"), Ok(Value::Boolean(true))); + assert_eq!( + eval("true && false || true && true"), + Ok(Value::Boolean(true)) + ); assert_eq!(eval("5 > 4 && 1 <= 1"), Ok(Value::Boolean(true))); assert_eq!(eval("5.0 <= 4.9 || !(4 > 3.5)"), Ok(Value::Boolean(false))); } diff --git a/src/operator/mod.rs b/src/operator/mod.rs index c7e05d7..5965fa3 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -400,11 +400,11 @@ impl Operator for And { let a = expect_boolean(&arguments[0])?; let b = expect_boolean(&arguments[1])?; - if a && b { - Ok(Value::Boolean(true)) - } else { - Ok(Value::Boolean(false)) - } + if a && b { + Ok(Value::Boolean(true)) + } else { + Ok(Value::Boolean(false)) + } } } diff --git a/src/token/mod.rs b/src/token/mod.rs index ab3d3af..3356399 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -1,5 +1,5 @@ -use value::{FloatType, IntType}; use error::Error; +use value::{FloatType, IntType}; #[derive(Clone, PartialEq, Debug)] pub enum Token { @@ -139,7 +139,10 @@ fn resolve_literals(mut tokens: &[PartialToken]) -> Result, Error> { let mut cutoff = 2; result.push(match first { - PartialToken::Token(token) => {cutoff = 1; token}, + PartialToken::Token(token) => { + cutoff = 1; + token + } PartialToken::Literal(literal) => { cutoff = 1; if let Ok(number) = literal.parse::() { @@ -151,42 +154,39 @@ fn resolve_literals(mut tokens: &[PartialToken]) -> Result, Error> { } else { Token::Identifier(literal.to_string()) } + } + PartialToken::Eq => match second { + Some(PartialToken::Eq) => Token::Eq, + _ => return Err(Error::unmatched_partial_token(first, second)), }, - PartialToken::Eq => { - match second { - Some(PartialToken::Eq) => Token::Eq, - _ => return Err(Error::unmatched_partial_token(first, second)), + PartialToken::ExclamationMark => match second { + Some(PartialToken::Eq) => Token::Eq, + _ => { + cutoff = 1; + Token::Not } }, - PartialToken::ExclamationMark => { - match second { - Some(PartialToken::Eq) => Token::Eq, - _ => {cutoff = 1; Token::Not}, + PartialToken::Gt => match second { + Some(PartialToken::Eq) => Token::Geq, + _ => { + cutoff = 1; + Token::Gt } }, - PartialToken::Gt => { - match second { - Some(PartialToken::Eq) => Token::Geq, - _ => {cutoff = 1; Token::Gt}, + PartialToken::Lt => match second { + Some(PartialToken::Eq) => Token::Leq, + _ => { + cutoff = 1; + Token::Lt } }, - PartialToken::Lt => { - match second { - Some(PartialToken::Eq) => Token::Leq, - _ => {cutoff = 1; Token::Lt}, - } + PartialToken::Ampersand => match second { + Some(PartialToken::Ampersand) => Token::And, + _ => return Err(Error::unmatched_partial_token(first, second)), }, - PartialToken::Ampersand => { - match second { - Some(PartialToken::Ampersand) => Token::And, - _ => return Err(Error::unmatched_partial_token(first, second)), - } - }, - PartialToken::VerticalBar => { - match second { - Some(PartialToken::VerticalBar) => Token::Or, - _ => return Err(Error::unmatched_partial_token(first, second)), - } + PartialToken::VerticalBar => match second { + Some(PartialToken::VerticalBar) => Token::Or, + _ => return Err(Error::unmatched_partial_token(first, second)), }, }); diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 121668b..242053c 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -43,7 +43,8 @@ impl Node { if self.operator().is_leaf() { Err(Error::AppendedToLeafNode) } else if self.has_correct_amount_of_children() { - if self.children.last_mut().unwrap().operator().precedence() < node.operator().precedence() + if self.children.last_mut().unwrap().operator().precedence() + < node.operator().precedence() { self.children .last_mut() @@ -98,11 +99,11 @@ pub fn tokens_to_operator_tree(tokens: Vec) -> Result { Token::And => Some(Node::new(And)), Token::Or => Some(Node::new(Or)), Token::Not => Some(Node::new(Not)), - + Token::LBrace => { root.push(Node::root_node()); None - }, + } Token::RBrace => { if root.len() < 2 { return Err(Error::UnmatchedRBrace); @@ -111,7 +112,7 @@ pub fn tokens_to_operator_tree(tokens: Vec) -> Result { } } Token::Whitespace => None, - + Token::Identifier(identifier) => Some(Node::new(Identifier::new(identifier))), Token::Float(number) => Some(Node::new(Const::new(Value::Float(number)))), Token::Int(number) => Some(Node::new(Const::new(Value::Int(number)))),