2018-10-10 16:57:21 +02:00
|
|
|
mod sum_of_typos;
|
|
|
|
mod number_of_words;
|
|
|
|
mod words_proximity;
|
|
|
|
mod sum_of_words_attribute;
|
|
|
|
mod sum_of_words_position;
|
|
|
|
mod exact;
|
|
|
|
|
|
|
|
use std::vec;
|
|
|
|
use std::cmp::Ordering;
|
2018-12-07 14:41:06 +01:00
|
|
|
|
|
|
|
use crate::database::DatabaseView;
|
2018-10-10 16:57:21 +02:00
|
|
|
use crate::rank::Document;
|
|
|
|
|
|
|
|
pub use self::{
|
2018-10-11 14:04:41 +02:00
|
|
|
sum_of_typos::SumOfTypos,
|
|
|
|
number_of_words::NumberOfWords,
|
|
|
|
words_proximity::WordsProximity,
|
|
|
|
sum_of_words_attribute::SumOfWordsAttribute,
|
|
|
|
sum_of_words_position::SumOfWordsPosition,
|
|
|
|
exact::Exact,
|
2018-10-10 16:57:21 +02:00
|
|
|
};
|
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
pub trait Criterion {
|
|
|
|
#[inline]
|
2018-12-07 14:41:06 +01:00
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering;
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
#[inline]
|
2018-12-07 14:41:06 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
|
|
|
self.evaluate(lhs, rhs, view) == Ordering::Equal
|
2018-10-11 14:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
impl<'a, T: Criterion + ?Sized> Criterion for &'a T {
|
2018-12-07 14:41:06 +01:00
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering {
|
|
|
|
(**self).evaluate(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:41:06 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
|
|
|
(**self).eq(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
2018-10-11 14:04:41 +02:00
|
|
|
}
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
2018-12-07 14:41:06 +01:00
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering {
|
|
|
|
(**self).evaluate(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 14:41:06 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
|
|
|
(**self).eq(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct DocumentId;
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
impl Criterion for DocumentId {
|
2018-12-07 14:41:06 +01:00
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
2018-10-11 14:04:41 +02:00
|
|
|
lhs.id.cmp(&rhs.id)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 16:42:19 +02:00
|
|
|
// TODO there is too much Box here, can we use
|
|
|
|
// static references or static closures
|
2018-10-11 14:04:41 +02:00
|
|
|
pub fn default() -> Vec<Box<dyn Criterion>> {
|
|
|
|
vec![
|
|
|
|
Box::new(SumOfTypos),
|
|
|
|
Box::new(NumberOfWords),
|
|
|
|
Box::new(WordsProximity),
|
|
|
|
Box::new(SumOfWordsAttribute),
|
|
|
|
Box::new(SumOfWordsPosition),
|
|
|
|
Box::new(Exact),
|
2018-10-18 17:29:27 +02:00
|
|
|
Box::new(DocumentId),
|
2018-10-11 14:04:41 +02:00
|
|
|
]
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|