lb_tantivy/schema/document/
value.rs1use std::fmt::Debug;
2use std::net::Ipv6Addr;
3
4use common::DateTime;
5
6use crate::tokenizer::PreTokenizedString;
7
8pub trait Value<'a>: Send + Sync + Debug {
10 type ArrayIter: Iterator<Item = Self>;
13 type ObjectIter: Iterator<Item = (&'a str, Self)>;
16
17 fn as_value(&self) -> ReferenceValue<'a, Self>;
19
20 #[inline]
21 fn as_leaf(&self) -> Option<ReferenceValueLeaf<'a>> {
23 if let ReferenceValue::Leaf(val) = self.as_value() {
24 Some(val)
25 } else {
26 None
27 }
28 }
29
30 #[inline]
31 fn as_str(&self) -> Option<&'a str> {
33 self.as_leaf().and_then(|leaf| leaf.as_str())
34 }
35
36 #[inline]
37 fn as_u64(&self) -> Option<u64> {
39 self.as_leaf().and_then(|leaf| leaf.as_u64())
40 }
41
42 #[inline]
43 fn as_i64(&self) -> Option<i64> {
45 self.as_leaf().and_then(|leaf| leaf.as_i64())
46 }
47
48 #[inline]
49 fn as_f64(&self) -> Option<f64> {
51 self.as_leaf().and_then(|leaf| leaf.as_f64())
52 }
53
54 #[inline]
55 fn as_datetime(&self) -> Option<DateTime> {
57 self.as_leaf().and_then(|leaf| leaf.as_datetime())
58 }
59
60 #[inline]
61 fn as_ip_addr(&self) -> Option<Ipv6Addr> {
63 self.as_leaf().and_then(|leaf| leaf.as_ip_addr())
64 }
65
66 #[inline]
67 fn as_bool(&self) -> Option<bool> {
69 self.as_leaf().and_then(|leaf| leaf.as_bool())
70 }
71
72 #[inline]
73 fn as_pre_tokenized_text(&self) -> Option<Box<PreTokenizedString>> {
76 self.as_leaf()
77 .and_then(|leaf| leaf.into_pre_tokenized_text())
78 }
79
80 #[inline]
81 fn as_bytes(&self) -> Option<&'a [u8]> {
83 self.as_leaf().and_then(|leaf| leaf.as_bytes())
84 }
85
86 #[inline]
87 fn as_facet(&self) -> Option<&'a str> {
89 self.as_leaf().and_then(|leaf| leaf.as_facet())
90 }
91
92 #[inline]
93 fn as_array(&self) -> Option<Self::ArrayIter> {
95 if let ReferenceValue::Array(val) = self.as_value() {
96 Some(val)
97 } else {
98 None
99 }
100 }
101
102 #[inline]
103 fn as_object(&self) -> Option<Self::ObjectIter> {
105 if let ReferenceValue::Object(val) = self.as_value() {
106 Some(val)
107 } else {
108 None
109 }
110 }
111}
112
113#[derive(Clone, Debug, PartialEq)]
115pub enum ReferenceValueLeaf<'a> {
116 Null,
118 Str(&'a str),
120 U64(u64),
122 I64(i64),
124 F64(f64),
126 Date(DateTime),
128 Facet(&'a str),
131 Bytes(&'a [u8]),
133 IpAddr(Ipv6Addr),
135 Bool(bool),
137 PreTokStr(Box<PreTokenizedString>),
139}
140
141impl From<u64> for ReferenceValueLeaf<'_> {
142 #[inline]
143 fn from(value: u64) -> Self {
144 ReferenceValueLeaf::U64(value)
145 }
146}
147
148impl From<i64> for ReferenceValueLeaf<'_> {
149 #[inline]
150 fn from(value: i64) -> Self {
151 ReferenceValueLeaf::I64(value)
152 }
153}
154
155impl From<f64> for ReferenceValueLeaf<'_> {
156 #[inline]
157 fn from(value: f64) -> Self {
158 ReferenceValueLeaf::F64(value)
159 }
160}
161
162impl From<bool> for ReferenceValueLeaf<'_> {
163 #[inline]
164 fn from(value: bool) -> Self {
165 ReferenceValueLeaf::Bool(value)
166 }
167}
168
169impl<'a> From<&'a str> for ReferenceValueLeaf<'a> {
170 #[inline]
171 fn from(value: &'a str) -> Self {
172 ReferenceValueLeaf::Str(value)
173 }
174}
175
176impl<'a> From<&'a [u8]> for ReferenceValueLeaf<'a> {
177 #[inline]
178 fn from(value: &'a [u8]) -> Self {
179 ReferenceValueLeaf::Bytes(value)
180 }
181}
182
183impl From<DateTime> for ReferenceValueLeaf<'_> {
184 #[inline]
185 fn from(value: DateTime) -> Self {
186 ReferenceValueLeaf::Date(value)
187 }
188}
189
190impl From<Ipv6Addr> for ReferenceValueLeaf<'_> {
191 #[inline]
192 fn from(value: Ipv6Addr) -> Self {
193 ReferenceValueLeaf::IpAddr(value)
194 }
195}
196
197impl From<PreTokenizedString> for ReferenceValueLeaf<'_> {
198 #[inline]
199 fn from(val: PreTokenizedString) -> Self {
200 ReferenceValueLeaf::PreTokStr(Box::new(val))
201 }
202}
203
204impl<'a, T: Value<'a> + ?Sized> From<ReferenceValueLeaf<'a>> for ReferenceValue<'a, T> {
205 #[inline]
206 fn from(value: ReferenceValueLeaf<'a>) -> Self {
207 match value {
208 ReferenceValueLeaf::Null => ReferenceValue::Leaf(ReferenceValueLeaf::Null),
209 ReferenceValueLeaf::Str(val) => ReferenceValue::Leaf(ReferenceValueLeaf::Str(val)),
210 ReferenceValueLeaf::U64(val) => ReferenceValue::Leaf(ReferenceValueLeaf::U64(val)),
211 ReferenceValueLeaf::I64(val) => ReferenceValue::Leaf(ReferenceValueLeaf::I64(val)),
212 ReferenceValueLeaf::F64(val) => ReferenceValue::Leaf(ReferenceValueLeaf::F64(val)),
213 ReferenceValueLeaf::Date(val) => ReferenceValue::Leaf(ReferenceValueLeaf::Date(val)),
214 ReferenceValueLeaf::Facet(val) => ReferenceValue::Leaf(ReferenceValueLeaf::Facet(val)),
215 ReferenceValueLeaf::Bytes(val) => ReferenceValue::Leaf(ReferenceValueLeaf::Bytes(val)),
216 ReferenceValueLeaf::IpAddr(val) => {
217 ReferenceValue::Leaf(ReferenceValueLeaf::IpAddr(val))
218 }
219 ReferenceValueLeaf::Bool(val) => ReferenceValue::Leaf(ReferenceValueLeaf::Bool(val)),
220 ReferenceValueLeaf::PreTokStr(val) => {
221 ReferenceValue::Leaf(ReferenceValueLeaf::PreTokStr(val))
222 }
223 }
224 }
225}
226
227impl<'a> ReferenceValueLeaf<'a> {
228 #[inline]
229 pub fn is_null(&self) -> bool {
231 matches!(self, Self::Null)
232 }
233
234 #[inline]
235 pub fn as_str(&self) -> Option<&'a str> {
237 if let Self::Str(val) = self {
238 Some(val)
239 } else {
240 None
241 }
242 }
243
244 #[inline]
245 pub fn as_u64(&self) -> Option<u64> {
247 if let Self::U64(val) = self {
248 Some(*val)
249 } else {
250 None
251 }
252 }
253
254 #[inline]
255 pub fn as_i64(&self) -> Option<i64> {
257 if let Self::I64(val) = self {
258 Some(*val)
259 } else {
260 None
261 }
262 }
263
264 #[inline]
265 pub fn as_f64(&self) -> Option<f64> {
267 if let Self::F64(val) = self {
268 Some(*val)
269 } else {
270 None
271 }
272 }
273
274 #[inline]
275 pub fn as_datetime(&self) -> Option<DateTime> {
277 if let Self::Date(val) = self {
278 Some(*val)
279 } else {
280 None
281 }
282 }
283
284 #[inline]
285 pub fn as_ip_addr(&self) -> Option<Ipv6Addr> {
287 if let Self::IpAddr(val) = self {
288 Some(*val)
289 } else {
290 None
291 }
292 }
293
294 #[inline]
295 pub fn as_bool(&self) -> Option<bool> {
297 if let Self::Bool(val) = self {
298 Some(*val)
299 } else {
300 None
301 }
302 }
303
304 #[inline]
305 pub fn into_pre_tokenized_text(self) -> Option<Box<PreTokenizedString>> {
308 if let Self::PreTokStr(val) = self {
309 Some(val)
310 } else {
311 None
312 }
313 }
314
315 #[inline]
316 pub fn as_bytes(&self) -> Option<&'a [u8]> {
318 if let Self::Bytes(val) = self {
319 Some(val)
320 } else {
321 None
322 }
323 }
324
325 #[inline]
326 pub fn as_facet(&self) -> Option<&'a str> {
328 if let Self::Facet(val) = self {
329 Some(val)
330 } else {
331 None
332 }
333 }
334}
335
336#[derive(Clone, Debug, PartialEq)]
338pub enum ReferenceValue<'a, V>
339where V: Value<'a> + ?Sized
340{
341 Leaf(ReferenceValueLeaf<'a>),
343 Array(V::ArrayIter),
345 Object(V::ObjectIter),
347}
348
349impl<'a, V> ReferenceValue<'a, V>
350where V: Value<'a>
351{
352 #[inline]
353 pub fn is_null(&self) -> bool {
355 matches!(self, Self::Leaf(ReferenceValueLeaf::Null))
356 }
357
358 #[inline]
359 pub fn as_leaf(&self) -> Option<&ReferenceValueLeaf<'a>> {
361 if let Self::Leaf(val) = self {
362 Some(val)
363 } else {
364 None
365 }
366 }
367
368 #[inline]
369 pub fn into_leaf(self) -> Option<ReferenceValueLeaf<'a>> {
371 if let Self::Leaf(val) = self {
372 Some(val)
373 } else {
374 None
375 }
376 }
377
378 #[inline]
379 pub fn as_str(&self) -> Option<&'a str> {
381 self.as_leaf().and_then(|leaf| leaf.as_str())
382 }
383
384 #[inline]
385 pub fn as_u64(&self) -> Option<u64> {
387 self.as_leaf().and_then(|leaf| leaf.as_u64())
388 }
389
390 #[inline]
391 pub fn as_i64(&self) -> Option<i64> {
393 self.as_leaf().and_then(|leaf| leaf.as_i64())
394 }
395
396 #[inline]
397 pub fn as_f64(&self) -> Option<f64> {
399 self.as_leaf().and_then(|leaf| leaf.as_f64())
400 }
401
402 #[inline]
403 pub fn as_datetime(&self) -> Option<DateTime> {
405 self.as_leaf().and_then(|leaf| leaf.as_datetime())
406 }
407
408 #[inline]
409 pub fn as_ip_addr(&self) -> Option<Ipv6Addr> {
411 self.as_leaf().and_then(|leaf| leaf.as_ip_addr())
412 }
413
414 #[inline]
415 pub fn as_bool(&self) -> Option<bool> {
417 self.as_leaf().and_then(|leaf| leaf.as_bool())
418 }
419
420 #[inline]
421 pub fn into_pre_tokenized_text(self) -> Option<Box<PreTokenizedString>> {
424 self.into_leaf()
425 .and_then(|leaf| leaf.into_pre_tokenized_text())
426 }
427
428 #[inline]
429 pub fn as_bytes(&self) -> Option<&'a [u8]> {
431 self.as_leaf().and_then(|leaf| leaf.as_bytes())
432 }
433
434 #[inline]
435 pub fn as_facet(&self) -> Option<&'a str> {
437 self.as_leaf().and_then(|leaf| leaf.as_facet())
438 }
439
440 #[inline]
441 pub fn is_array(&self) -> bool {
443 matches!(self, Self::Array(_))
444 }
445
446 #[inline]
447 pub fn is_object(&self) -> bool {
449 matches!(self, Self::Object(_))
450 }
451}