From 7b733dd34f75408c19fab4b2476cfe8e669331ec Mon Sep 17 00:00:00 2001 From: Laurentiu Nicola Date: Mon, 8 May 2017 07:09:54 +0300 Subject: [PATCH] Fix i64 overflow check and merge NotJSON with NotJSONObject --- src/schema/field_type.rs | 13 ++++--------- src/schema/schema.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/schema/field_type.rs b/src/schema/field_type.rs index 638cb7267..0f5cd6c5b 100644 --- a/src/schema/field_type.rs +++ b/src/schema/field_type.rs @@ -65,16 +65,11 @@ impl FieldType { JsonValue::Number(ref field_val_num) => { match *self { FieldType::I64(_) => { - if let Some(field_val_u64) = field_val_num.as_u64() { - if field_val_u64 > (i64::max_value() as u64) { - Err(ValueParsingError::OverflowError(format!("Expected i64, but value {:?} overflows.", field_val_u64))) - } - else { - Ok(Value::I64(field_val_u64 as i64)) - } + if let Some(field_val_i64) = field_val_num.as_i64() { + Ok(Value::I64(field_val_i64)) } else { - Err(ValueParsingError::TypeError(format!("Expected a u32 int, got {:?}", json))) + Err(ValueParsingError::OverflowError(format!("Expected an i64 int, got {:?}", json))) } } FieldType::U64(_) => { @@ -82,7 +77,7 @@ impl FieldType { Ok(Value::U64(field_val_u64)) } else { - Err(ValueParsingError::TypeError(format!("Expected a u64 int, got {:?}", json))) + Err(ValueParsingError::TypeError(format!("Expected an u64 int, got {:?}", json))) } } FieldType::Str(_) => { diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 7fb77bd69..3f61de38b 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -226,7 +226,7 @@ impl Schema { else { format!("{:?}...", &doc_json[0..20]) }; - DocParsingError::NotJSONObject(doc_json_sample) + DocParsingError::NotJSON(doc_json_sample) })?; let mut doc = Document::default(); @@ -335,22 +335,13 @@ impl From for Schema { #[derive(Debug)] pub enum DocParsingError { /// The payload given is not valid JSON. - NotJSON(serde_json::Error), - /// The payload given is not a JSON Object (`{...}`). - NotJSONObject(String), + NotJSON(String), /// One of the value node could not be parsed. ValueError(String, ValueParsingError), /// The json-document contains a field that is not declared in the schema. NoSuchFieldInSchema(String), } -impl From for DocParsingError { - fn from(err: serde_json::Error) -> DocParsingError { - DocParsingError::NotJSON(err) - } -} - - #[cfg(test)] mod tests { @@ -358,7 +349,7 @@ mod tests { use schema::*; use serde_json; use schema::field_type::ValueParsingError; - use schema::schema::DocParsingError::{NotJSON, NotJSONObject}; + use schema::schema::DocParsingError::NotJSON; #[test] pub fn test_schema_serialization() { @@ -527,7 +518,7 @@ mod tests { }"#); match json_err { Err(DocParsingError::ValueError(_, ValueParsingError::OverflowError(_))) => { - assert!(false); + panic!("expected 9223372036854775808 to fit into u64, but it didn't"); } _ => { assert!(true); @@ -539,14 +530,29 @@ mod tests { "title": "my title", "author": "fulmicoton", "count": 50, - "popularity": 9223372036854775808, + "popularity": 9223372036854775808 }"#); match json_err { - Err(NotJSON(_)) | Err(NotJSONObject(_)) => { + Err(DocParsingError::ValueError(_, ValueParsingError::OverflowError(_))) => { assert!(true); }, _ => { - panic!("expected overflow but didn't"); + panic!("expected 9223372036854775808 to overflow i64, but it didn't"); + } + } + } + { + let json_err = schema.parse_document(r#"{ + "title": "my title", + "author": "fulmicoton", + "count": 50, + }"#); + match json_err { + Err(NotJSON(_)) => { + assert!(true); + }, + _ => { + panic!("expected invalid JSON to fail parsing, but it didn't"); } } }