reject invalid regex without fallback to plain text
This commit is contained in:
parent
27ad10c295
commit
51eecc5372
@ -97,24 +97,21 @@ fn values(input: &str) -> IResult<&str, Value> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_values(input: &str) -> Result<Value, String> {
|
fn parse_values(input: &str) -> Result<Value, String> {
|
||||||
if input.starts_with('/') {
|
Ok(if let Some(regex) = input.strip_prefix('/').and_then(|i| i.strip_suffix('/')) {
|
||||||
parse_single_value(input)
|
Value::Regex(Regex::new(&format!("(?i){regex}")).map_err(|_| format!("Invalid regex: {regex}"))?)
|
||||||
} else {
|
} else {
|
||||||
let values = input.split('|').map(parse_single_value).collect::<Result<Vec<Value>, String>>()?;
|
let values = input.split('|').map(parse_single_value).collect::<Result<Vec<Value>, String>>()?;
|
||||||
Ok(match values.as_slice() {
|
match values.as_slice() {
|
||||||
[v] => v.clone(),
|
[v] => v.clone(),
|
||||||
_ => Value::Multiple(values),
|
_ => Value::Multiple(values),
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_single_value(input: &str) -> Result<Value, String> {
|
fn parse_single_value(input: &str) -> Result<Value, String> {
|
||||||
Ok(match input.parse() {
|
Ok(match input.parse() {
|
||||||
Ok(n) => Value::Numerical(n),
|
Ok(n) => Value::Numerical(n),
|
||||||
Err(_) => match try { Regex::new(&input.strip_prefix('/')?.strip_suffix('/')?.to_lowercase()).ok()? } {
|
Err(_) => Value::String(sanitize(input)?),
|
||||||
Some(regex) => Value::Regex(regex),
|
|
||||||
None => Value::String(sanitize(input)?),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,7 +354,7 @@ mod tests {
|
|||||||
assert_eq!(field, Field::Text);
|
assert_eq!(field, Field::Text);
|
||||||
assert_eq!(op, Operator::Equal);
|
assert_eq!(op, Operator::Equal);
|
||||||
match value {
|
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"),
|
_ => panic!("Should have been a regex"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user