use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GenericValue {
Null,
Bool(bool),
SmallInt(i16),
Int(i32),
BigInt(i64),
Float(f32),
Double(f64),
Decimal(Decimal),
String(String),
Uuid(Uuid),
Date(NaiveDate),
Time(NaiveTime),
Timestamp(NaiveDateTime),
TimestampTz(DateTime<Utc>),
Bytes(Vec<u8>),
Array(Vec<GenericValue>),
Map(BTreeMap<String, GenericValue>),
}
impl GenericValue {
pub fn is_bindable_scalar(&self) -> bool {
match self {
GenericValue::Bool(_)
| GenericValue::SmallInt(_)
| GenericValue::Int(_)
| GenericValue::BigInt(_)
| GenericValue::Float(_)
| GenericValue::Double(_)
| GenericValue::Decimal(_)
| GenericValue::String(_)
| GenericValue::Uuid(_)
| GenericValue::Date(_)
| GenericValue::Time(_)
| GenericValue::Timestamp(_)
| GenericValue::TimestampTz(_)
| GenericValue::Bytes(_) => true,
GenericValue::Null | GenericValue::Array(_) | GenericValue::Map(_) => false,
}
}
}
impl PartialEq for GenericValue {
fn eq(&self, other: &Self) -> bool {
use GenericValue::*;
match (self, other) {
(Null, Null) => true,
(Bool(a), Bool(b)) => a == b,
(SmallInt(a), SmallInt(b)) => a == b,
(Int(a), Int(b)) => a == b,
(BigInt(a), BigInt(b)) => a == b,
(Float(a), Float(b)) => a.to_bits() == b.to_bits(),
(Double(a), Double(b)) => a.to_bits() == b.to_bits(),
(Decimal(a), Decimal(b)) => a == b,
(String(a), String(b)) => a == b,
(Uuid(a), Uuid(b)) => a == b,
(Date(a), Date(b)) => a == b,
(Time(a), Time(b)) => a == b,
(Timestamp(a), Timestamp(b)) => a == b,
(TimestampTz(a), TimestampTz(b)) => a == b,
(Bytes(a), Bytes(b)) => a == b,
(Array(a), Array(b)) => a == b,
(Map(a), Map(b)) => a == b,
_ => false,
}
}
}
impl Eq for GenericValue {}
impl Hash for GenericValue {
fn hash<H: Hasher>(&self, state: &mut H) {
use GenericValue::*;
std::mem::discriminant(self).hash(state);
match self {
Null => {}
Bool(v) => v.hash(state),
SmallInt(v) => v.hash(state),
Int(v) => v.hash(state),
BigInt(v) => v.hash(state),
Float(v) => v.to_bits().hash(state),
Double(v) => v.to_bits().hash(state),
Decimal(v) => v.hash(state),
String(v) => v.hash(state),
Uuid(v) => v.hash(state),
Date(v) => v.hash(state),
Time(v) => v.hash(state),
Timestamp(v) => v.hash(state),
TimestampTz(v) => v.hash(state),
Bytes(v) => v.hash(state),
Array(v) => v.hash(state),
Map(v) => v.hash(state),
}
}
}