use super::{Null, TimeUnit, Type};
use rust_decimal::prelude::*;
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Null,
Boolean(bool),
TinyInt(i8),
SmallInt(i16),
Int(i32),
BigInt(i64),
HugeInt(i128),
UTinyInt(u8),
USmallInt(u16),
UInt(u32),
UBigInt(u64),
Float(f32),
Double(f64),
Decimal(Decimal),
Timestamp(TimeUnit, i64),
Text(String),
Blob(Vec<u8>),
Date32(i32),
Time64(TimeUnit, i64),
}
impl From<Null> for Value {
#[inline]
fn from(_: Null) -> Value {
Value::Null
}
}
impl From<bool> for Value {
#[inline]
fn from(i: bool) -> Value {
Value::Boolean(i)
}
}
impl From<usize> for Value {
#[inline]
fn from(i: usize) -> Value {
Value::BigInt(i as i64)
}
}
impl From<isize> for Value {
#[inline]
fn from(i: isize) -> Value {
Value::BigInt(i as i64)
}
}
#[cfg(feature = "uuid")]
impl From<uuid::Uuid> for Value {
#[inline]
fn from(id: uuid::Uuid) -> Value {
Value::Text(id.to_string())
}
}
impl From<i8> for Value {
#[inline]
fn from(i: i8) -> Value {
Value::TinyInt(i)
}
}
impl From<i16> for Value {
#[inline]
fn from(i: i16) -> Value {
Value::SmallInt(i)
}
}
impl From<i32> for Value {
#[inline]
fn from(i: i32) -> Value {
Value::Int(i)
}
}
impl From<i64> for Value {
#[inline]
fn from(i: i64) -> Value {
Value::BigInt(i)
}
}
macro_rules! from_i64(
($t:ty) => (
impl From<$t> for Value {
#[inline]
fn from(i: $t) -> Value {
Value::BigInt(i as i64)
}
}
)
);
from_i64!(u8);
from_i64!(u16);
from_i64!(u32);
from_i64!(u64);
impl From<i128> for Value {
#[inline]
fn from(i: i128) -> Value {
Value::HugeInt(i)
}
}
impl From<f32> for Value {
#[inline]
fn from(f: f32) -> Value {
Value::Float(f)
}
}
impl From<f64> for Value {
#[inline]
fn from(f: f64) -> Value {
Value::Double(f)
}
}
impl From<String> for Value {
#[inline]
fn from(s: String) -> Value {
Value::Text(s)
}
}
impl From<Vec<u8>> for Value {
#[inline]
fn from(v: Vec<u8>) -> Value {
Value::Blob(v)
}
}
impl<T> From<Option<T>> for Value
where
T: Into<Value>,
{
#[inline]
fn from(v: Option<T>) -> Value {
match v {
Some(x) => x.into(),
None => Value::Null,
}
}
}
impl Value {
#[inline]
pub fn data_type(&self) -> Type {
match *self {
Value::Null => Type::Null,
Value::Boolean(_) => Type::Boolean,
Value::TinyInt(_) => Type::TinyInt,
Value::SmallInt(_) => Type::SmallInt,
Value::Int(_) => Type::Int,
Value::BigInt(_) => Type::BigInt,
Value::HugeInt(_) => Type::HugeInt,
Value::UTinyInt(_) => Type::UTinyInt,
Value::USmallInt(_) => Type::USmallInt,
Value::UInt(_) => Type::UInt,
Value::UBigInt(_) => Type::UBigInt,
Value::Float(_) => Type::Float,
Value::Double(_) => Type::Double,
Value::Decimal(_) => Type::Decimal,
Value::Timestamp(_, _) => Type::Timestamp,
Value::Text(_) => Type::Text,
Value::Blob(_) => Type::Blob,
Value::Date32(_) => Type::Date32,
Value::Time64(..) => Type::Time64,
}
}
}