os_query_builder_rs/term/
range.rs

1use serde::{
2    Serialize,
3    Serializer,
4    ser::SerializeMap
5};
6use crate::misc::relation::Relation;
7use crate::term::term_type::TermType;
8
9#[derive(Debug, Default, Clone)]
10pub struct Range {
11    field: Option<String>,
12    value: RangeValue
13}
14
15#[derive(Debug, Default, Clone, Serialize)]
16struct RangeValue {
17    #[serde(skip_serializing_if = "Option::is_none")]
18    gt: Option<TermType>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    gte: Option<TermType>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    lte: Option<TermType>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    lt: Option<TermType>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    format: Option<String>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    relation: Option<Relation>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    boost: Option<f64>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    time_zone: Option<String>
33}
34
35
36impl Serialize for Range {
37    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38        where
39            S: Serializer,
40    {
41        let mut state = serializer.serialize_map(Some(1))?;
42        state.serialize_entry(&self.field.as_deref().unwrap_or_default(), &self.value)?;
43        state.end()
44    }
45}
46
47impl Range {
48    pub fn new() -> Self {
49        Self::default()
50    }
51
52    pub fn field<T: Into<String> + Serialize>(self, field: T) -> Self {
53        Self {
54            field: Some(field.into()),
55            ..self
56        }
57    }
58
59    pub fn gt<T: Into<TermType> + Serialize>(self, gt: T) -> Self {
60        Self {
61            value: RangeValue {
62                gt: Some(gt.into()),
63                ..self.value
64            },
65            ..self
66        }
67    }
68    pub fn gte<T: Into<TermType> + Serialize>(self, gte: T) -> Self {
69        Self {
70            value: RangeValue {
71                gte: Some(gte.into()),
72                ..self.value
73            },
74            ..self
75        }
76    }
77
78    pub fn lt<T: Into<TermType> + Serialize>(self, lt: T) -> Self {
79        Self {
80            value: RangeValue {
81                lt: Some(lt.into()),
82                ..self.value
83            },
84            ..self
85        }
86    }
87    pub fn lte<T: Into<TermType> + Serialize>(self, lte: T) -> Self {
88        Self {
89            value: RangeValue {
90                lte: Some(lte.into()),
91                ..self.value
92            },
93            ..self
94        }
95    }
96
97    pub fn time_zone<T: Into<String> + Serialize>(self, time_zone: T) -> Self {
98        Self {
99            value: RangeValue {
100                time_zone: Some(time_zone.into()),
101                ..self.value
102            },
103            ..self
104        }
105    }
106
107    pub fn format<T: Into<String> + Serialize>(self, format: T) -> Self {
108        Self {
109            value: RangeValue {
110              format: Some(format.into()),
111                ..self.value
112            },
113            ..self
114        }
115    }
116
117    pub fn boost<T: Into<f64> + Serialize>(self, boost: T) -> Self {
118        Self {
119            value: RangeValue {
120                boost: Some(boost.into()),
121                ..self.value
122            },
123            ..self
124        }
125    }
126
127    pub fn relation<T: Into<Relation> + Serialize>(self, relation: T) -> Self {
128        Self {
129            value: RangeValue {
130                relation: Some(relation.into()),
131                ..self.value
132            },
133            ..self
134        }
135    }
136}