diff --git a/src/context/mod.rs b/src/context/mod.rs index 9988ebd..e07f9c7 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -103,7 +103,7 @@ impl Context for HashMapContext { /// /// let ctx = evalexpr::context_map! { /// "x" => 8, -/// "f" => Function::new(None, Box::new(|_| Ok(42.into()) )) +/// "f" => Function::new(None, Box::new(|_| Ok(42.into()) )) /// }.unwrap(); /// /// assert_eq!(eval_with_context("x + f()", &ctx), Ok(50.into())); diff --git a/src/error/display.rs b/src/error/display.rs index b704b2a..8b15a49 100644 --- a/src/error/display.rs +++ b/src/error/display.rs @@ -18,7 +18,7 @@ impl fmt::Display for EvalexprError { ), ExpectedString { actual } => { write!(f, "Expected a Value::String, but got {:?}.", actual) - } + }, ExpectedInt { actual } => write!(f, "Expected a Value::Int, but got {:?}.", actual), ExpectedFloat { actual } => write!(f, "Expected a Value::Float, but got {:?}.", actual), ExpectedNumber { actual } => write!( @@ -33,7 +33,7 @@ impl fmt::Display for EvalexprError { ), ExpectedBoolean { actual } => { write!(f, "Expected a Value::Boolean, but got {:?}.", actual) - } + }, ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, 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."), @@ -53,7 +53,7 @@ impl fmt::Display for EvalexprError { ), TypeError { expected, actual } => { write!(f, "Expected one of {:?}, but got {:?}.", expected, actual) - } + }, UnmatchedLBrace => write!(f, "Found an unmatched opening parenthesis '('."), UnmatchedRBrace => write!(f, "Found an unmatched closing parenthesis ')'."), UnmatchedPartialToken { first, second } => { @@ -71,7 +71,7 @@ impl fmt::Display for EvalexprError { first ) } - } + }, AdditionError { augend, addend } => write!(f, "Error adding {} + {}", augend, addend), SubtractionError { minuend, @@ -84,10 +84,10 @@ impl fmt::Display for EvalexprError { } => write!(f, "Error multiplying {} * {}", multiplicand, multiplier), DivisionError { dividend, divisor } => { write!(f, "Error dividing {} / {}", dividend, divisor) - } + }, ModulationError { dividend, divisor } => { write!(f, "Error modulating {} % {}", dividend, divisor) - } + }, InvalidRegex { regex, message } => write!( f, "Regular expression {:?} is invalid: {:?}", diff --git a/src/operator/mod.rs b/src/operator/mod.rs index a86eb53..de59d87 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -121,7 +121,7 @@ impl Operator { } else { Ok(Value::Empty) } - } + }, Add => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number_or_string(&arguments[0])?; @@ -147,7 +147,7 @@ impl Operator { arguments[0].as_number().unwrap() + arguments[1].as_number().unwrap(), )) } - } + }, Sub => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number(&arguments[0])?; @@ -168,7 +168,7 @@ impl Operator { arguments[0].as_number().unwrap() - arguments[1].as_number().unwrap(), )) } - } + }, Neg => { expect_operator_argument_amount(arguments.len(), 1)?; expect_number(&arguments[0])?; @@ -183,7 +183,7 @@ impl Operator { } else { Ok(Value::Float(-arguments[0].as_number().unwrap())) } - } + }, Mul => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number(&arguments[0])?; @@ -204,7 +204,7 @@ impl Operator { arguments[0].as_number().unwrap() * arguments[1].as_number().unwrap(), )) } - } + }, Div => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number(&arguments[0])?; @@ -225,7 +225,7 @@ impl Operator { arguments[0].as_number().unwrap() / arguments[1].as_number().unwrap(), )) } - } + }, Mod => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number(&arguments[0])?; @@ -246,7 +246,7 @@ impl Operator { arguments[0].as_number().unwrap() % arguments[1].as_number().unwrap(), )) } - } + }, Exp => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number(&arguments[0])?; @@ -258,7 +258,7 @@ impl Operator { .unwrap() .powf(arguments[1].as_number().unwrap()), )) - } + }, Eq => { expect_operator_argument_amount(arguments.len(), 2)?; @@ -267,7 +267,7 @@ impl Operator { } else { Ok(Value::Boolean(false)) } - } + }, Neq => { expect_operator_argument_amount(arguments.len(), 2)?; @@ -276,7 +276,7 @@ impl Operator { } else { Ok(Value::Boolean(false)) } - } + }, Gt => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number_or_string(&arguments[0])?; @@ -301,7 +301,7 @@ impl Operator { Ok(Value::Boolean(false)) } } - } + }, Lt => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number_or_string(&arguments[0])?; @@ -326,7 +326,7 @@ impl Operator { Ok(Value::Boolean(false)) } } - } + }, Geq => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number_or_string(&arguments[0])?; @@ -351,7 +351,7 @@ impl Operator { Ok(Value::Boolean(false)) } } - } + }, Leq => { expect_operator_argument_amount(arguments.len(), 2)?; expect_number_or_string(&arguments[0])?; @@ -376,7 +376,7 @@ impl Operator { Ok(Value::Boolean(false)) } } - } + }, And => { expect_operator_argument_amount(arguments.len(), 2)?; let a = expect_boolean(&arguments[0])?; @@ -387,7 +387,7 @@ impl Operator { } else { Ok(Value::Boolean(false)) } - } + }, Or => { expect_operator_argument_amount(arguments.len(), 2)?; let a = expect_boolean(&arguments[0])?; @@ -398,7 +398,7 @@ impl Operator { } else { Ok(Value::Boolean(false)) } - } + }, Not => { expect_operator_argument_amount(arguments.len(), 1)?; let a = expect_boolean(&arguments[0])?; @@ -408,7 +408,7 @@ impl Operator { } else { Ok(Value::Boolean(false)) } - } + }, Tuple => { expect_operator_argument_amount(arguments.len(), 2)?; if let Value::Tuple(tuple) = &arguments[0] { @@ -431,7 +431,7 @@ impl Operator { ])) } } - } + }, Assign => Err(EvalexprError::ContextNotManipulable), Chain => { if arguments.is_empty() { @@ -439,12 +439,12 @@ impl Operator { } Ok(arguments.get(1).cloned().unwrap_or(Value::Empty)) - } + }, Const { value } => { expect_operator_argument_amount(arguments.len(), 0)?; Ok(value.clone()) - } + }, VariableIdentifier { identifier } => { if let Some(value) = context.get_value(&identifier).cloned() { Ok(value) @@ -453,7 +453,7 @@ impl Operator { identifier.clone(), )) } - } + }, FunctionIdentifier { identifier } => { expect_operator_argument_amount(arguments.len(), 1)?; @@ -472,7 +472,7 @@ impl Operator { identifier.clone(), )) } - } + }, } } @@ -490,7 +490,7 @@ impl Operator { context.set_value(target.into(), arguments[1].clone())?; Ok(Value::Empty) - } + }, _ => self.eval(arguments, context), } } diff --git a/src/token/mod.rs b/src/token/mod.rs index 0b77c06..d3a3fe4 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -83,7 +83,7 @@ fn char_to_partial_token(c: char) -> PartialToken { } else { PartialToken::Literal(c.to_string()) } - } + }, } } @@ -233,7 +233,7 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult { cutoff = 1; Some(token) - } + }, PartialToken::Literal(literal) => { cutoff = 1; if let Ok(number) = literal.parse::() { @@ -245,38 +245,38 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult { cutoff = 1; None - } + }, PartialToken::Eq => match second { Some(PartialToken::Eq) => Some(Token::Eq), _ => { cutoff = 1; Some(Token::Assign) - } + }, }, PartialToken::ExclamationMark => match second { Some(PartialToken::Eq) => Some(Token::Eq), _ => { cutoff = 1; Some(Token::Not) - } + }, }, PartialToken::Gt => match second { Some(PartialToken::Eq) => Some(Token::Geq), _ => { cutoff = 1; Some(Token::Gt) - } + }, }, PartialToken::Lt => match second { Some(PartialToken::Eq) => Some(Token::Leq), _ => { cutoff = 1; Some(Token::Lt) - } + }, }, PartialToken::Ampersand => match second { Some(PartialToken::Ampersand) => Some(Token::And), diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 0d728d2..a60f2b6 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -421,7 +421,7 @@ pub(crate) fn tokens_to_operator_tree(tokens: Vec) -> EvalexprResult Some(Node::new(Operator::Mul)), Token::Slash => Some(Node::new(Operator::Div)), Token::Percent => Some(Node::new(Operator::Mod)), @@ -440,14 +440,14 @@ pub(crate) fn tokens_to_operator_tree(tokens: Vec) -> EvalexprResult { root.push(Node::root_node()); None - } + }, Token::RBrace => { if root.len() < 2 { return Err(EvalexprError::UnmatchedRBrace); } else { root.pop() } - } + }, Token::Comma => Some(Node::new(Operator::Tuple)), Token::Assign => Some(Node::new(Operator::Assign)), @@ -463,7 +463,7 @@ pub(crate) fn tokens_to_operator_tree(tokens: Vec) -> EvalexprResult Some(Node::new(Operator::value(Value::Float(float)))), Token::Int(int) => Some(Node::new(Operator::value(Value::Int(int)))), Token::Boolean(boolean) => Some(Node::new(Operator::value(Value::Boolean(boolean)))), diff --git a/src/value/display.rs b/src/value/display.rs index 63991e4..4e98d39 100644 --- a/src/value/display.rs +++ b/src/value/display.rs @@ -21,7 +21,7 @@ impl Display for Value { value.fmt(f)?; } write!(f, ")") - } + }, Value::Empty => write!(f, "()"), } } diff --git a/tests/integration.rs b/tests/integration.rs index 4aedc2a..0300454 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -311,7 +311,7 @@ fn test_regex_functions() { Err(EvalexprError::InvalidRegex { regex, message }) => { assert_eq!(regex, "["); assert!(message.contains("unclosed character class")); - } + }, v => panic!(v), }; assert_eq!(