121 lines
2.8 KiB
Rust
Raw Normal View History

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;
2018-12-10 20:14:16 +01:00
mod document_id;
2018-10-10 16:57:21 +02:00
use std::cmp::Ordering;
use crate::RawDocument;
2018-10-10 16:57:21 +02:00
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-12-10 20:14:16 +01:00
document_id::DocumentId,
2018-10-10 16:57:21 +02:00
};
pub trait Criterion: Send + Sync {
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering;
2018-10-10 16:57:21 +02:00
fn name(&self) -> &'static str;
2018-10-11 14:04:41 +02:00
#[inline]
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
self.evaluate(lhs, rhs) == Ordering::Equal
2018-10-11 14:04:41 +02:00
}
}
2018-10-10 16:57:21 +02:00
impl<'a, T: Criterion + ?Sized + Send + Sync> Criterion for &'a T {
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
(**self).evaluate(lhs, rhs)
2018-10-10 16:57:21 +02:00
}
fn name(&self) -> &'static str {
(**self).name()
}
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
(**self).eq(lhs, rhs)
2018-10-10 16:57:21 +02:00
}
2018-10-11 14:04:41 +02:00
}
2018-10-10 16:57:21 +02:00
impl<T: Criterion + ?Sized> Criterion for Box<T> {
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
(**self).evaluate(lhs, rhs)
2018-10-10 16:57:21 +02:00
}
fn name(&self) -> &'static str {
(**self).name()
}
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
(**self).eq(lhs, rhs)
2018-10-10 16:57:21 +02:00
}
}
2018-12-31 23:20:18 +01:00
#[derive(Default)]
pub struct CriteriaBuilder<'a> {
inner: Vec<Box<dyn Criterion + 'a>>
2018-12-10 11:36:09 +01:00
}
impl<'a> CriteriaBuilder<'a>
2018-12-10 11:36:09 +01:00
{
pub fn new() -> CriteriaBuilder<'a> {
2018-12-10 11:36:09 +01:00
CriteriaBuilder { inner: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> CriteriaBuilder<'a> {
2018-12-10 11:36:09 +01:00
CriteriaBuilder { inner: Vec::with_capacity(capacity) }
}
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
pub fn add<C: 'a>(mut self, criterion: C) -> CriteriaBuilder<'a>
where C: Criterion,
2018-12-10 11:36:09 +01:00
{
self.push(criterion);
self
}
pub fn push<C: 'a>(&mut self, criterion: C)
where C: Criterion,
2018-12-10 11:36:09 +01:00
{
self.inner.push(Box::new(criterion));
}
pub fn build(self) -> Criteria<'a> {
2018-12-10 20:14:16 +01:00
Criteria { inner: self.inner }
}
}
pub struct Criteria<'a> {
inner: Vec<Box<dyn Criterion + 'a>>,
2018-12-10 20:14:16 +01:00
}
impl<'a> Default for Criteria<'a> {
2018-12-10 20:14:16 +01:00
fn default() -> Self {
CriteriaBuilder::with_capacity(7)
.add(SumOfTypos)
.add(NumberOfWords)
.add(WordsProximity)
.add(SumOfWordsAttribute)
.add(SumOfWordsPosition)
.add(Exact)
.add(DocumentId)
.build()
2018-12-10 11:36:09 +01:00
}
}
impl<'a> AsRef<[Box<dyn Criterion + 'a>]> for Criteria<'a> {
fn as_ref(&self) -> &[Box<dyn Criterion + 'a>] {
2018-12-10 20:14:16 +01:00
&self.inner
}
2018-10-10 16:57:21 +02:00
}