NOBUG split field_type into 2

This commit is contained in:
Paul Masurel
2016-12-20 16:51:21 +01:00
parent d3d34be167
commit e1cb5e299d
6 changed files with 77 additions and 71 deletions
+1
View File
@@ -45,6 +45,7 @@ extern crate itertools;
extern crate chan;
extern crate crossbeam;
#[cfg(feature="simdcompression")]
extern crate libc;
+1 -1
View File
@@ -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 {
+1 -66
View File
@@ -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<Value, ValueParsingError> {
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
+69
View File
@@ -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<Value, ValueParsingError> {
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)))
}
}
}
}
+3 -2
View File
@@ -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;
+2 -2
View File
@@ -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() {