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
105
106
107
108
109
110
use ore_encoding_rs::{
    encode_eq, encode_gt, encode_gte, encode_lt, encode_lte, OrePlaintext, OreRange,
};

use crate::record::Value;

use super::mapping::Mapping;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(untagged, rename_all = "kebab-case")]
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),
        }
    }
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum RangeValue {
    Single {
        #[serde(rename = "op")]
        operator: RangeOperator,
        value: Value,
    },
    Between {
        min: Value,
        max: Value,
    },
}

#[derive(Deserialize, Debug)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum QueryKind {
    Exact {
        value: Value,
    },
    Range {
        #[serde(flatten)]
        value: RangeValue,
    },
    Match {
        value: String,
    },
    DynamicMatch {
        value: String,
    },
    #[serde(rename_all = "camelCase")]
    FieldDynamicMatch {
        field_name: String,
        value: String,
    },
}

impl QueryKind {
    /// Check whether the current [`QueryKind`] can be used against a certain [`Mapping`]
    pub fn is_for_mapping(&self, mapping: &Mapping) -> bool {
        match (self, mapping) {
            (QueryKind::Exact { .. }, Mapping::Exact { .. }) => true,
            (QueryKind::Range { .. }, Mapping::Range { .. }) => true,
            (QueryKind::Match { .. }, Mapping::Match { .. }) => true,
            (QueryKind::DynamicMatch { .. }, Mapping::DynamicMatch { .. }) => true,
            (QueryKind::FieldDynamicMatch { .. }, Mapping::FieldDynamicMatch { .. }) => true,
            _ => false,
        }
    }

    /// 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",
        }
    }
}

#[derive(Deserialize, Debug)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ConjunctiveCondition {
    All { conditions: Vec<Query> },
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum Query {
    #[serde(rename_all = "camelCase")]
    Basic {
        index_name: String,
        #[serde(flatten)]
        kind: QueryKind,
    },
    Conjunctive(ConjunctiveCondition),
}