From e1cb5e299d7934258109c99db10085a4845415ae Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Tue, 20 Dec 2016 16:51:21 +0100 Subject: [PATCH] NOBUG split field_type into 2 --- src/lib.rs | 1 + src/schema/field.rs | 2 +- src/schema/field_entry.rs | 67 +------------------------------------ src/schema/field_type.rs | 69 +++++++++++++++++++++++++++++++++++++++ src/schema/mod.rs | 5 +-- src/schema/schema.rs | 4 +-- 6 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 src/schema/field_type.rs diff --git a/src/lib.rs b/src/lib.rs index e64a5189d..9ea438950 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,7 @@ extern crate itertools; extern crate chan; extern crate crossbeam; + #[cfg(feature="simdcompression")] extern crate libc; diff --git a/src/schema/field.rs b/src/schema/field.rs index d3af27441..792b5b658 100644 --- a/src/schema/field.rs +++ b/src/schema/field.rs @@ -10,7 +10,7 @@ use common::BinarySerializable; /// /// Because the field id is a `u8`, tantivy can only have at most `255` fields. /// Value 255 is reserved. -#[derive(Copy,Clone,Debug,PartialEq,PartialOrd,Eq,Ord,Hash, RustcEncodable, RustcDecodable)] +#[derive(Copy, Clone, Debug, PartialEq,PartialOrd,Eq,Ord,Hash, RustcEncodable, RustcDecodable)] pub struct Field(pub u8); impl BinarySerializable for Field { diff --git a/src/schema/field_entry.rs b/src/schema/field_entry.rs index 142a3d5e9..30c741330 100644 --- a/src/schema/field_entry.rs +++ b/src/schema/field_entry.rs @@ -6,60 +6,7 @@ use rustc_serialize::Decoder; use rustc_serialize::Encodable; use rustc_serialize::Encoder; use rustc_serialize::json::Json; -use schema::Value; - - -/// A `FieldType` describes the type (text, u32) of a field as well as -/// how it should be handled by tantivy. -#[derive(Clone, Debug, RustcDecodable, RustcEncodable)] -pub enum FieldType { - /// String field type configuration - Str(TextOptions), - /// U32 field type configuration - U32(U32Options), -} - -impl FieldType { - - /// 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: &Json) -> Result { - match *json { - Json::String(ref field_text) => { - match *self { - FieldType::Str(_) => { - Ok(Value::Str(field_text.clone())) - } - FieldType::U32(_) => { - Err(ValueParsingError::TypeError(format!("Expected a u32 int, got {:?}", json))) - } - } - } - Json::U64(ref field_val_u64) => { - match *self { - FieldType::U32(_) => { - if *field_val_u64 > (u32::max_value() as u64) { - Err(ValueParsingError::OverflowError(format!("Expected u32, but value {:?} overflows.", field_val_u64))) - } - else { - Ok(Value::U32(*field_val_u64 as u32)) - } - } - _ => { - Err(ValueParsingError::TypeError(format!("Expected a string, got {:?}", json))) - } - } - }, - _ => { - Err(ValueParsingError::TypeError(format!("Expected a string or a u32, got {:?}", json))) - } - } - } -} - +use schema::FieldType; /// A `FieldEntry` represents a field and its configuration. /// `Schema` are a collection of `FieldEntry` @@ -74,18 +21,6 @@ pub struct FieldEntry { field_type: FieldType, } - -/// Possible error that may occur while parsing a field value -/// At this point the JSON is known to be valid. -#[derive(Debug)] -pub enum ValueParsingError { - /// Encounterred 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 u32 type) - /// Tantivy will try to autocast values. - TypeError(String), -} - impl FieldEntry { /// Creates a new u32 field entry in the schema, given diff --git a/src/schema/field_type.rs b/src/schema/field_type.rs new file mode 100644 index 000000000..5debe63b0 --- /dev/null +++ b/src/schema/field_type.rs @@ -0,0 +1,69 @@ +use schema::TextOptions; +use schema::U32Options; + +use rustc_serialize::json::Json; +use schema::Value; + + +/// Possible error that may occur while parsing a field value +/// At this point the JSON is known to be valid. +#[derive(Debug)] +pub enum ValueParsingError { + /// Encounterred 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 u32 type) + /// Tantivy will try to autocast values. + TypeError(String), +} + + +/// A `FieldType` describes the type (text, u32) of a field as well as +/// how it should be handled by tantivy. +#[derive(Clone, Debug, RustcDecodable, RustcEncodable)] +pub enum FieldType { + /// String field type configuration + Str(TextOptions), + /// U32 field type configuration + U32(U32Options), +} + +impl FieldType { + + /// 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: &Json) -> Result { + match *json { + Json::String(ref field_text) => { + match *self { + FieldType::Str(_) => { + Ok(Value::Str(field_text.clone())) + } + FieldType::U32(_) => { + Err(ValueParsingError::TypeError(format!("Expected a u32 int, got {:?}", json))) + } + } + } + Json::U64(ref field_val_u64) => { + match *self { + FieldType::U32(_) => { + if *field_val_u64 > (u32::max_value() as u64) { + Err(ValueParsingError::OverflowError(format!("Expected u32, but value {:?} overflows.", field_val_u64))) + } + else { + Ok(Value::U32(*field_val_u64 as u32)) + } + } + _ => { + Err(ValueParsingError::TypeError(format!("Expected a string, got {:?}", json))) + } + } + }, + _ => { + Err(ValueParsingError::TypeError(format!("Expected a string or a u32, got {:?}", json))) + } + } + } +} \ No newline at end of file diff --git a/src/schema/mod.rs b/src/schema/mod.rs index da6863824..224d9f47d 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -99,6 +99,7 @@ mod schema; mod term; mod document; +mod field_type; mod field_entry; mod field_value; @@ -118,15 +119,15 @@ pub use self::document::Document; pub use self::field::Field; pub use self::term::Term; +pub use self::field_type::FieldType; pub use self::field_entry::FieldEntry; pub use self::field_value::FieldValue; -pub use self::field_entry::FieldType; pub use self::text_options::TextOptions; +pub use self::text_options::TextIndexingOptions; pub use self::text_options::TEXT; pub use self::text_options::STRING; pub use self::text_options::STORED; -pub use self::text_options::TextIndexingOptions; pub use self::u32_options::U32Options; pub use self::u32_options::FAST; diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 8d6e3bedd..bdd699eea 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -7,7 +7,7 @@ use rustc_serialize::Encoder; use rustc_serialize::json; use rustc_serialize::json::Json; use std::collections::BTreeMap; -use schema::field_entry::ValueParsingError; +use schema::field_type::ValueParsingError; use std::sync::Arc; use super::*; use std::fmt; @@ -318,7 +318,7 @@ mod tests { use schema::*; use rustc_serialize::json; - use schema::field_entry::ValueParsingError; + use schema::field_type::ValueParsingError; #[test] pub fn test_schema_serialization() {