Add types::BorrowedValue.

This commit is contained in:
John Gallagher 2016-05-23 21:48:56 -04:00
parent efc6c89370
commit c90cd37c00
2 changed files with 53 additions and 0 deletions

View File

@ -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<i64> {
match *self {
BorrowedValue::Integer(i) => Ok(i),
_ => Err(Error::InvalidColumnType),
}
}
pub fn as_f64(&self) -> Result<f64> {
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),
}
}
}

View File

@ -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;