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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use ore_encoding_rs::{
    encode_eq, encode_gt, encode_gte, encode_lt, encode_lte, OrePlaintext, OreRange,
};

use crate::record::Value;

use super::mapping::{FilterMapping, Mapping, OreMapping};

pub enum RangeOperator {
    Eq,
    Lt,
    Lte,
    Gt,
    Gte,
}

impl RangeOperator {
    pub fn encode_plaintext(&self, plaintext: OrePlaintext<u64>) -> OreRange<u64> {
        match self {
            Self::Eq => encode_eq(plaintext),
            Self::Lt => encode_lt(plaintext),
            Self::Lte => encode_lte(plaintext),
            Self::Gt => encode_gt(plaintext),
            Self::Gte => encode_gte(plaintext),
        }
    }
}

pub enum RangeValue {
    Single {
        operator: RangeOperator,
        value: Value,
    },
    Between {
        min: Value,
        max: Value,
    },
}

pub enum QueryKind {
    Exact { value: Value },
    Range { value: RangeValue },
    Match { value: String },
    DynamicMatch { value: String },
    FieldDynamicMatch { field_name: String, value: String },
}

impl QueryKind {
    pub fn is_for_ore_mapping(&self, mapping: &OreMapping) -> bool {
        matches!(
            (self, mapping),
            (QueryKind::Exact { .. }, OreMapping::Exact { .. })
                | (QueryKind::Range { .. }, OreMapping::Range { .. })
                | (QueryKind::Match { .. }, OreMapping::OreMatch { .. })
                | (
                    QueryKind::DynamicMatch { .. },
                    OreMapping::DynamicOreMatch { .. }
                )
                | (
                    QueryKind::FieldDynamicMatch { .. },
                    OreMapping::FieldDynamicOreMatch { .. }
                )
        )
    }

    pub fn is_for_filter_mapping(&self, mapping: &FilterMapping) -> bool {
        matches!(
            (self, mapping),
            (QueryKind::Match { .. }, FilterMapping::FilterMatch { .. })
                | (
                    QueryKind::DynamicMatch { .. },
                    FilterMapping::DynamicFilterMatch { .. }
                )
                | (
                    QueryKind::FieldDynamicMatch { .. },
                    FilterMapping::FieldDynamicFilterMatch { .. }
                )
        )
    }

    /// Check whether the current [`QueryKind`] can be used against a certain [`Mapping`]
    pub fn is_for_mapping(&self, mapping: &Mapping) -> bool {
        match mapping {
            Mapping::Filter(f) => self.is_for_filter_mapping(f),
            Mapping::Ore(o) => self.is_for_ore_mapping(o),
        }
    }

    /// Get the human readable name of the current [`QueryKind`]
    pub fn name(&self) -> &'static str {
        match self {
            QueryKind::Exact { .. } => "exact",
            QueryKind::Range { .. } => "range",
            QueryKind::Match { .. } => "match",
            QueryKind::DynamicMatch { .. } => "dynamic-match",
            QueryKind::FieldDynamicMatch { .. } => "field-dynamic-match",
        }
    }
}

pub enum Query {
    Basic { index_name: String, kind: QueryKind },
    All { conditions: Vec<Query> },
}