use alloc::string::String;
use core::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
};
use super::{
array::PgArray,
cast::{CastError, CastValue},
datetime::{PgDate, PgTimestamp},
integer::IntegerType,
jsonb::PgJsonb,
numeric::PgNumeric,
};
#[derive(Debug, Clone)]
pub enum PgValue {
Boolean(bool),
Integer(i64),
Numeric(PgNumeric),
Text(String),
Date(PgDate),
Timestamp(PgTimestamp),
Array(PgArray),
Jsonb(PgJsonb),
}
impl PgValue {
const fn family(&self) -> u8 {
match self {
Self::Boolean(_) => 0,
Self::Integer(_) | Self::Numeric(_) => 1,
Self::Text(_) => 2,
Self::Date(_) | Self::Timestamp(_) => 3,
Self::Array(_) => 4,
Self::Jsonb(_) => 5,
}
}
pub fn into_cast_value(self, integer: IntegerType) -> Result<CastValue, CastError> {
Ok(match self {
Self::Boolean(value) => CastValue::Boolean(value),
Self::Integer(value) => return integer.value(value),
Self::Numeric(value) => CastValue::Numeric(value),
Self::Text(text) => CastValue::Text(text),
Self::Date(date) => CastValue::Date(date),
Self::Timestamp(at) => CastValue::Timestamp(at),
Self::Jsonb(value) => CastValue::Jsonb(value),
Self::Array(_) => return Err(CastError::Unsupported),
})
}
#[must_use]
pub fn as_numeric(&self) -> Option<PgNumeric> {
match self {
Self::Integer(value) => Some(PgNumeric::from(*value)),
Self::Numeric(value) => Some(value.clone()),
_ => None,
}
}
#[must_use]
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(text) => Some(text),
_ => None,
}
}
#[must_use]
pub const fn as_date(&self) -> Option<PgDate> {
match self {
Self::Date(date) => Some(*date),
_ => None,
}
}
#[must_use]
pub fn as_timestamp(&self) -> Option<PgTimestamp> {
match self {
Self::Date(date) => date.to_timestamp().ok(),
Self::Timestamp(at) => Some(*at),
_ => None,
}
}
#[must_use]
pub fn output(&self) -> Option<impl fmt::Display + '_> {
match self {
Self::Array(array) => (array
.elements()
.iter()
.all(|value| !matches!(value, Some(Self::Date(_) | Self::Timestamp(_)))))
.then_some(self),
_ => (!matches!(self, Self::Date(_) | Self::Timestamp(_))).then_some(self),
}
}
}
impl TryFrom<CastValue> for PgValue {
type Error = CastValue;
fn try_from(value: CastValue) -> Result<Self, Self::Error> {
Ok(match value {
CastValue::Boolean(value) => Self::Boolean(value),
CastValue::Smallint(value) => Self::Integer(value.into()),
CastValue::Integer(value) => Self::Integer(value.into()),
CastValue::Bigint(value) => Self::Integer(value),
CastValue::Numeric(value) => Self::Numeric(value),
CastValue::Text(text) => Self::Text(text),
CastValue::Date(date) => Self::Date(date),
CastValue::Timestamp(at) => Self::Timestamp(at),
CastValue::Jsonb(value) => Self::Jsonb(value),
CastValue::Bpchar(_) | CastValue::TimestampTz(_) => return Err(value),
})
}
}
impl PartialEq for PgValue {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for PgValue {}
impl PartialOrd for PgValue {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PgValue {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::Boolean(left), Self::Boolean(right)) => left.cmp(right),
(Self::Integer(left), Self::Integer(right)) => left.cmp(right),
(Self::Integer(left), Self::Numeric(right)) => right.cmp_integer(*left).reverse(),
(Self::Numeric(left), Self::Integer(right)) => left.cmp_integer(*right),
(Self::Numeric(left), Self::Numeric(right)) => left.cmp(right),
(Self::Text(left), Self::Text(right)) => left.cmp(right),
(Self::Date(left), Self::Date(right)) => left.cmp(right),
(Self::Date(left), Self::Timestamp(right)) => left.cmp_timestamp(*right),
(Self::Timestamp(left), Self::Date(right)) => right.cmp_timestamp(*left).reverse(),
(Self::Timestamp(left), Self::Timestamp(right)) => left.cmp(right),
(Self::Array(left), Self::Array(right)) => left.cmp(right),
(Self::Jsonb(left), Self::Jsonb(right)) => left.cmp(right),
(left, right) => left.family().cmp(&right.family()),
}
}
}
impl Hash for PgValue {
fn hash<H: Hasher>(&self, state: &mut H) {
self.family().hash(state);
match self {
Self::Boolean(value) => value.hash(state),
Self::Integer(value) => PgNumeric::hash_integer(*value, state),
Self::Numeric(value) => value.hash(state),
Self::Text(text) => text.hash(state),
Self::Date(date) => match date.to_timestamp() {
Ok(at) => at.hash(state),
Err(_) => date.hash(state),
},
Self::Timestamp(at) => at.hash(state),
Self::Array(array) => array.hash(state),
Self::Jsonb(value) => value.hash(state),
}
}
}
impl fmt::Display for PgValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Boolean(value) => f.write_str(if *value { "t" } else { "f" }),
Self::Integer(value) => write!(f, "{value}"),
Self::Numeric(value) => write!(f, "{value}"),
Self::Text(text) => f.write_str(text),
Self::Date(date) => write!(f, "{date}"),
Self::Timestamp(at) => write!(f, "{at}"),
Self::Array(array) => write!(f, "{array}"),
Self::Jsonb(value) => write!(f, "{value}"),
}
}
}