fix | in regex splitting the argument

This commit is contained in:
kageru 2023-09-26 14:01:46 +02:00
parent 8947a11907
commit 5d7ace01fb

@ -97,22 +97,24 @@ fn values(input: &str) -> IResult<&str, Value> {
}
fn parse_values(input: &str) -> Result<Value, String> {
if input.starts_with('/') {
return parse_single_value(input);
} else {
let values = input.split('|').map(parse_single_value).collect::<Result<Vec<Value>, String>>()?;
Ok(match values.as_slice() {
[v] => v.clone(),
_ => Value::Multiple(values),
})
}
}
fn parse_single_value(input: &str) -> Result<Value, String> {
Ok(match input.parse() {
Ok(n) => Value::Numerical(n),
Err(_) => {
match try { Value::Regex(Regex::new(&input.strip_prefix('/')?.strip_suffix('/')?.to_lowercase()).ok()?) } {
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"),
}
}
}