Skip to content

Commit

Permalink
Add a constructor for wildcard queries, and types to encapsulate the …
Browse files Browse the repository at this point in the history
…flags for them
  • Loading branch information
torrancew committed Jul 20, 2024
1 parent b2e1c8d commit 2586759
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cpp/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#define _XAPIAN_SHIM_H

namespace shim {
enum WildcardLimitBehavior {
WILDCARD_LIMIT_ERROR = Xapian::Query::WILDCARD_LIMIT_ERROR,
WILDCARD_LIMIT_FIRST = Xapian::Query::WILDCARD_LIMIT_FIRST,
WILDCARD_LIMIT_MOST_FREQUENT = Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT,
};

class FfiExpandDecider : public Xapian::ExpandDecider {
public:
FfiExpandDecider() : Xapian::ExpandDecider() {}
Expand Down Expand Up @@ -100,6 +106,8 @@ namespace shim {
inline void term_iterator_increment(Xapian::TermIterator &it) { it++; }
inline std::string term_iterator_term(const Xapian::TermIterator &it) { return *it; }

inline int wildcard_limit_behavior_to_int(const WildcardLimitBehavior b) { return b; }

inline const Xapian::Database& writable_database_upcast(const Xapian::WritableDatabase &db) { return db; }
}

Expand Down
75 changes: 75 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ impl Query {
)
}

pub fn wildcard(
pattern: impl AsRef<str>,
max_expansion: impl Into<Option<u32>>,
limit_behavior: impl Into<Option<WildcardLimitBehavior>>,
combiner: impl Into<Option<WildcardCombiner>>,
) -> Self {
cxx::let_cxx_string!(pattern = pattern.as_ref());
let max_expansion = max_expansion.into().unwrap_or(0);
let limit_behavior = limit_behavior
.into()
.unwrap_or(WildcardLimitBehavior::Error);
let combiner = combiner.into().unwrap_or(WildcardCombiner::Synonym);
Self(
ffi::Query::new11(
Operator::Wildcard.into(),
&pattern,
max_expansion.into(),
limit_behavior.into(),
Operator::from(combiner).into(),
)
.within_box(),
)
}

pub fn is_invalid(&self) -> bool {
self.operator() == Operator::Invalid
}
Expand Down Expand Up @@ -432,3 +456,54 @@ impl Default for QueryParser {
Self(ffi::QueryParser::new2().within_box())
}
}

pub enum WildcardCombiner {
Synonym,
Or,
Max,
}

impl From<WildcardCombiner> for Operator {
fn from(value: WildcardCombiner) -> Self {
match value {
WildcardCombiner::Synonym => Operator::Synonym,
WildcardCombiner::Or => Operator::Or,
WildcardCombiner::Max => Operator::Max,
}
}
}

#[derive(Clone, Copy, Debug)]
pub enum WildcardLimitBehavior {
Error,
FirstN,
MostFrequent,
}

impl From<WildcardLimitBehavior> for c_int {
fn from(value: WildcardLimitBehavior) -> Self {
use ffi::shim::WildcardLimitBehavior::*;
use WildcardLimitBehavior::*;

let ffi = match value {
Error => WILDCARD_LIMIT_ERROR,
FirstN => WILDCARD_LIMIT_FIRST,
MostFrequent => WILDCARD_LIMIT_MOST_FREQUENT,
};

ffi::shim::wildcard_limit_behavior_to_int(ffi)
}
}

impl From<ffi::shim::WildcardLimitBehavior> for WildcardLimitBehavior {
fn from(value: ffi::shim::WildcardLimitBehavior) -> Self {
use ffi::shim::WildcardLimitBehavior::*;
use WildcardLimitBehavior::*;

match value {
WILDCARD_LIMIT_ERROR => Error,
WILDCARD_LIMIT_FIRST => FirstN,
WILDCARD_LIMIT_MOST_FREQUENT => MostFrequent,
}
}
}

0 comments on commit 2586759

Please sign in to comment.