use super::{Null, OrderedMap, 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),
Interval {
months: i32,
days: i32,
nanos: i64,
},
List(Vec<Value>),
Enum(String),
Struct(OrderedMap<String, Value>),
Array(Vec<Value>),
Map(OrderedMap<Value, Value>),
Union(Box<Value>),
}
impl From<Null> for Value {
#[inline]
fn from(_: Null) -> Self {
Self::Null
}
}
impl From<bool> for Value {
#[inline]
fn from(i: bool) -> Self {
Self::Boolean(i)
}
}
impl From<usize> for Value {
#[inline]
fn from(i: usize) -> Self {
Self::UBigInt(i as u64)
}
}
impl From<isize> for Value {
#[inline]
fn from(i: isize) -> Self {
Self::BigInt(i as i64)
}
}
#[cfg(feature = "uuid")]
impl From<uuid::Uuid> for Value {
#[inline]
fn from(id: uuid::Uuid) -> Self {
Self::Text(id.to_string())
}
}
impl From<i8> for Value {
#[inline]
fn from(i: i8) -> Self {
Self::TinyInt(i)
}
}
impl From<i16> for Value {
#[inline]
fn from(i: i16) -> Self {
Self::SmallInt(i)
}
}
impl From<i32> for Value {
#[inline]
fn from(i: i32) -> Self {
Self::Int(i)
}
}
impl From<i64> for Value {
#[inline]
fn from(i: i64) -> Self {
Self::BigInt(i)
}
}
impl From<u8> for Value {
#[inline]
fn from(i: u8) -> Self {
Self::UTinyInt(i)
}
}
impl From<u16> for Value {
#[inline]
fn from(i: u16) -> Self {
Self::USmallInt(i)
}
}
impl From<u32> for Value {
#[inline]
fn from(i: u32) -> Self {
Self::UInt(i)
}
}
impl From<u64> for Value {
#[inline]
fn from(i: u64) -> Self {
Self::UBigInt(i)
}
}
impl From<i128> for Value {
#[inline]
fn from(i: i128) -> Self {
Self::HugeInt(i)
}
}
impl From<f32> for Value {
#[inline]
fn from(f: f32) -> Self {
Self::Float(f)
}
}
impl From<f64> for Value {
#[inline]
fn from(f: f64) -> Self {
Self::Double(f)
}
}
impl From<String> for Value {
#[inline]
fn from(s: String) -> Self {
Self::Text(s)
}
}
impl From<Vec<u8>> for Value {
#[inline]
fn from(v: Vec<u8>) -> Self {
Self::Blob(v)
}
}
impl<T> From<Option<T>> for Value
where
T: Into<Self>,
{
#[inline]
fn from(v: Option<T>) -> Self {
match v {
Some(x) => x.into(),
None => Self::Null,
}
}
}
impl Value {
#[inline]
pub fn data_type(&self) -> Type {
match *self {
Self::Null => Type::Null,
Self::Boolean(_) => Type::Boolean,
Self::TinyInt(_) => Type::TinyInt,
Self::SmallInt(_) => Type::SmallInt,
Self::Int(_) => Type::Int,
Self::BigInt(_) => Type::BigInt,
Self::HugeInt(_) => Type::HugeInt,
Self::UTinyInt(_) => Type::UTinyInt,
Self::USmallInt(_) => Type::USmallInt,
Self::UInt(_) => Type::UInt,
Self::UBigInt(_) => Type::UBigInt,
Self::Float(_) => Type::Float,
Self::Double(_) => Type::Double,
Self::Decimal(_) => Type::Decimal,
Self::Timestamp(_, _) => Type::Timestamp,
Self::Text(_) => Type::Text,
Self::Blob(_) => Type::Blob,
Self::Date32(_) => Type::Date32,
Self::Time64(..) => Type::Time64,
Self::Interval { .. } => Type::Interval,
Self::Union(..) | Self::Struct(..) | Self::List(..) | Self::Array(..) | Self::Map(..) => todo!(),
Self::Enum(..) => Type::Enum,
}
}
}