Skip to content

Commit

Permalink
Mutable implementation if rewrite_ast_clause()
Browse files Browse the repository at this point in the history
  • Loading branch information
bazhenov committed Oct 9, 2022
1 parent 5c24487 commit 3058300
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions query-grammar/src/query_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,27 @@ pub fn parse_to_ast<'a>() -> impl Parser<&'a str, Output = UserInputAst> {
spaces()
.with(optional(ast()).skip(eof()))
.map(|opt_ast| opt_ast.unwrap_or_else(UserInputAst::empty_query))
.map(|ast| rewrite_ast(ast))
.map(rewrite_ast)
}

/// Removes unnecessary children clauses in AST
///
/// Motivated by [issue #1433](https://github.com/quickwit-oss/tantivy/issues/1433)
fn rewrite_ast(input: UserInputAst) -> UserInputAst {
if let UserInputAst::Clause(terms) = input {
UserInputAst::Clause(terms.into_iter().map(rewrite_ast_clause).collect())
} else {
input
fn rewrite_ast(mut input: UserInputAst) -> UserInputAst {
if let UserInputAst::Clause(terms) = &mut input {
for term in terms {
rewrite_ast_clause(term);
}
}
input
}

fn rewrite_ast_clause(input: (Option<Occur>, UserInputAst)) -> (Option<Occur>, UserInputAst) {
fn rewrite_ast_clause(input: &mut (Option<Occur>, UserInputAst)) {
match input {
(None, UserInputAst::Clause(mut clauses)) if clauses.len() == 1 => clauses.remove(0),
other => other,
(None, UserInputAst::Clause(ref mut clauses)) if clauses.len() == 1 => {
*input = clauses.pop().unwrap(); // safe because clasues.len() == 1
}
_ => {}
}
}

Expand Down

0 comments on commit 3058300

Please sign in to comment.