From 3c194145934b8e19cdae592a74dd418f18d67cbb Mon Sep 17 00:00:00 2001 From: fengcen Date: Thu, 17 Nov 2016 23:53:17 +0800 Subject: [PATCH] format code. --- src/builtin/mod.rs | 17 ++-- src/expression/mod.rs | 193 ++++++++++++++++++++++++++++-------------- src/function/mod.rs | 13 ++- src/lib.rs | 53 ++++++------ src/math/mod.rs | 2 +- src/node/mod.rs | 14 ++- src/operator/mod.rs | 100 ++++++++++++---------- 7 files changed, 237 insertions(+), 155 deletions(-) diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 0e79ba5..346cb1d 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -20,7 +20,7 @@ impl BuiltIn { #[derive(PartialEq)] enum Compare { Min, - Max + Max, } fn create_min_fuction() -> Function { @@ -36,7 +36,8 @@ fn compare(compare: Compare) -> Function { max_args: None, min_args: Some(1), compiled: Box::new(move |values| { - let mut prev: Result = Err(Error::Custom("can't find min value.".to_owned())); + let mut prev: Result = Err(Error::Custom("can't find min value." + .to_owned())); for value in values { match value { @@ -56,7 +57,7 @@ fn compare(compare: Compare) -> Function { prev = Ok(value); } } - }, + } _ => { if prev.is_ok() { if compare == Compare::Min { @@ -75,7 +76,7 @@ fn compare(compare: Compare) -> Function { } } prev - }) + }), } } @@ -84,18 +85,18 @@ fn create_is_empty_fuction() -> Function { Function { max_args: Some(1), min_args: Some(1), - compiled: Box::new(|values|{ + compiled: Box::new(|values| { match *values.first().unwrap() { Value::String(ref string) => Ok(Value::Bool(string.is_empty())), Value::Array(ref array) => Ok(Value::Bool(array.is_empty())), Value::Object(ref object) => Ok(Value::Bool(object.is_empty())), Value::Null => Ok(Value::Bool(true)), - _ => Ok(Value::Bool(false)) + _ => Ok(Value::Bool(false)), } - }) + }), } } fn create_array_function() -> Function { - Function::new(|values|Ok(to_value(values))) + Function::new(|values| Ok(to_value(values))) } diff --git a/src/expression/mod.rs b/src/expression/mod.rs index 188a70d..02faba9 100644 --- a/src/expression/mod.rs +++ b/src/expression/mod.rs @@ -15,15 +15,12 @@ pub struct Expression { pub raw: String, pub pos: Vec, pub operators: Vec, - pub node: Option + pub node: Option, } impl Expression { pub fn new>(raw: T) -> Result { - let mut expr = Expression { - raw: raw.into(), - ..Default::default() - }; + let mut expr = Expression { raw: raw.into(), ..Default::default() }; expr.parse_pos()?; expr.parse_operators()?; @@ -36,19 +33,19 @@ impl Expression { for (index, cur) in self.raw.chars().enumerate() { match cur { - '(' | ')' | '+' | '-' | '*' | '/' | ',' | ' ' | '!' | '=' | - '>' | '<' | '\'' | '[' | ']' | '%' | '&' | '|' => { - if ! found_quote { + '(' | ')' | '+' | '-' | '*' | '/' | ',' | ' ' | '!' | '=' | '>' | '<' | '\'' | + '[' | ']' | '%' | '&' | '|' => { + if !found_quote { self.pos.push(index); self.pos.push(index + 1); } - }, + } '"' => { - found_quote = ! found_quote; + found_quote = !found_quote; self.pos.push(index); self.pos.push(index + 1); - }, - _ => () + } + _ => (), } } @@ -90,8 +87,8 @@ impl Expression { prev.clear(); continue; } - }, - _ => () + } + _ => (), }; if quote.is_some() { @@ -147,11 +144,11 @@ impl Expression { operators.push(Operator::Function("array".to_owned())); operators.push(operator); continue; - }, + } Operator::LeftParenthesis => { parenthesis += 1; - if ! operators.is_empty() { + if !operators.is_empty() { let prev_operator = operators.pop().unwrap(); if prev_operator.is_identifier() { operators.push(Operator::Function(prev_operator.get_identifier())); @@ -161,11 +158,11 @@ impl Expression { operators.push(prev_operator); } } - }, + } Operator::RightParenthesis => parenthesis -= 1, Operator::RightSquareBracket => square_brackets -= 1, Operator::WhiteSpace => continue, - _ => () + _ => (), } prev = raw; @@ -185,17 +182,24 @@ impl Expression { for operator in &self.operators { match *operator { - Operator::Add(priority) | Operator::Sub(priority) | - Operator::Mul(priority) | Operator::Div(priority) | - Operator::Not(priority) | Operator::Eq(priority) | - Operator::Ne(priority) | Operator::Gt(priority) | - Operator::Lt(priority) | Operator::Ge(priority) | - Operator::And(priority) | Operator::Or(priority) | - Operator::Le(priority) | Operator::Rem(priority) => { - if ! parsing_nodes.is_empty() { + Operator::Add(priority) | + Operator::Sub(priority) | + Operator::Mul(priority) | + Operator::Div(priority) | + Operator::Not(priority) | + Operator::Eq(priority) | + Operator::Ne(priority) | + Operator::Gt(priority) | + Operator::Lt(priority) | + Operator::Ge(priority) | + Operator::And(priority) | + Operator::Or(priority) | + Operator::Le(priority) | + Operator::Rem(priority) => { + if !parsing_nodes.is_empty() { let prev = parsing_nodes.pop().unwrap(); if prev.is_value_or_enough() { - if prev.operator.get_priority() < priority && ! prev.closed { + if prev.operator.get_priority() < priority && !prev.closed { parsing_nodes.extend_from_slice(&rob_to(prev, operator.to_node())); } else { parsing_nodes.push(operator.children_to_node(vec![prev])); @@ -211,12 +215,18 @@ impl Expression { } else { return Err(Error::StartWithNonValueOperator); } - }, - Operator::Function(_) | Operator::LeftParenthesis | Operator::LeftSquareBracket => parsing_nodes.push(operator.to_node()), + } + Operator::Function(_) | + Operator::LeftParenthesis | + Operator::LeftSquareBracket => parsing_nodes.push(operator.to_node()), Operator::Comma => close_comma(&mut parsing_nodes)?, - Operator::RightParenthesis | Operator::RightSquareBracket => close_bracket(&mut parsing_nodes, operator.get_left())?, - Operator::Value(_) | Operator::Identifier(_) => append_child_to_last_node(&mut parsing_nodes, operator)?, - _ => () + Operator::RightParenthesis | + Operator::RightSquareBracket => { + close_bracket(&mut parsing_nodes, operator.get_left())? + } + Operator::Value(_) | + Operator::Identifier(_) => append_child_to_last_node(&mut parsing_nodes, operator)?, + _ => (), } } @@ -230,21 +240,75 @@ impl Expression { Box::new(move |contexts, buildin, functions| -> Result { return exec_node(&node, contexts, buildin, functions); - fn exec_node(node: &Node, contexts: ContextsRef, buildin: &Functions, functions: &Functions) -> Result { + fn exec_node(node: &Node, + contexts: ContextsRef, + buildin: &Functions, + functions: &Functions) + -> Result { match node.operator { - Operator::Add(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.add(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Mul(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.mul(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Sub(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.sub(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Div(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.div(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Rem(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.rem(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Eq(_) => Math::eq(&exec_node(&node.get_first_child(), contexts, buildin, functions)?, &exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Ne(_) => Math::ne(&exec_node(&node.get_first_child(), contexts, buildin, functions)?, &exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Gt(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.gt(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Lt(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.lt(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Ge(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.ge(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Le(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.le(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::And(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.and(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), - Operator::Or(_) => exec_node(&node.get_first_child(), contexts, buildin, functions)?.or(&exec_node(&node.get_last_child(), contexts, buildin, functions)?), + Operator::Add(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .add(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Mul(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .mul(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Sub(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .sub(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Div(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .div(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Rem(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .rem(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Eq(_) => { + Math::eq(&exec_node(&node.get_first_child(), contexts, buildin, functions)?, + &exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Ne(_) => { + Math::ne(&exec_node(&node.get_first_child(), contexts, buildin, functions)?, + &exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Gt(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .gt(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Lt(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .lt(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Ge(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .ge(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Le(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .le(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::And(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .and(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } + Operator::Or(_) => { + exec_node(&node.get_first_child(), contexts, buildin, functions) + ? + .or(&exec_node(&node.get_last_child(), contexts, buildin, functions)?) + } Operator::Function(ref ident) => { let function_option = if functions.contains_key(ident) { functions.get(ident) @@ -263,16 +327,17 @@ impl Expression { } else { Err(Error::FunctionNotExists(ident.to_owned())) } - }, + } Operator::Value(ref value) => Ok(value.clone()), Operator::Not(_) => { - let value = exec_node(&node.get_first_child(), contexts, buildin, functions)?; + let value = + exec_node(&node.get_first_child(), contexts, buildin, functions)?; match value { Value::Bool(boolean) => Ok(Value::Bool(!boolean)), Value::Null => Ok(Value::Bool(true)), - _ => Err(Error::NotBoolean(value)) + _ => Err(Error::NotBoolean(value)), } - }, + } Operator::Identifier(ref ident) => { let number = parse_number(ident); if number.is_some() { @@ -282,18 +347,20 @@ impl Expression { } else { match find(contexts, ident) { Some(value) => Ok(value), - None => Ok(Value::Null) + None => Ok(Value::Null), } } - }, - _ => Err(Error::CanNotExec(node.operator.clone())) + } + _ => Err(Error::CanNotExec(node.operator.clone())), } } }) } } -fn append_child_to_last_node(parsing_nodes: &mut Vec, operator: &Operator) -> Result<(), Error> { +fn append_child_to_last_node(parsing_nodes: &mut Vec, + operator: &Operator) + -> Result<(), Error> { let mut node = operator.to_node(); node.closed = true; @@ -316,7 +383,7 @@ fn append_child_to_last_node(parsing_nodes: &mut Vec, operator: &Operator) fn get_final_node(mut parsing_nodes: Vec) -> Result { if parsing_nodes.is_empty() { - return Err(Error::NoFinalNode) + return Err(Error::NoFinalNode); } while parsing_nodes.len() != 1 { @@ -343,7 +410,7 @@ fn close_bracket(parsing_nodes: &mut Vec, bracket: Operator) -> Result<(), return Err(Error::BracketNotWithFunction); } } else if prev.operator == bracket { - if ! current.closed { + if !current.closed { current.closed = true; } @@ -361,13 +428,13 @@ fn close_bracket(parsing_nodes: &mut Vec, bracket: Operator) -> Result<(), } break; } else { - if ! prev.closed { + if !prev.closed { prev.add_child(current); if prev.is_enough() { prev.closed = true; } - if ! parsing_nodes.is_empty() { + if !parsing_nodes.is_empty() { parsing_nodes.push(prev); } else { return Err(Error::StartWithNonValueOperator); @@ -410,13 +477,13 @@ fn close_comma(parsing_nodes: &mut Vec) -> Result<(), Error> { return Err(Error::CommaNotWithFunction); } } else { - if ! prev.closed { + if !prev.closed { prev.add_child(current); if prev.is_enough() { prev.closed = true; } - if ! parsing_nodes.is_empty() { + if !parsing_nodes.is_empty() { parsing_nodes.push(prev); } else { return Err(Error::StartWithNonValueOperator); @@ -440,7 +507,7 @@ fn find(contexts: ContextsRef, key: &str) -> Option { let value = get(context, key); match value { Some(_) => return value, - None => continue + None => continue, } } @@ -454,10 +521,10 @@ fn get(context: &Context, key: &str) -> Option { if context_value_option.is_none() { None - } else if ! keys.is_empty() { + } else if !keys.is_empty() { match context_value_option.unwrap().search(&keys.join(".")) { Some(value) => Some(value.clone()), - None => None + None => None, } } else { Some(context_value_option.unwrap().clone()) diff --git a/src/function/mod.rs b/src/function/mod.rs index 0d341a3..c6ce043 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -11,22 +11,27 @@ pub struct Function { /// Minimum number of arguments. pub min_args: Option, /// Accept values and return a result which contains a value. - pub compiled: Box) -> Result + Sync + Send> + pub compiled: Box) -> Result + Sync + Send>, } impl Function { /// Create a function with a closure. - pub fn new(closure: F) -> Function where F: 'static + Fn(Vec) -> Result + Sync + Send { + pub fn new(closure: F) -> Function + where F: 'static + Fn(Vec) -> Result + Sync + Send + { Function { max_args: None, min_args: None, - compiled: Box::new(closure) + compiled: Box::new(closure), } } } impl fmt::Debug for Function { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Function {{ max_args: {:?}, min_args: {:?} }}", self.max_args, self.min_args) + write!(f, + "Function {{ max_args: {:?}, min_args: {:?} }}", + self.max_args, + self.min_args) } } diff --git a/src/lib.rs b/src/lib.rs index 8f35b18..48fcaa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,7 +117,10 @@ pub fn eval_with_functions(expr: &str, functions: &Functions) -> Result Result { +pub fn eval_with_context_and_functions(expr: &str, + context: &Context, + functions: &Functions) + -> Result { let mut contexts = Contexts::new(); contexts.push(context.clone()); eval_with_contexts_and_functions(expr, &contexts, functions) @@ -125,7 +128,10 @@ pub fn eval_with_context_and_functions(expr: &str, context: &Context, functions: /// Evaluates the value of an expression with the given contexts and functions.
/// The value of the last context is searched first. -pub fn eval_with_contexts_and_functions(expr: &str, contexts: ContextsRef, functions: &Functions) -> Result { +pub fn eval_with_contexts_and_functions(expr: &str, + contexts: ContextsRef, + functions: &Functions) + -> Result { Expression::new(expr)?.compile()(contexts, &BuiltIn::new(), functions) } @@ -194,7 +200,8 @@ mod tests { #[test] fn test_min_brackets() { - assert_eq!(eval("(min(30, 5, 245, 20) * 10 + (5 + 5) * 5)"), Ok(to_value(100))); + assert_eq!(eval("(min(30, 5, 245, 20) * 10 + (5 + 5) * 5)"), + Ok(to_value(100))); } #[test] @@ -209,7 +216,8 @@ mod tests { #[test] fn test_max_brackets() { - assert_eq!(eval("(max(30, 5, 245, 20) * 10 + (5 + 5) * 5)"), Ok(to_value(2500))); + assert_eq!(eval("(max(30, 5, 245, 20) * 10 + (5 + 5) * 5)"), + Ok(to_value(2500))); } #[test] @@ -301,12 +309,14 @@ mod tests { #[test] fn test_single_and_double_quote() { - assert_eq!(eval(r#"' """" ' + ' """" '"#), Ok(to_value(r#" """" """" "#))); + assert_eq!(eval(r#"' """" ' + ' """" '"#), + Ok(to_value(r#" """" """" "#))); } #[test] fn test_double_and_single_quote() { - assert_eq!(eval(r#"" '''' " + " '''' ""#), Ok(to_value(r#" '''' '''' "#))); + assert_eq!(eval(r#"" '''' " + " '''' ""#), + Ok(to_value(r#" '''' '''' "#))); } #[test] @@ -398,7 +408,8 @@ mod tests { fn test_buildin_is_empty() { let mut context = Context::new(); context.insert("array".to_owned(), to_value(Vec::::new())); - assert_eq!(eval_with_context("is_empty(array)", &context), Ok(to_value(true))); + assert_eq!(eval_with_context("is_empty(array)", &context), + Ok(to_value(true))); } #[test] @@ -411,8 +422,10 @@ mod tests { #[test] fn test_custom_function() { let mut functions = Functions::new(); - functions.insert("output".to_owned(), Function::new(|_|Ok(to_value("This is custom function's output")))); - assert_eq!(eval_with_functions("output()", &functions), Ok(to_value("This is custom function's output"))); + functions.insert("output".to_owned(), + Function::new(|_| Ok(to_value("This is custom function's output")))); + assert_eq!(eval_with_functions("output()", &functions), + Ok(to_value("This is custom function's output"))); } #[test] @@ -421,7 +434,7 @@ mod tests { raw: "+ + 5".to_owned(), pos: Vec::new(), operators: Vec::new(), - node: None + node: None, }; expr.parse_pos().unwrap(); @@ -432,10 +445,7 @@ mod tests { #[test] fn test_error_duplicate_operator() { - let mut expr = Expression { - raw: "5 + + 5".to_owned(), - ..Default::default() - }; + let mut expr = Expression { raw: "5 + + 5".to_owned(), ..Default::default() }; expr.parse_pos().unwrap(); expr.parse_operators().unwrap(); @@ -445,10 +455,7 @@ mod tests { #[test] fn test_error_duplicate_value() { - let mut expr = Expression { - raw: "2 + 6 5".to_owned(), - ..Default::default() - }; + let mut expr = Expression { raw: "2 + 6 5".to_owned(), ..Default::default() }; expr.parse_pos().unwrap(); expr.parse_operators().unwrap(); @@ -458,10 +465,7 @@ mod tests { #[test] fn test_error_unpaired_brackets() { - let mut expr = Expression { - raw: "(2 + 3)) * 5".to_owned(), - ..Default::default() - }; + let mut expr = Expression { raw: "(2 + 3)) * 5".to_owned(), ..Default::default() }; expr.parse_pos().unwrap(); @@ -470,10 +474,7 @@ mod tests { #[test] fn test_error_comma() { - let mut expr = Expression { - raw: ", 2 + 5".to_owned(), - ..Default::default() - }; + let mut expr = Expression { raw: ", 2 + 5".to_owned(), ..Default::default() }; expr.parse_pos().unwrap(); expr.parse_operators().unwrap(); diff --git a/src/math/mod.rs b/src/math/mod.rs index ff5b082..fe4a306 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -165,7 +165,7 @@ impl Type for Value { Value::F64(f) => f, Value::I64(f) => f as f64, Value::U64(f) => f as f64, - _ => panic!("not a number") + _ => panic!("not a number"), } } diff --git a/src/node/mod.rs b/src/node/mod.rs index 7c70353..da49037 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -8,7 +8,7 @@ use Function; pub struct Node { pub operator: Operator, pub children: Vec, - pub closed: bool + pub closed: bool, } impl Node { @@ -16,7 +16,7 @@ impl Node { Node { operator: operator, children: Vec::new(), - closed: false + closed: false, } } @@ -51,11 +51,7 @@ impl Node { if self.operator.is_value_or_ident() { true } else if self.operator.can_have_child() { - if self.closed { - true - } else { - self.is_enough() - } + if self.closed { true } else { self.is_enough() } } else { false } @@ -63,8 +59,8 @@ impl Node { pub fn is_unclosed_function(&self) -> bool { match self.operator { - Operator::Function(_) => ! self.closed, - _ => false + Operator::Function(_) => !self.closed, + _ => false, } } diff --git a/src/operator/mod.rs b/src/operator/mod.rs index 59fd957..c686711 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -31,65 +31,68 @@ pub enum Operator { Comma, Function(String), Identifier(String), - Value(Value) + Value(Value), } impl Operator { pub fn is_identifier(&self) -> bool { match *self { Operator::Identifier(_) => true, - _ => false + _ => false, } } pub fn can_at_beginning(&self) -> bool { match *self { - Operator::Not(_) | Operator::Function(_) | Operator::LeftParenthesis => true, - _ => false + Operator::Not(_) | + Operator::Function(_) | + Operator::LeftParenthesis => true, + _ => false, } } pub fn get_max_args(&self) -> Option { match *self { - Operator::Add(_) | Operator::Sub(_) | - Operator::Mul(_) | Operator::Div(_) | - Operator::Eq(_) | Operator::Ne(_) | - Operator::Gt(_) | Operator::Lt(_) | - Operator::Ge(_) | Operator::Le(_) | - Operator::And(_) | Operator::Or(_) | - Operator::Rem(_)=> Some(2), + Operator::Add(_) | Operator::Sub(_) | Operator::Mul(_) | Operator::Div(_) | + Operator::Eq(_) | Operator::Ne(_) | Operator::Gt(_) | Operator::Lt(_) | + Operator::Ge(_) | Operator::Le(_) | Operator::And(_) | Operator::Or(_) | + Operator::Rem(_) => Some(2), Operator::Not(_) => Some(1), Operator::Function(_) => None, - _ => Some(0) + _ => Some(0), } } pub fn get_min_args(&self) -> Option { match *self { - Operator::Add(_) | Operator::Sub(_) | - Operator::Mul(_) | Operator::Div(_) | - Operator::Eq(_) | Operator::Ne(_) | - Operator::Gt(_) | Operator::Lt(_) | - Operator::Ge(_) | Operator::Le(_) | - Operator::And(_) | Operator::Or(_) | + Operator::Add(_) | Operator::Sub(_) | Operator::Mul(_) | Operator::Div(_) | + Operator::Eq(_) | Operator::Ne(_) | Operator::Gt(_) | Operator::Lt(_) | + Operator::Ge(_) | Operator::Le(_) | Operator::And(_) | Operator::Or(_) | Operator::Rem(_) => Some(2), Operator::Not(_) => Some(1), Operator::Function(_) => None, - _ => Some(0) + _ => Some(0), } } pub fn get_priority(&self) -> u8 { match *self { - Operator::Add(priority) | Operator::Sub(priority) | - Operator::Div(priority) | Operator::Mul(priority) | - Operator::Eq(priority) | Operator::Ne(priority) | - Operator::Gt(priority) | Operator::Lt(priority) | - Operator::Ge(priority) | Operator::Le(priority) | - Operator::And(priority) | Operator::Or(priority) | + Operator::Add(priority) | + Operator::Sub(priority) | + Operator::Div(priority) | + Operator::Mul(priority) | + Operator::Eq(priority) | + Operator::Ne(priority) | + Operator::Gt(priority) | + Operator::Lt(priority) | + Operator::Ge(priority) | + Operator::Le(priority) | + Operator::And(priority) | + Operator::Or(priority) | Operator::Rem(priority) => priority, - Operator::Value(_) | Operator::Identifier(_) => 0, - _ => 99 + Operator::Value(_) | + Operator::Identifier(_) => 0, + _ => 99, } } @@ -100,34 +103,43 @@ impl Operator { pub fn is_not(&self) -> bool { match *self { Operator::Not(_) => true, - _ => false + _ => false, } } pub fn is_value_or_ident(&self) -> bool { match *self { - Operator::Value(_) | Operator::Identifier(_) => true, - _ => false + Operator::Value(_) | + Operator::Identifier(_) => true, + _ => false, } } pub fn can_have_child(&self) -> bool { match *self { - Operator::Function(_) | Operator::Add(_) | - Operator::Sub(_) | Operator::Div(_) | - Operator::Mul(_) | Operator::Rem(_) | - Operator::Eq(_) | Operator::Ne(_) | - Operator::Gt(_) | Operator::Lt(_) | - Operator::And(_) | Operator::Or(_) | - Operator::Ge(_) | Operator::Le(_) => true, - _ => false + Operator::Function(_) | + Operator::Add(_) | + Operator::Sub(_) | + Operator::Div(_) | + Operator::Mul(_) | + Operator::Rem(_) | + Operator::Eq(_) | + Operator::Ne(_) | + Operator::Gt(_) | + Operator::Lt(_) | + Operator::And(_) | + Operator::Or(_) | + Operator::Ge(_) | + Operator::Le(_) => true, + _ => false, } } pub fn is_left(&self) -> bool { match *self { - Operator::LeftParenthesis | Operator::LeftSquareBracket => true, - _ => false + Operator::LeftParenthesis | + Operator::LeftSquareBracket => true, + _ => false, } } @@ -135,12 +147,12 @@ impl Operator { match *self { Operator::RightParenthesis => Operator::LeftParenthesis, Operator::RightSquareBracket => Operator::LeftSquareBracket, - _ => panic!("not bracket") + _ => panic!("not bracket"), } } pub fn to_node(&self) -> Node { - Node::new(self.clone()) + Node::new(self.clone()) } pub fn children_to_node(&self, children: Vec) -> Node { @@ -152,7 +164,7 @@ impl Operator { pub fn get_identifier(&self) -> String { match *self { Operator::Identifier(ref ident) => ident.to_owned(), - _ => panic!("not identifier") + _ => panic!("not identifier"), } } } @@ -186,7 +198,7 @@ impl FromStr for Operator { "<=" => Ok(Operator::Le(6)), "&&" => Ok(Operator::And(4)), "||" => Ok(Operator::Or(2)), - _ => Ok(Operator::Identifier(raw.to_owned())) + _ => Ok(Operator::Identifier(raw.to_owned())), } } }