1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::grpc::api::{documents, queries};

use crate::indexer::vector::{Constraint, ConstraintCondition, TermVector, VectorTerm};

// Convert internal VectorTerm to grpc term type
// Perhaps one day our analyzer can directly generate the grpc types!
impl From<VectorTerm> for documents::Term {
    fn from(term: VectorTerm) -> Self {
        Self {
            term: term.term.to_vec(),
            link: term.link.to_vec(),
            bits: vec![], // TODO: We don't support bloom filters in Rust, yet!
        }
    }
}

impl From<TermVector> for documents::Vector {
    fn from(term_vector: TermVector) -> Self {
        Self {
            index_id: term_vector.index_id.to_vec(),
            terms: term_vector
                .terms
                .into_iter()
                .map(|term| term.into())
                .collect(),
        }
    }
}

impl From<ConstraintCondition> for queries::constraint::Condition {
    fn from(condition: ConstraintCondition) -> Self {
        match condition {
            ConstraintCondition::Exact { term } => Self::Exact(queries::Exact {
                term: term.to_vec(),
            }),
            ConstraintCondition::Range { upper, lower } => Self::Range(queries::Range {
                lower: lower.to_vec(),
                upper: upper.to_vec(),
            }),
        }
    }
}

impl From<Constraint> for queries::Constraint {
    fn from(
        Constraint {
            index_id,
            condition,
        }: Constraint,
    ) -> Self {
        Self {
            index_id: index_id.into(),
            condition: Some(condition.into()),
        }
    }
}