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

View File

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

View File

@ -420,9 +420,7 @@ impl Operator {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
} }
}, },
Tuple => { Tuple => Ok(Value::Tuple(arguments.into())),
Ok(Value::Tuple(arguments.into()))
},
Assign => Err(EvalexprError::ContextNotManipulable), Assign => Err(EvalexprError::ContextNotManipulable),
Chain => { Chain => {
if arguments.is_empty() { 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 { loop {
if let Some(mut potential_higher_root) = root_stack.pop() { 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 // 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); potential_higher_root.children.push(root);
root = potential_higher_root; root = potential_higher_root;
} else { } else {

View File

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