From dbf3949af62f44814e70a1f2f226ec592d331833 Mon Sep 17 00:00:00 2001 From: Quest Date: Fri, 12 Apr 2019 23:13:37 +0200 Subject: [PATCH] Namespace string builtins into "str::". --- README.md | 10 +++++----- src/function/builtin.rs | 29 +++++++++++++++-------------- tests/integration.rs | 24 ++++++++++++------------ 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7448d3d..9421533 100644 --- a/README.md +++ b/README.md @@ -214,12 +214,12 @@ This crate offers a set of builtin functions. |------------|-----------------|-------------| | min | >= 1 | Returns the minimum of the arguments | | max | >= 1 | Returns the maximum of the arguments | -| to_lowercase | 1 | Returns lower-case version of string | | len | 1 | Return the character length of string argument | -| regex_matches | 2 | Returns true if first string argument matches regex in second | -| regex_replace | 3 | Returns string with matches replaced by third argument | -| trim | 1 | Strips whitespace from start and end of string | -| to_uppercase | 1 | Returns upper-case version of string | +| str::regex_matches | 2 | Returns true if first string argument matches regex in second | +| str::regex_replace | 3 | Returns string with matches replaced by third argument | +| str::to_lowercase | 1 | Returns lower-case version of string | +| str::to_uppercase | 1 | Returns upper-case version of string | +| str::trim | 1 | Strips whitespace from start and end of string | The `min` and `max` functions can deal with a mixture of integer and floating point arguments. They return the result as the type it was passed into the function. The regex functions require diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 38ccb56..717b7db 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -58,15 +58,6 @@ pub fn builtin_function(identifier: &str) -> Option { }), )), - // string functions - - "to_lowercase" => Some(Function::new( - Some(1), - Box::new(|arguments| { - let subject = expect_string(&arguments[0])?; - Ok(Value::from(subject.to_lowercase())) - }), - )), "len" => Some(Function::new( Some(1), Box::new(|arguments| { @@ -74,8 +65,11 @@ pub fn builtin_function(identifier: &str) -> Option { Ok(Value::from(subject.len() as i64)) }), )), + + // string functions + #[cfg(feature = "regex_support")] - "regex_matches" => Some(Function::new( + "str::regex_matches" => Some(Function::new( Some(2), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; @@ -87,7 +81,7 @@ pub fn builtin_function(identifier: &str) -> Option { }), )), #[cfg(feature = "regex_support")] - "regex_replace" => Some(Function::new( + "str::regex_replace" => Some(Function::new( Some(3), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; @@ -99,20 +93,27 @@ pub fn builtin_function(identifier: &str) -> Option { } }), )), - "trim" => Some(Function::new( + "str::to_lowercase" => Some(Function::new( Some(1), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; - Ok(Value::from(subject.trim())) + Ok(Value::from(subject.to_lowercase())) }), )), - "to_uppercase" => Some(Function::new( + "str::to_uppercase" => Some(Function::new( Some(1), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; Ok(Value::from(subject.to_uppercase())) }), )), + "str::trim" => Some(Function::new( + Some(1), + Box::new(|arguments| { + let subject = expect_string(&arguments[0])?; + Ok(Value::from(subject.trim())) + }), + )), _ => None, } } diff --git a/tests/integration.rs b/tests/integration.rs index 6d308b7..2c90c9f 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -287,36 +287,36 @@ fn test_builtin_functions() { eval("max(4.0, 3)"), Ok(Value::Float(4.0)) ); - assert_eq!( - eval("to_lowercase(\"FOOBAR\")"), - Ok(Value::from("foobar")) - ); assert_eq!( eval("len(\"foobar\")"), Ok(Value::Int(6)) ); assert_eq!( - eval("trim(\" foo bar \")"), - Ok(Value::from("foo bar")) + eval("str::to_lowercase(\"FOOBAR\")"), + Ok(Value::from("foobar")) ); assert_eq!( - eval("to_uppercase(\"foobar\")"), + eval("str::to_uppercase(\"foobar\")"), Ok(Value::from("FOOBAR")) ); + assert_eq!( + eval("str::trim(\" foo bar \")"), + Ok(Value::from("foo bar")) + ); } #[test] #[cfg(feature = "regex_support")] fn test_regex_functions() { assert_eq!( - eval("regex_matches(\"foobar\", \"[ob]{3}\")"), + eval("str::regex_matches(\"foobar\", \"[ob]{3}\")"), Ok(Value::Boolean(true)) ); assert_eq!( - eval("regex_matches(\"gazonk\", \"[ob]{3}\")"), + eval("str::regex_matches(\"gazonk\", \"[ob]{3}\")"), Ok(Value::Boolean(false)) ); - match eval("regex_matches(\"foo\", \"[\")") { + match eval("str::regex_matches(\"foo\", \"[\")") { Err(EvalexprError::InvalidRegex{ regex, message }) => { assert_eq!(regex, "["); assert!(message.contains("unclosed character class")); @@ -324,11 +324,11 @@ fn test_regex_functions() { v => panic!(v), }; assert_eq!( - eval("regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"), + eval("str::regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"), Ok(Value::String("boobar".to_owned())) ); assert_eq!( - eval("regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"), + eval("str::regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"), Ok(Value::String("foobar".to_owned())) ); }