Format code

This commit is contained in:
Sebastian Schmidt 2019-05-04 13:54:19 +02:00
parent 6f533ca925
commit 502ec0adce
5 changed files with 156 additions and 177 deletions

View File

@ -9,8 +9,7 @@ use Value;
pub fn builtin_function(identifier: &str) -> Option<Function> {
match identifier {
"min" => Some(Function::new(
Box::new(|argument| {
"min" => Some(Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
let mut min_int = IntType::max_value();
let mut min_float = 1.0f64 / 0.0f64;
@ -31,10 +30,8 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
} else {
Ok(Value::Float(min_float))
}
}),
)),
"max" => Some(Function::new(
Box::new(|argument| {
}))),
"max" => Some(Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
let mut max_int = IntType::min_value();
let mut max_float = -1.0f64 / 0.0f64;
@ -55,20 +52,16 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
} else {
Ok(Value::Float(max_float))
}
}),
)),
}))),
"len" => Some(Function::new(
Box::new(|argument| {
"len" => Some(Function::new(Box::new(|argument| {
let subject = expect_string(argument)?;
Ok(Value::from(subject.len() as i64))
}),
)),
}))),
// string functions
#[cfg(feature = "regex_support")]
"str::regex_matches" => Some(Function::new(
Box::new(|argument| {
"str::regex_matches" => Some(Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
let subject = expect_string(&arguments[0])?;
@ -80,11 +73,9 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
format!("{}", err),
)),
}
}),
)),
}))),
#[cfg(feature = "regex_support")]
"str::regex_replace" => Some(Function::new(
Box::new(|argument| {
"str::regex_replace" => Some(Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
let subject = expect_string(&arguments[0])?;
@ -97,26 +88,19 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
format!("{}", err),
)),
}
}),
)),
"str::to_lowercase" => Some(Function::new(
Box::new(|argument| {
}))),
"str::to_lowercase" => Some(Function::new(Box::new(|argument| {
let subject = expect_string(argument)?;
Ok(Value::from(subject.to_lowercase()))
}),
)),
"str::to_uppercase" => Some(Function::new(
Box::new(|argument| {
}))),
"str::to_uppercase" => Some(Function::new(Box::new(|argument| {
let subject = expect_string(argument)?;
Ok(Value::from(subject.to_uppercase()))
}),
)),
"str::trim" => Some(Function::new(
Box::new(|argument| {
}))),
"str::trim" => Some(Function::new(Box::new(|argument| {
let subject = expect_string(argument)?;
Ok(Value::from(subject.trim()))
}),
)),
}))),
_ => None,
}
}

View File

@ -1,6 +1,6 @@
use std::fmt;
use error::{EvalexprResult};
use error::EvalexprResult;
use value::Value;
pub(crate) mod builtin;
@ -27,12 +27,8 @@ impl Function {
/// Creates a user-defined function.
///
/// The `function` is a boxed function that takes a `Value` and returns a `EvalexprResult<Value, Error>`.
pub fn new(
function: Box<Fn(&Value) -> EvalexprResult<Value>>,
) -> Self {
Self {
function,
}
pub fn new(function: Box<Fn(&Value) -> EvalexprResult<Value>>) -> Self {
Self { function }
}
pub(crate) fn call(&self, argument: &Value) -> EvalexprResult<Value> {
@ -42,9 +38,6 @@ impl Function {
impl fmt::Debug for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"Function {{ [...] }}"
)
write!(f, "Function {{ [...] }}")
}
}

View File

@ -420,9 +420,7 @@ impl Operator {
Ok(Value::Boolean(false))
}
},
Tuple => {
Ok(Value::Tuple(arguments.into()))
},
Tuple => Ok(Value::Tuple(arguments.into())),
Assign => Err(EvalexprError::ContextNotManipulable),
Chain => {
if arguments.is_empty() {

View File

@ -410,11 +410,16 @@ impl Node {
}
}
fn collapse_root_stack_to(root_stack: &mut Vec<Node>, mut root: Node, collapse_goal: &Node) -> EvalexprResult<Node> {
fn collapse_root_stack_to(
root_stack: &mut Vec<Node>,
mut root: Node,
collapse_goal: &Node,
) -> EvalexprResult<Node> {
loop {
if let Some(mut potential_higher_root) = root_stack.pop() {
// TODO I'm not sure about this >, as I have no example for different sequence operators with the same precedence
if potential_higher_root.operator().precedence() > collapse_goal.operator().precedence() {
if potential_higher_root.operator().precedence() > collapse_goal.operator().precedence()
{
potential_higher_root.children.push(root);
root = potential_higher_root;
} else {

View File

@ -137,8 +137,7 @@ fn test_functions() {
context
.set_function(
"sub2".to_string(),
Function::new(
Box::new(|argument| {
Function::new(Box::new(|argument| {
if let Value::Int(int) = argument {
Ok(Value::Int(int - 2))
} else if let Value::Float(float) = argument {
@ -146,8 +145,7 @@ fn test_functions() {
} else {
Err(EvalexprError::expected_number(argument.clone()))
}
}),
),
})),
)
.unwrap();
context
@ -170,8 +168,7 @@ fn test_n_ary_functions() {
context
.set_function(
"sub2".into(),
Function::new(
Box::new(|argument| {
Function::new(Box::new(|argument| {
if let Value::Int(int) = argument {
Ok(Value::Int(int - 2))
} else if let Value::Float(float) = argument {
@ -179,15 +176,13 @@ fn test_n_ary_functions() {
} else {
Err(EvalexprError::expected_number(argument.clone()))
}
}),
),
})),
)
.unwrap();
context
.set_function(
"avg".into(),
Function::new(
Box::new(|argument| {
Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -199,15 +194,13 @@ fn test_n_ary_functions() {
(arguments[0].as_float()? + arguments[1].as_float()?) / 2.0,
))
}
}),
),
})),
)
.unwrap();
context
.set_function(
"muladd".into(),
Function::new(
Box::new(|argument| {
Function::new(Box::new(|argument| {
let arguments = expect_tuple(argument)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -223,28 +216,28 @@ fn test_n_ary_functions() {
+ arguments[2].as_float()?,
))
}
}),
),
})),
)
.unwrap();
context
.set_function(
"count".into(),
Function::new(
Box::new(|arguments| {
match arguments {
Function::new(Box::new(|arguments| match arguments {
Value::Tuple(tuple) => Ok(Value::from(tuple.len() as IntType)),
Value::Empty => Ok(Value::from(0)),
_ => Ok(Value::from(1)),
}
}),
),
})),
)
.unwrap();
context
.set_value("five".to_string(), Value::Int(5))
.unwrap();
context.set_function("function_four".into(), Function::new(Box::new(|_| {Ok(Value::Int(4))}))).unwrap();
context
.set_function(
"function_four".into(),
Function::new(Box::new(|_| Ok(Value::Int(4)))),
)
.unwrap();
assert_eq!(eval_with_context("avg(7, 5)", &context), Ok(Value::Int(6)));
assert_eq!(
@ -264,13 +257,19 @@ fn test_n_ary_functions() {
Ok(Value::Int(14))
);
assert_eq!(eval_with_context("count()", &context), Ok(Value::Int(0)));
assert_eq!(eval_with_context("count((1, 2, 3))", &context), Ok(Value::Int(3)));
assert_eq!(
eval_with_context("count((1, 2, 3))", &context),
Ok(Value::Int(3))
);
assert_eq!(
eval_with_context("count(3, 5.5, 2)", &context),
Ok(Value::Int(3))
);
assert_eq!(eval_with_context("count 5", &context), Ok(Value::Int(1)));
assert_eq!(eval_with_context("function_four()", &context), Ok(Value::Int(4)));
assert_eq!(
eval_with_context("function_four()", &context),
Ok(Value::Int(4))
);
}
#[test]