Skip to content

Commit

Permalink
Mutable implementation if rewrite_ast()
Browse files Browse the repository at this point in the history
  • Loading branch information
bazhenov committed Oct 9, 2022
1 parent 5c24487 commit 88b0d2c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions query-grammar/src/query_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,23 @@ pub fn parse_to_ast<'a>() -> impl Parser<&'a str, Output = UserInputAst> {
/// 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 => {
let new_term = clauses.pop().unwrap(); // safe because clasues.len() == 1
input.0 = new_term.0;
input.1 = new_term.1;
}
_ => {}
}
}

Expand Down

0 comments on commit 88b0d2c

Please sign in to comment.