catch leading/trailing boolean operators

https://forums.ankiweb.net/t/is-it-only-me-anki-giving-corrupt-collection-error/2424/20
This commit is contained in:
Damien Elmes 2020-08-27 09:59:29 +10:00
parent 96f9ad1f25
commit a7e254e81d

View File

@ -168,6 +168,9 @@ fn group_inner(input: &str) -> IResult<&str, Vec<Node>> {
if nodes.is_empty() {
Err(nom::Err::Error((remaining, nom::error::ErrorKind::Many1)))
} else if matches!(nodes.last().unwrap(), Node::And | Node::Or) {
// no trailing and/or
Err(nom::Err::Failure(("", nom::error::ErrorKind::NoneOf)))
} else {
// chomp any trailing whitespace
let (remaining, _) = whitespace0(remaining)?;
@ -434,6 +437,11 @@ mod test {
assert_eq!(parse("")?, vec![Search(SearchNode::WholeCollection)]);
assert_eq!(parse(" ")?, vec![Search(SearchNode::WholeCollection)]);
// leading/trailing boolean operators
assert!(parse("foo and").is_err());
assert!(parse("and foo").is_err());
assert!(parse("and").is_err());
// leading/trailing/interspersed whitespace
assert_eq!(
parse(" t t2 ")?,