From c90cd37c00e12e673441f6b0e1e9160b222a053f Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 23 May 2016 21:48:56 -0400 Subject: [PATCH] Add types::BorrowedValue. --- src/types/borrowed_value.rs | 51 +++++++++++++++++++++++++++++++++++++ src/types/mod.rs | 2 ++ 2 files changed, 53 insertions(+) create mode 100644 src/types/borrowed_value.rs diff --git a/src/types/borrowed_value.rs b/src/types/borrowed_value.rs new file mode 100644 index 0000000..8b97a33 --- /dev/null +++ b/src/types/borrowed_value.rs @@ -0,0 +1,51 @@ +use ::Result; +use ::error::Error; +use super::Value; + +pub enum BorrowedValue<'a> { + Null, + Integer(i64), + Real(f64), + Text(&'a str), + Blob(&'a [u8]), +} + +impl<'a> BorrowedValue<'a> { + pub fn to_value(&self) -> Value { + match *self { + BorrowedValue::Null => Value::Null, + BorrowedValue::Integer(i) => Value::Integer(i), + BorrowedValue::Real(r) => Value::Real(r), + BorrowedValue::Text(s) => Value::Text(s.to_string()), + BorrowedValue::Blob(b) => Value::Blob(b.to_vec()), + } + } + + pub fn as_i64(&self) -> Result { + match *self { + BorrowedValue::Integer(i) => Ok(i), + _ => Err(Error::InvalidColumnType), + } + } + + pub fn as_f64(&self) -> Result { + match *self { + BorrowedValue::Real(f) => Ok(f), + _ => Err(Error::InvalidColumnType), + } + } + + pub fn as_str(&self) -> Result<&str> { + match *self { + BorrowedValue::Text(ref t) => Ok(t), + _ => Err(Error::InvalidColumnType), + } + } + + pub fn as_blob(&self) -> Result<&[u8]> { + match *self { + BorrowedValue::Blob(ref b) => Ok(b), + _ => Err(Error::InvalidColumnType), + } + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs index 6110d25..f8c8fba 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -58,7 +58,9 @@ pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NUL pub use self::from_sql::FromSql; pub use self::to_sql::ToSql; +pub use self::borrowed_value::BorrowedValue; +mod borrowed_value; mod from_sql; mod to_sql; mod time;