From 5d7ace01fb50ea28778de82a9ed022655e18c661 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 26 Sep 2023 14:01:46 +0200 Subject: [PATCH] fix | in regex splitting the argument --- src/parser.rs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 827055b..836662b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -97,22 +97,24 @@ fn values(input: &str) -> IResult<&str, Value> { } fn parse_values(input: &str) -> Result { - let values = input.split('|').map(parse_single_value).collect::, String>>()?; - Ok(match values.as_slice() { - [v] => v.clone(), - _ => Value::Multiple(values), - }) + if input.starts_with('/') { + return parse_single_value(input); + } else { + let values = input.split('|').map(parse_single_value).collect::, String>>()?; + Ok(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 { Value::Regex(Regex::new(&input.strip_prefix('/')?.strip_suffix('/')?.to_lowercase()).ok()?) } { - Some(regex) => regex, - None => Value::String(sanitize(input)?), - } - } + Err(_) => match try { Value::Regex(Regex::new(&input.strip_prefix('/')?.strip_suffix('/')?.to_lowercase()).ok()?) } { + Some(regex) => regex, + None => Value::String(sanitize(input)?), + }, }) } @@ -348,4 +350,15 @@ mod tests { assert_eq!(rest, ""); assert_eq!(filter, RawCardFilter(Field::Text, Operator::Equal, Value::String("destroy that target".into()))); } + + #[test] + fn regex_should_have_precedence_over_split() { + let RawCardFilter(field, op, value) = parse_raw_filters("o:/(if|when) this card is synchro summoned:/").unwrap().1[0].clone(); + 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:"), + _ => panic!("Should have been a regex"), + } + } }