use std::fmt;
use sqlx_core::type_info::TypeInfo;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpgTypeInfo {
kind: Kind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Kind {
Int,
BigInt,
SmallInt,
Bool,
Text,
Bytes,
Float,
Date,
Timestamp,
Timestamptz,
Json,
Null,
}
impl SpgTypeInfo {
#[must_use]
pub const fn of(kind: Kind) -> Self {
Self { kind }
}
#[must_use]
pub const fn kind(&self) -> Kind {
self.kind
}
#[must_use]
pub fn from_data_type(ty: spg_embedded::DataType) -> Self {
use spg_embedded::DataType;
let kind = match ty {
DataType::Int => Kind::Int,
DataType::BigInt => Kind::BigInt,
DataType::SmallInt => Kind::SmallInt,
DataType::Bool => Kind::Bool,
DataType::Text => Kind::Text,
DataType::Bytes => Kind::Bytes,
DataType::Float => Kind::Float,
DataType::Date => Kind::Date,
DataType::Timestamp => Kind::Timestamp,
DataType::Timestamptz => Kind::Timestamptz,
DataType::Json => Kind::Json,
_ => Kind::Null,
};
Self { kind }
}
}
impl TypeInfo for SpgTypeInfo {
fn is_null(&self) -> bool {
matches!(self.kind, Kind::Null)
}
fn name(&self) -> &str {
match self.kind {
Kind::Int => "INT",
Kind::BigInt => "BIGINT",
Kind::SmallInt => "SMALLINT",
Kind::Bool => "BOOLEAN",
Kind::Text => "TEXT",
Kind::Bytes => "BYTEA",
Kind::Float => "FLOAT",
Kind::Date => "DATE",
Kind::Timestamp => "TIMESTAMP",
Kind::Timestamptz => "TIMESTAMPTZ",
Kind::Json => "JSON",
Kind::Null => "NULL",
}
}
}
impl fmt::Display for SpgTypeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}