From 7c6b6d4319fd088e60b69a79a0c457feb9b08454 Mon Sep 17 00:00:00 2001 From: Quest Date: Fri, 12 Apr 2019 23:03:13 +0200 Subject: [PATCH] Rename matches,replace -> regex_{matches,replace}. Also simplify their error handling. --- README.md | 4 ++-- src/error/display.rs | 2 +- src/error/mod.rs | 2 +- src/function/builtin.rs | 27 ++++++--------------------- tests/integration.rs | 10 +++++----- 5 files changed, 15 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 414f0e9..46a113f 100644 --- a/README.md +++ b/README.md @@ -216,8 +216,8 @@ This crate offers a set of builtin functions. | max | >= 1 | Returns the maximum of the arguments | | downcase | 1 | Returns lower-case version of string | | len | 1 | Return the character length of string argument | -| match | 2 | Returns true if first string argument matches regex in second | -| replace | 3 | Returns string with matches replaced by third 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 | | upcase | 1 | Returns upper-case version of string | diff --git a/src/error/display.rs b/src/error/display.rs index 589af9e..496f787 100644 --- a/src/error/display.rs +++ b/src/error/display.rs @@ -84,7 +84,7 @@ impl fmt::Display for EvalexprError { ModulationError { dividend, divisor } => { write!(f, "Error modulating {} % {}", dividend, divisor) }, - InvalidRegex { regex, message } => write!(f, "Regular expression {} is invalid: {}", regex, message), + InvalidRegex { regex, message } => write!(f, "Regular expression {:?} is invalid: {:?}", regex, message), ContextNotManipulable => write!(f, "Cannot manipulate context"), IllegalEscapeSequence(string) => write!(f, "Illegal escape sequence: {}", string), CustomMessage(message) => write!(f, "Error: {}", message), diff --git a/src/error/mod.rs b/src/error/mod.rs index 7eab199..729f192 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -167,7 +167,7 @@ pub enum EvalexprError { divisor: Value, }, - /// This regular expression could not be parsed + /// A regular expression could not be parsed InvalidRegex { /// The invalid regular expression regex: String, diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 358bf28..e0d7a7d 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -7,21 +7,6 @@ use EvalexprError; use Function; use Value; -#[cfg(feature = "regex_support")] -fn regex_with_local_errors(re_str: &str) -> Result { - match Regex::new(re_str) { - Ok(re) => Ok(re), - Err(regex::Error::Syntax(message)) => - Err(EvalexprError::invalid_regex(re_str.to_string(), message)), - Err(regex::Error::CompiledTooBig(max_size)) => - Err(EvalexprError::invalid_regex( - re_str.to_string(), - format!("Regex exceeded max size {}", max_size)) - ), - Err(err) => Err(EvalexprError::CustomMessage(err.to_string())), - } -} - pub fn builtin_function(identifier: &str) -> Option { match identifier { "min" => Some(Function::new( @@ -90,27 +75,27 @@ pub fn builtin_function(identifier: &str) -> Option { }), )), #[cfg(feature = "regex_support")] - "match" => Some(Function::new( + "regex_matches" => Some(Function::new( Some(2), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; let re_str = expect_string(&arguments[1])?; - match regex_with_local_errors(re_str) { + match Regex::new(re_str) { Ok(re) => Ok(Value::Boolean(re.is_match(subject))), - Err(err) => Err(err) + Err(err) => Err(EvalexprError::invalid_regex(re_str.to_string(), format!("{}", err))) } }), )), #[cfg(feature = "regex_support")] - "replace" => Some(Function::new( + "regex_replace" => Some(Function::new( Some(3), Box::new(|arguments| { let subject = expect_string(&arguments[0])?; let re_str = expect_string(&arguments[1])?; let repl = expect_string(&arguments[2])?; - match regex_with_local_errors(re_str) { + match Regex::new(re_str) { Ok(re) => Ok(Value::String(re.replace_all(subject, repl).to_string())), - Err(err) => Err(err), + Err(err) => Err(EvalexprError::invalid_regex(re_str.to_string(), format!("{}", err))), } }), )), diff --git a/tests/integration.rs b/tests/integration.rs index 01b9a0d..13f0793 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -309,14 +309,14 @@ fn test_builtin_functions() { #[cfg(feature = "regex_support")] fn test_regex_functions() { assert_eq!( - eval("match(\"foobar\", \"[ob]{3}\")"), + eval("regex_matches(\"foobar\", \"[ob]{3}\")"), Ok(Value::Boolean(true)) ); assert_eq!( - eval("match(\"gazonk\", \"[ob]{3}\")"), + eval("regex_matches(\"gazonk\", \"[ob]{3}\")"), Ok(Value::Boolean(false)) ); - match eval("match(\"foo\", \"[\")") { + match eval("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("replace(\"foobar\", \".*?(o+)\", \"b$1\")"), + eval("regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"), Ok(Value::String("boobar".to_owned())) ); assert_eq!( - eval("replace(\"foobar\", \".*?(i+)\", \"b$1\")"), + eval("regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"), Ok(Value::String("foobar".to_owned())) ); }