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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::ops::BitOr;

pub use common::DateTimePrecision;
use serde::{Deserialize, Serialize};

use crate::schema::flags::{FastFlag, IndexedFlag, SchemaFlagList, StoredFlag};

/// The precision of the indexed date/time values in the inverted index.
pub const DATE_TIME_PRECISION_INDEXED: DateTimePrecision = DateTimePrecision::Seconds;

/// Defines how DateTime field should be handled by tantivy.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct DateOptions {
    indexed: bool,
    // This boolean has no effect if the field is not marked as indexed true.
    fieldnorms: bool,
    #[serde(default)]
    fast: bool,
    stored: bool,
    // Internal storage precision, used to optimize storage
    // compression on fast fields.
    #[serde(default)]
    precision: DateTimePrecision,
}

impl DateOptions {
    /// Returns true iff the value is stored.
    #[inline]
    pub fn is_stored(&self) -> bool {
        self.stored
    }

    /// Returns true iff the value is indexed and therefore searchable.
    #[inline]
    pub fn is_indexed(&self) -> bool {
        self.indexed
    }

    /// Returns true iff the field has fieldnorm.
    #[inline]
    pub fn fieldnorms(&self) -> bool {
        self.fieldnorms && self.indexed
    }

    /// Returns true iff the value is a fast field.
    #[inline]
    pub fn is_fast(&self) -> bool {
        self.fast
    }

    /// Set the field as stored.
    ///
    /// Only the fields that are set as *stored* are
    /// persisted into the Tantivy's store.
    #[must_use]
    pub fn set_stored(mut self) -> DateOptions {
        self.stored = true;
        self
    }

    /// Set the field as indexed.
    ///
    /// Setting an integer as indexed will generate
    /// a posting list for each value taken by the integer.
    ///
    /// This is required for the field to be searchable.
    #[must_use]
    pub fn set_indexed(mut self) -> DateOptions {
        self.indexed = true;
        self
    }

    /// Set the field with fieldnorm.
    ///
    /// Setting an integer as fieldnorm will generate
    /// the fieldnorm data for it.
    #[must_use]
    pub fn set_fieldnorm(mut self) -> DateOptions {
        self.fieldnorms = true;
        self
    }

    /// Set the field as a fast field.
    ///
    /// Fast fields are designed for random access.
    #[must_use]
    pub fn set_fast(mut self) -> DateOptions {
        self.fast = true;
        self
    }

    /// Sets the precision for this DateTime field on the fast field.
    /// Indexed precision is always [`DATE_TIME_PRECISION_INDEXED`].
    ///
    /// Internal storage precision, used to optimize storage
    /// compression on fast fields.
    pub fn set_precision(mut self, precision: DateTimePrecision) -> DateOptions {
        self.precision = precision;
        self
    }

    /// Returns the storage precision for this DateTime field.
    ///
    /// Internal storage precision, used to optimize storage
    /// compression on fast fields.
    pub fn get_precision(&self) -> DateTimePrecision {
        self.precision
    }
}

impl From<()> for DateOptions {
    fn from(_: ()) -> DateOptions {
        DateOptions::default()
    }
}

impl From<FastFlag> for DateOptions {
    fn from(_: FastFlag) -> Self {
        DateOptions {
            fast: true,
            ..Default::default()
        }
    }
}

impl From<StoredFlag> for DateOptions {
    fn from(_: StoredFlag) -> Self {
        DateOptions {
            stored: true,
            ..Default::default()
        }
    }
}

impl From<IndexedFlag> for DateOptions {
    fn from(_: IndexedFlag) -> Self {
        DateOptions {
            indexed: true,
            fieldnorms: true,
            ..Default::default()
        }
    }
}

impl<T: Into<DateOptions>> BitOr<T> for DateOptions {
    type Output = DateOptions;

    fn bitor(self, other: T) -> DateOptions {
        let other = other.into();
        DateOptions {
            indexed: self.indexed | other.indexed,
            fieldnorms: self.fieldnorms | other.fieldnorms,
            stored: self.stored | other.stored,
            fast: self.fast | other.fast,
            precision: self.precision,
        }
    }
}

impl<Head, Tail> From<SchemaFlagList<Head, Tail>> for DateOptions
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)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_date_options_consistent_with_default() {
        let date_time_options: DateOptions = serde_json::from_str(
            r#"{
            "indexed": false,
            "fieldnorms": false,
            "stored": false
        }"#,
        )
        .unwrap();
        assert_eq!(date_time_options, DateOptions::default());
    }

    #[test]
    fn test_serialize_date_option() {
        let date_options = serde_json::from_str::<DateOptions>(
            r#"
            {
                "indexed": true,
                "fieldnorms": false,
                "stored": false,
                "precision": "milliseconds"
            }"#,
        )
        .unwrap();

        let date_options_json = serde_json::to_value(date_options).unwrap();
        assert_eq!(
            date_options_json,
            serde_json::json!({
                "precision": "milliseconds",
                "indexed": true,
                "fast": false,
                "fieldnorms": false,
                "stored": false
            })
        );
    }

    #[test]
    fn test_deserialize_date_options_with_wrong_options() {
        assert!(serde_json::from_str::<DateOptions>(
            r#"{
            "indexed": true,
            "fieldnorms": false,
            "stored": "wrong_value"
        }"#
        )
        .unwrap_err()
        .to_string()
        .contains("expected a boolean"));

        assert!(serde_json::from_str::<DateOptions>(
            r#"{
            "indexed": true,
            "fieldnorms": false,
            "stored": false,
            "precision": "hours"
        }"#
        )
        .unwrap_err()
        .to_string()
        .contains("unknown variant `hours`"));
    }
}