use std::fmt;
use std::str::FromStr;
use super::data_type_parse_error::DataTypeParseError;
use serde::{
Deserialize,
Serialize,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DataType {
Bool,
Char,
Int8,
Int16,
Int32,
Int64,
Int128,
UInt8,
UInt16,
UInt32,
UInt64,
UInt128,
Float32,
Float64,
String,
Date,
Time,
DateTime,
Instant,
BigInteger,
BigDecimal,
IntSize,
UIntSize,
Duration,
Url,
StringMap,
Json,
}
impl fmt::Display for DataType {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for DataType {
type Err = DataTypeParseError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_ascii_lowercase().as_str() {
"bool" => Ok(DataType::Bool),
"char" => Ok(DataType::Char),
"int8" => Ok(DataType::Int8),
"int16" => Ok(DataType::Int16),
"int32" => Ok(DataType::Int32),
"int64" => Ok(DataType::Int64),
"int128" => Ok(DataType::Int128),
"uint8" => Ok(DataType::UInt8),
"uint16" => Ok(DataType::UInt16),
"uint32" => Ok(DataType::UInt32),
"uint64" => Ok(DataType::UInt64),
"uint128" => Ok(DataType::UInt128),
"float32" => Ok(DataType::Float32),
"float64" => Ok(DataType::Float64),
"string" => Ok(DataType::String),
"date" => Ok(DataType::Date),
"time" => Ok(DataType::Time),
"datetime" => Ok(DataType::DateTime),
"instant" => Ok(DataType::Instant),
"biginteger" => Ok(DataType::BigInteger),
"bigdecimal" => Ok(DataType::BigDecimal),
"intsize" => Ok(DataType::IntSize),
"uintsize" => Ok(DataType::UIntSize),
"duration" => Ok(DataType::Duration),
"url" => Ok(DataType::Url),
"stringmap" => Ok(DataType::StringMap),
"json" => Ok(DataType::Json),
_ => Err(DataTypeParseError::new(value)),
}
}
}
impl DataType {
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
DataType::Bool => "bool",
DataType::Char => "char",
DataType::Int8 => "int8",
DataType::Int16 => "int16",
DataType::Int32 => "int32",
DataType::Int64 => "int64",
DataType::Int128 => "int128",
DataType::UInt8 => "uint8",
DataType::UInt16 => "uint16",
DataType::UInt32 => "uint32",
DataType::UInt64 => "uint64",
DataType::UInt128 => "uint128",
DataType::Float32 => "float32",
DataType::Float64 => "float64",
DataType::String => "string",
DataType::Date => "date",
DataType::Time => "time",
DataType::DateTime => "datetime",
DataType::Instant => "instant",
DataType::BigInteger => "biginteger",
DataType::BigDecimal => "bigdecimal",
DataType::IntSize => "intsize",
DataType::UIntSize => "uintsize",
DataType::Duration => "duration",
DataType::Url => "url",
DataType::StringMap => "stringmap",
DataType::Json => "json",
}
}
}