mod sum_of_typos; mod number_of_words; mod words_proximity; mod sum_of_words_attribute; mod sum_of_words_position; mod exact; mod sort_by; mod document_id; use std::cmp::Ordering; use std::ops::Deref; use rocksdb::DB; use crate::database::DatabaseView; use crate::rank::Document; pub use self::{ sum_of_typos::SumOfTypos, number_of_words::NumberOfWords, words_proximity::WordsProximity, sum_of_words_attribute::SumOfWordsAttribute, sum_of_words_position::SumOfWordsPosition, exact::Exact, sort_by::SortBy, document_id::DocumentId, }; pub trait Criterion where D: Deref { fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering; #[inline] fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { self.evaluate(lhs, rhs, view) == Ordering::Equal } } impl<'a, D, T: Criterion + ?Sized> Criterion for &'a T where D: Deref { fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering { (**self).evaluate(lhs, rhs, view) } fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { (**self).eq(lhs, rhs, view) } } impl + ?Sized> Criterion for Box where D: Deref { fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering { (**self).evaluate(lhs, rhs, view) } fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { (**self).eq(lhs, rhs, view) } } #[derive(Default)] pub struct CriteriaBuilder where D: Deref { inner: Vec>> } impl CriteriaBuilder where D: Deref { pub fn new() -> CriteriaBuilder { CriteriaBuilder { inner: Vec::new() } } pub fn with_capacity(capacity: usize) -> CriteriaBuilder { CriteriaBuilder { inner: Vec::with_capacity(capacity) } } pub fn reserve(&mut self, additional: usize) { self.inner.reserve(additional) } pub fn add(mut self, criterion: C) -> CriteriaBuilder where C: 'static + Criterion, { self.push(criterion); self } pub fn push(&mut self, criterion: C) where C: 'static + Criterion, { self.inner.push(Box::new(criterion)); } pub fn build(self) -> Criteria { Criteria { inner: self.inner } } } pub struct Criteria where D: Deref { inner: Vec>>, } impl Default for Criteria where D: Deref { fn default() -> Self { CriteriaBuilder::with_capacity(7) .add(SumOfTypos) .add(NumberOfWords) .add(WordsProximity) .add(SumOfWordsAttribute) .add(SumOfWordsPosition) .add(Exact) .add(DocumentId) .build() } } impl AsRef<[Box>]> for Criteria where D: Deref { fn as_ref(&self) -> &[Box>] { &self.inner } }