From b87d4b44a633beb0c46bc12ab154f60f51e3c127 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 22:51:04 -0500 Subject: [PATCH] Minor code cleanup/refactoring. No functional changes. --- src/types/serde_json.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index 22eebb3..3c2089a 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -22,23 +22,18 @@ impl ToSql for Value { /// Deserialize text/blob to JSON `Value`. impl FromSql for Value { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result { - match sqlite3_column_type(stmt, col) { + let value_result = match sqlite3_column_type(stmt, col) { ffi::SQLITE_TEXT => { let s = try!(String::column_result(stmt, col)); - match serde_json::from_str(&s) { - Ok(v) => Ok(v), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } + serde_json::from_str(&s) } ffi::SQLITE_BLOB => { - let blob: Vec = try!(FromSql::column_result(stmt, col)); - match serde_json::from_slice(&blob[..]) { - Ok(v) => Ok(v), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } + let blob = try!(Vec::::column_result(stmt, col)); + serde_json::from_slice(&blob) } - _ => Err(Error::InvalidColumnType), - } + _ => return Err(Error::InvalidColumnType) + }; + value_result.map_err(|err| { Error::FromSqlConversionFailure(Box::new(err)) }) } } @@ -57,7 +52,7 @@ mod test { fn test_json_value() { let db = checked_memory_handle(); - let json = "{\"foo\": 13, \"bar\": \"baz\"}"; + let json = r#"{"foo": 13, "bar": "baz"}"#; let data: serde_json::Value = serde_json::from_str(json).unwrap(); db.execute("INSERT INTO foo (t, b) VALUES (?, ?)", &[&data, &json.as_bytes()])