Skip to content

Commit

Permalink
re-write query parser using nom
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkeldenker committed Mar 18, 2024
1 parent 642e64d commit f1bc1a5
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 188 deletions.
14 changes: 7 additions & 7 deletions crates/core/src/bangs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl Bangs {
}
}

pub fn get(&self, terms: &[Box<Term>]) -> Option<BangHit> {
pub fn get(&self, terms: &[Term]) -> Option<BangHit> {
for possible_bang in terms.iter().filter_map(|term| {
if let Term::PossibleBang(possible_bang) = term.as_ref() {
if let Term::PossibleBang(possible_bang) = term {
Some(possible_bang)
} else {
None
Expand All @@ -122,13 +122,13 @@ impl Bangs {
terms
.iter()
.filter(|term| {
if let Term::PossibleBang(bang) = term.as_ref() {
if let Term::PossibleBang(bang) = term {
bang != possible_bang
} else {
true
}
})
.map(|term| term.as_ref().to_string()),
.map(|term| term.to_string()),
" ".to_string(),
)
.collect::<String>();
Expand Down Expand Up @@ -172,11 +172,11 @@ mod tests {
}]"#,
);

assert_eq!(bangs.get(&parse("no bangs")), None);
assert_eq!(bangs.get(&parse("!no bangs")), None);
assert_eq!(bangs.get(&parse("no bangs").unwrap()), None);
assert_eq!(bangs.get(&parse("!no bangs").unwrap()), None);

assert_eq!(
bangs.get(&parse("!ty bangs")),
bangs.get(&parse("!ty bangs").unwrap()),
Some(BangHit {
bang: Bang {
category: Some("Multimedia".to_string()),
Expand Down
9 changes: 4 additions & 5 deletions crates/core/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const MAX_SIMILAR_TERMS: usize = 10;

#[derive(Clone, Debug)]
pub struct Query {
#[allow(clippy::vec_box)]
terms: Vec<Box<Term>>,
terms: Vec<Term>,
simple_terms_text: Vec<String>,
tantivy_query: Box<BooleanQuery>,
host_rankings: HostRankings,
Expand All @@ -61,7 +60,7 @@ pub struct Query {

impl Query {
pub fn parse(ctx: &Ctx, query: &SearchQuery, index: &InvertedIndex) -> Result<Query> {
let parsed_terms = parser::parse(&query.query);
let parsed_terms = parser::parse(&query.query)?;
let mut term_count = HashMap::new();
let mut terms = Vec::new();

Expand All @@ -79,7 +78,7 @@ impl Query {
.clone()
.into_iter()
.map(|term| CompoundAwareTerm {
term: *term,
term,
adjacent_terms: Vec::new(),
})
.collect();
Expand Down Expand Up @@ -185,7 +184,7 @@ impl Query {
&self.simple_terms_text
}

pub fn terms(&self) -> &[Box<Term>] {
pub fn terms(&self) -> &[Term] {
&self.terms
}

Expand Down
Loading

0 comments on commit f1bc1a5

Please sign in to comment.