Format code
This commit is contained in:
parent
b85e8e1b75
commit
21b308f3ae
@ -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()));
|
||||
|
@ -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: {:?}",
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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<Vec<T
|
||||
PartialToken::Token(token) => {
|
||||
cutoff = 1;
|
||||
Some(token)
|
||||
}
|
||||
},
|
||||
PartialToken::Literal(literal) => {
|
||||
cutoff = 1;
|
||||
if let Ok(number) = literal.parse::<IntType>() {
|
||||
@ -245,38 +245,38 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
|
||||
} else {
|
||||
Some(Token::Identifier(literal.to_string()))
|
||||
}
|
||||
}
|
||||
},
|
||||
PartialToken::Whitespace => {
|
||||
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),
|
||||
|
@ -421,7 +421,7 @@ pub(crate) fn tokens_to_operator_tree(tokens: Vec<Token>) -> EvalexprResult<Node
|
||||
} else {
|
||||
Some(Node::new(Operator::Neg))
|
||||
}
|
||||
}
|
||||
},
|
||||
Token::Star => 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<Token>) -> EvalexprResult<Node
|
||||
Token::LBrace => {
|
||||
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<Token>) -> EvalexprResult<Node
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
},
|
||||
Token::Float(float) => 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)))),
|
||||
|
@ -21,7 +21,7 @@ impl Display for Value {
|
||||
value.fmt(f)?;
|
||||
}
|
||||
write!(f, ")")
|
||||
}
|
||||
},
|
||||
Value::Empty => write!(f, "()"),
|
||||
}
|
||||
}
|
||||
|
@ -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!(
|
||||
|
Loading…
Reference in New Issue
Block a user