forked from quickwit-oss/tantivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_object_options.rs
123 lines (107 loc) · 3.37 KB
/
json_object_options.rs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::ops::BitOr;
use serde::{Deserialize, Serialize};
use crate::schema::flags::{SchemaFlagList, StoredFlag};
use crate::schema::{TextFieldIndexing, TextOptions};
/// The `JsonObjectOptions` make it possible to
/// configure how a json object field should be indexed and stored.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonObjectOptions {
stored: bool,
// If set to some, int, date, f64 and text will be indexed.
// Text will use the TextFieldIndexing setting for indexing.
indexing: Option<TextFieldIndexing>,
}
impl JsonObjectOptions {
/// Returns `true` if the json object should be stored.
pub fn is_stored(&self) -> bool {
self.stored
}
/// Returns `true` iff the json object should be indexed.
pub fn is_indexed(&self) -> bool {
self.indexing.is_some()
}
/// Returns the text indexing options.
///
/// If set to `Some` then both int and str values will be indexed.
/// The inner `TextFieldIndexing` will however, only apply to the str values
/// in the json object.
pub fn get_text_indexing_options(&self) -> Option<&TextFieldIndexing> {
self.indexing.as_ref()
}
/// Sets the field as stored
#[must_use]
pub fn set_stored(mut self) -> Self {
self.stored = true;
self
}
/// Sets the field as indexed, with the specific indexing options.
#[must_use]
pub fn set_indexing_options(mut self, indexing: TextFieldIndexing) -> Self {
self.indexing = Some(indexing);
self
}
}
impl From<StoredFlag> for JsonObjectOptions {
fn from(_stored_flag: StoredFlag) -> Self {
JsonObjectOptions {
stored: true,
indexing: None,
}
}
}
impl From<()> for JsonObjectOptions {
fn from(_: ()) -> Self {
Self::default()
}
}
impl<T: Into<JsonObjectOptions>> BitOr<T> for JsonObjectOptions {
type Output = JsonObjectOptions;
fn bitor(self, other: T) -> Self {
let other = other.into();
JsonObjectOptions {
indexing: self.indexing.or(other.indexing),
stored: self.stored | other.stored,
}
}
}
impl<Head, Tail> From<SchemaFlagList<Head, Tail>> for JsonObjectOptions
where
Head: Clone,
Tail: Clone,
Self: BitOr<Output = Self> + From<Head> + From<Tail>,
{
fn from(head_tail: SchemaFlagList<Head, Tail>) -> Self {
Self::from(head_tail.head) | Self::from(head_tail.tail)
}
}
impl From<TextOptions> for JsonObjectOptions {
fn from(text_options: TextOptions) -> Self {
JsonObjectOptions {
stored: text_options.is_stored(),
indexing: text_options.get_indexing_options().cloned(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::{STORED, TEXT};
#[test]
fn test_json_options() {
{
let json_options: JsonObjectOptions = (STORED | TEXT).into();
assert!(json_options.is_stored());
assert!(json_options.is_indexed());
}
{
let json_options: JsonObjectOptions = TEXT.into();
assert!(!json_options.is_stored());
assert!(json_options.is_indexed());
}
{
let json_options: JsonObjectOptions = STORED.into();
assert!(json_options.is_stored());
assert!(!json_options.is_indexed());
}
}
}