From 51eecc537279e525fdc42ea6c2d830406479f92c Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 26 Sep 2023 14:51:45 +0200 Subject: [PATCH] reject invalid regex without fallback to plain text --- src/parser.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index d3d52dc..1de912c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -97,24 +97,21 @@ fn values(input: &str) -> IResult<&str, Value> { } fn parse_values(input: &str) -> Result { - if input.starts_with('/') { - parse_single_value(input) + Ok(if let Some(regex) = input.strip_prefix('/').and_then(|i| i.strip_suffix('/')) { + Value::Regex(Regex::new(&format!("(?i){regex}")).map_err(|_| format!("Invalid regex: {regex}"))?) } else { let values = input.split('|').map(parse_single_value).collect::, String>>()?; - Ok(match values.as_slice() { + match values.as_slice() { [v] => v.clone(), _ => Value::Multiple(values), - }) - } + } + }) } fn parse_single_value(input: &str) -> Result { Ok(match input.parse() { Ok(n) => Value::Numerical(n), - Err(_) => match try { Regex::new(&input.strip_prefix('/')?.strip_suffix('/')?.to_lowercase()).ok()? } { - Some(regex) => Value::Regex(regex), - None => Value::String(sanitize(input)?), - }, + Err(_) => Value::String(sanitize(input)?), }) } @@ -357,7 +354,7 @@ mod tests { assert_eq!(field, Field::Text); assert_eq!(op, Operator::Equal); match value { - Value::Regex(r) => assert_eq!(r.as_str(), "(if|when) this card is synchro summoned:"), + Value::Regex(r) => assert_eq!(r.as_str(), "(?i)(if|when) this card is synchro summoned:"), _ => panic!("Should have been a regex"), } }