Minor code cleanup/refactoring. No functional changes.

This commit is contained in:
John Gallagher 2016-05-15 22:51:04 -05:00
parent 5038e2a705
commit b87d4b44a6

View File

@ -22,23 +22,18 @@ impl ToSql for Value {
/// Deserialize text/blob to JSON `Value`. /// Deserialize text/blob to JSON `Value`.
impl FromSql for Value { impl FromSql for Value {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Value> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Value> {
match sqlite3_column_type(stmt, col) { let value_result = match sqlite3_column_type(stmt, col) {
ffi::SQLITE_TEXT => { ffi::SQLITE_TEXT => {
let s = try!(String::column_result(stmt, col)); let s = try!(String::column_result(stmt, col));
match serde_json::from_str(&s) { serde_json::from_str(&s)
Ok(v) => Ok(v),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
}
} }
ffi::SQLITE_BLOB => { ffi::SQLITE_BLOB => {
let blob: Vec<u8> = try!(FromSql::column_result(stmt, col)); let blob = try!(Vec::<u8>::column_result(stmt, col));
match serde_json::from_slice(&blob[..]) { serde_json::from_slice(&blob)
Ok(v) => Ok(v),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
}
} }
_ => 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() { fn test_json_value() {
let db = checked_memory_handle(); 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(); let data: serde_json::Value = serde_json::from_str(json).unwrap();
db.execute("INSERT INTO foo (t, b) VALUES (?, ?)", db.execute("INSERT INTO foo (t, b) VALUES (?, ?)",
&[&data, &json.as_bytes()]) &[&data, &json.as_bytes()])