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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::schema::bytes_options::BytesOptions;
use crate::schema::facet_options::FacetOptions;
use crate::schema::Facet;
use crate::schema::IndexRecordOption;
use crate::schema::TextFieldIndexing;
use crate::schema::Value;
use crate::schema::{IntOptions, TextOptions};
use crate::tokenizer::PreTokenizedString;
use chrono::{FixedOffset, Utc};
use serde_json::Value as JsonValue;

/// Possible error that may occur while parsing a field value
/// At this point the JSON is known to be valid.
#[derive(Debug, PartialEq)]
pub enum ValueParsingError {
    /// Encountered a numerical value that overflows or underflow its integer type.
    OverflowError(String),
    /// The json node is not of the correct type.
    /// (e.g. 3 for a `Str` type or `"abc"` for a u64 type)
    /// Tantivy will try to autocast values.
    TypeError(String),
    /// The json node is a string but contains json that is
    /// not valid base64.
    InvalidBase64(String),
}

/// Type of the value that a field can take.
///
/// Contrary to FieldType, this does
/// not include the way the field must be indexed.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Type {
    /// `&str`
    Str,
    /// `u64`
    U64,
    /// `i64`
    I64,
    /// `f64`
    F64,
    /// `date(i64) timestamp`
    Date,
    /// `tantivy::schema::Facet`. Passed as a string in JSON.
    HierarchicalFacet,
    /// `Vec<u8>`
    Bytes,
}

/// A `FieldType` describes the type (text, u64) of a field as well as
/// how it should be handled by tantivy.
#[derive(Clone, Debug, PartialEq)]
pub enum FieldType {
    /// String field type configuration
    Str(TextOptions),
    /// Unsigned 64-bits integers field type configuration
    U64(IntOptions),
    /// Signed 64-bits integers 64 field type configuration
    I64(IntOptions),
    /// 64-bits float 64 field type configuration
    F64(IntOptions),
    /// Signed 64-bits Date 64 field type configuration,
    Date(IntOptions),
    /// Hierachical Facet
    HierarchicalFacet(FacetOptions),
    /// Bytes (one per document)
    Bytes(BytesOptions),
}

impl FieldType {
    /// Returns the value type associated for this field.
    pub fn value_type(&self) -> Type {
        match *self {
            FieldType::Str(_) => Type::Str,
            FieldType::U64(_) => Type::U64,
            FieldType::I64(_) => Type::I64,
            FieldType::F64(_) => Type::F64,
            FieldType::Date(_) => Type::Date,
            FieldType::HierarchicalFacet(_) => Type::HierarchicalFacet,
            FieldType::Bytes(_) => Type::Bytes,
        }
    }

    /// returns true iff the field is indexed.
    pub fn is_indexed(&self) -> bool {
        match *self {
            FieldType::Str(ref text_options) => text_options.get_indexing_options().is_some(),
            FieldType::U64(ref int_options)
            | FieldType::I64(ref int_options)
            | FieldType::F64(ref int_options) => int_options.is_indexed(),
            FieldType::Date(ref date_options) => date_options.is_indexed(),
            FieldType::HierarchicalFacet(ref facet_options) => facet_options.is_indexed(),
            FieldType::Bytes(ref bytes_options) => bytes_options.is_indexed(),
        }
    }

    /// Given a field configuration, return the maximal possible
    /// `IndexRecordOption` available.
    ///
    /// If the field is not indexed, then returns `None`.
    pub fn get_index_record_option(&self) -> Option<IndexRecordOption> {
        match *self {
            FieldType::Str(ref text_options) => text_options
                .get_indexing_options()
                .map(TextFieldIndexing::index_option),
            FieldType::U64(ref int_options)
            | FieldType::I64(ref int_options)
            | FieldType::F64(ref int_options)
            | FieldType::Date(ref int_options) => {
                if int_options.is_indexed() {
                    Some(IndexRecordOption::Basic)
                } else {
                    None
                }
            }
            FieldType::HierarchicalFacet(ref facet_options) => {
                if facet_options.is_indexed() {
                    Some(IndexRecordOption::Basic)
                } else {
                    None
                }
            }
            FieldType::Bytes(ref bytes_options) => {
                if bytes_options.is_indexed() {
                    Some(IndexRecordOption::Basic)
                } else {
                    None
                }
            }
        }
    }

    /// Parses a field value from json, given the target FieldType.
    ///
    /// Tantivy will not try to cast values.
    /// For instance, If the json value is the integer `3` and the
    /// target field is a `Str`, this method will return an Error.
    pub fn value_from_json(&self, json: &JsonValue) -> Result<Value, ValueParsingError> {
        match *json {
            JsonValue::String(ref field_text) => match *self {
                FieldType::Date(_) => {
                    let dt_with_fixed_tz: chrono::DateTime<FixedOffset> =
                        chrono::DateTime::parse_from_rfc3339(field_text).map_err(|err|
                            ValueParsingError::TypeError(format!(
                                "Failed to parse date from JSON. Expected rfc3339 format, got {}. {:?}",
                                field_text, err
                            ))
                        )?;
                    Ok(Value::Date(dt_with_fixed_tz.with_timezone(&Utc)))
                }
                FieldType::Str(_) => Ok(Value::Str(field_text.clone())),
                FieldType::U64(_) | FieldType::I64(_) | FieldType::F64(_) => Err(
                    ValueParsingError::TypeError(format!("Expected an integer, got {:?}", json)),
                ),
                FieldType::HierarchicalFacet(_) => Ok(Value::Facet(Facet::from(field_text))),
                FieldType::Bytes(_) => base64::decode(field_text).map(Value::Bytes).map_err(|_| {
                    ValueParsingError::InvalidBase64(format!(
                        "Expected base64 string, got {:?}",
                        field_text
                    ))
                }),
            },
            JsonValue::Number(ref field_val_num) => match *self {
                FieldType::I64(_) | FieldType::Date(_) => {
                    if let Some(field_val_i64) = field_val_num.as_i64() {
                        Ok(Value::I64(field_val_i64))
                    } else {
                        let msg = format!("Expected an i64 int, got {:?}", json);
                        Err(ValueParsingError::OverflowError(msg))
                    }
                }
                FieldType::U64(_) => {
                    if let Some(field_val_u64) = field_val_num.as_u64() {
                        Ok(Value::U64(field_val_u64))
                    } else {
                        let msg = format!("Expected a u64 int, got {:?}", json);
                        Err(ValueParsingError::OverflowError(msg))
                    }
                }
                FieldType::F64(_) => {
                    if let Some(field_val_f64) = field_val_num.as_f64() {
                        Ok(Value::F64(field_val_f64))
                    } else {
                        let msg = format!("Expected a f64 int, got {:?}", json);
                        Err(ValueParsingError::OverflowError(msg))
                    }
                }
                FieldType::Str(_) | FieldType::HierarchicalFacet(_) | FieldType::Bytes(_) => {
                    let msg = format!("Expected a string, got {:?}", json);
                    Err(ValueParsingError::TypeError(msg))
                }
            },
            JsonValue::Object(_) => match *self {
                FieldType::Str(_) => {
                    if let Ok(tok_str_val) =
                        serde_json::from_value::<PreTokenizedString>(json.clone())
                    {
                        Ok(Value::PreTokStr(tok_str_val))
                    } else {
                        let msg = format!(
                            "Json value {:?} cannot be translated to PreTokenizedString.",
                            json
                        );
                        Err(ValueParsingError::TypeError(msg))
                    }
                }
                _ => {
                    let msg = format!(
                        "Json value not supported error {:?}. Expected {:?}",
                        json, self
                    );
                    Err(ValueParsingError::TypeError(msg))
                }
            },
            _ => {
                let msg = format!(
                    "Json value not supported error {:?}. Expected {:?}",
                    json, self
                );
                Err(ValueParsingError::TypeError(msg))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::FieldType;
    use crate::schema::field_type::ValueParsingError;
    use crate::schema::TextOptions;
    use crate::schema::Value;
    use crate::schema::{Schema, INDEXED};
    use crate::tokenizer::{PreTokenizedString, Token};
    use crate::{DateTime, Document};
    use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc};

    #[test]
    fn test_deserialize_json_date() {
        let mut schema_builder = Schema::builder();
        let date_field = schema_builder.add_date_field("date", INDEXED);
        let schema = schema_builder.build();
        let doc_json = r#"{"date": "2019-10-12T07:20:50.52+02:00"}"#;
        let doc = schema.parse_document(doc_json).unwrap();
        let date = doc.get_first(date_field).unwrap();
        assert_eq!(format!("{:?}", date), "Date(2019-10-12T05:20:50.520Z)");
    }

    #[test]
    fn test_serialize_json_date() {
        let mut doc = Document::new();
        let mut schema_builder = Schema::builder();
        let date_field = schema_builder.add_date_field("date", INDEXED);
        let schema = schema_builder.build();
        let naive_date = NaiveDate::from_ymd(1982, 9, 17);
        let naive_time = NaiveTime::from_hms(13, 20, 00);
        let date_time = DateTime::from_utc(NaiveDateTime::new(naive_date, naive_time), Utc);
        doc.add_date(date_field, &date_time);
        let doc_json = schema.to_json(&doc);
        assert_eq!(doc_json, r#"{"date":["1982-09-17T13:20:00+00:00"]}"#);
    }

    #[test]
    fn test_bytes_value_from_json() {
        let result = FieldType::Bytes(Default::default())
            .value_from_json(&json!("dGhpcyBpcyBhIHRlc3Q="))
            .unwrap();
        assert_eq!(result, Value::Bytes("this is a test".as_bytes().to_vec()));

        let result = FieldType::Bytes(Default::default()).value_from_json(&json!(521));
        match result {
            Err(ValueParsingError::TypeError(_)) => {}
            _ => panic!("Expected parse failure for wrong type"),
        }

        let result = FieldType::Bytes(Default::default()).value_from_json(&json!("-"));
        match result {
            Err(ValueParsingError::InvalidBase64(_)) => {}
            _ => panic!("Expected parse failure for invalid base64"),
        }
    }

    #[test]
    fn test_pre_tok_str_value_from_json() {
        let pre_tokenized_string_json = r#"{
  "text": "The Old Man",
  "tokens": [
    {
      "offset_from": 0,
      "offset_to": 3,
      "position": 0,
      "text": "The",
      "position_length": 1
    },
    {
      "offset_from": 4,
      "offset_to": 7,
      "position": 1,
      "text": "Old",
      "position_length": 1
    },
    {
      "offset_from": 8,
      "offset_to": 11,
      "position": 2,
      "text": "Man",
      "position_length": 1
    }
  ]
}"#;

        let expected_value = Value::PreTokStr(PreTokenizedString {
            text: String::from("The Old Man"),
            tokens: vec![
                Token {
                    offset_from: 0,
                    offset_to: 3,
                    position: 0,
                    text: String::from("The"),
                    position_length: 1,
                },
                Token {
                    offset_from: 4,
                    offset_to: 7,
                    position: 1,
                    text: String::from("Old"),
                    position_length: 1,
                },
                Token {
                    offset_from: 8,
                    offset_to: 11,
                    position: 2,
                    text: String::from("Man"),
                    position_length: 1,
                },
            ],
        });

        let deserialized_value = FieldType::Str(TextOptions::default())
            .value_from_json(&serde_json::from_str(pre_tokenized_string_json).unwrap())
            .unwrap();

        assert_eq!(deserialized_value, expected_value);

        let serialized_value_json = serde_json::to_string_pretty(&expected_value).unwrap();

        assert_eq!(serialized_value_json, pre_tokenized_string_json);
    }
}