Add conversion methods to ColumnValueOps

This commit is contained in:
Dan Spencer 2015-04-13 04:05:40 -06:00
parent ba3f05eb0e
commit 4b76b7fe81
2 changed files with 36 additions and 0 deletions

View File

@ -5,6 +5,14 @@ pub trait ColumnValueOps: Sized {
fn from_string_literal(s: Cow<str>) -> Result<Self, Cow<str>>;
fn from_number_literal(s: Cow<str>) -> Result<Self, Cow<str>>;
fn from_f64(value: f64) -> Self;
fn to_f64(self) -> Result<f64, ()>;
fn from_u64(value: u64) -> Self;
fn to_u64(self) -> Result<u64, ()>;
fn null() -> Self { ColumnValueOps::from_3vl(0) }
fn from_bytes(dbtype: DbType, bytes: Cow<[u8]>) -> Result<Self, ()>;
fn to_bytes(self, dbtype: DbType) -> Result<Box<[u8]>, ()>;
fn get_dbtype(&self) -> DbType;
@ -25,6 +33,8 @@ pub trait ColumnValueOps: Sized {
fn equals(&self, rhs: &Self) -> Self;
fn not_equals(&self, rhs: &Self) -> Self;
fn is_null(&self) -> bool { self.to_3vl() == 0 }
fn not(&self) -> Self {
ColumnValueOps::from_3vl(-self.to_3vl())
}

View File

@ -55,6 +55,32 @@ impl ColumnValueOps for Variant {
}
}
fn from_f64(value: f64) -> Variant {
Variant::Float(F64NoNaN::new(value).unwrap())
}
fn to_f64(self) -> Result<f64, ()> {
let num = self.cast(DbType::F64);
if let Some(Variant::Float(float)) = num {
Ok(*float)
} else {
Err(())
}
}
fn from_u64(value: u64) -> Variant {
Variant::UnsignedInteger(value)
}
fn to_u64(self) -> Result<u64, ()> {
let num = self.cast(DbType::Integer { signed: false, bytes: 8 });
if let Some(Variant::UnsignedInteger(i)) = num {
Ok(i)
} else {
Err(())
}
}
fn from_bytes(dbtype: DbType, bytes: Cow<[u8]>) -> Result<Variant, ()> {
match dbtype {
DbType::Null => Ok(Variant::Null),