From a7e254e81d085f24e6a019bff3899bdca2728859 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 27 Aug 2020 09:59:29 +1000 Subject: [PATCH] catch leading/trailing boolean operators https://forums.ankiweb.net/t/is-it-only-me-anki-giving-corrupt-collection-error/2424/20 --- rslib/src/search/parser.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rslib/src/search/parser.rs b/rslib/src/search/parser.rs index a450f26f8..d160a5c69 100644 --- a/rslib/src/search/parser.rs +++ b/rslib/src/search/parser.rs @@ -168,6 +168,9 @@ fn group_inner(input: &str) -> IResult<&str, Vec> { 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 ")?,