use phf::phf_map;
use strum_macros::{AsRefStr, Display, EnumString};
#[cfg(feature = "alloc")]
use ::alloc::{borrow::Cow, format, string::String};
#[derive(
AsRefStr, Clone, Debug, Default, Display, EnumString, Eq, Hash, Ord, PartialEq, PartialOrd,
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PrimitiveType {
#[default]
#[strum(to_string = "string")]
String,
#[strum(to_string = "boolean")]
Boolean,
#[strum(to_string = "decimal")]
Decimal,
#[strum(to_string = "float")]
Float,
#[strum(to_string = "double")]
Double,
#[strum(to_string = "duration")]
Duration,
#[strum(to_string = "dateTime")]
DateTime,
#[strum(to_string = "time")]
Time,
#[strum(to_string = "date")]
Date,
#[strum(to_string = "gYearMonth")]
GYearMonth,
#[strum(to_string = "gYear")]
GYear,
#[strum(to_string = "gMonthDay")]
GMonthDay,
#[strum(to_string = "gDay")]
GDay,
#[strum(to_string = "gMonth")]
GMonth,
#[strum(to_string = "hexBinary")]
HexBinary,
#[strum(to_string = "base64Binary")]
Base64Binary,
#[strum(to_string = "anyURI")]
AnyUri,
#[strum(to_string = "QName")]
QName,
#[strum(to_string = "{0}")]
#[strum(default)]
#[cfg(feature = "alloc")]
Other(String),
#[cfg(not(feature = "alloc"))]
Other(&'static str),
}
impl PrimitiveType {
pub fn name(&self) -> &str {
use PrimitiveType::*;
match self {
String => "string",
Boolean => "boolean",
Decimal => "decimal",
Float => "float",
Double => "double",
Duration => "duration",
DateTime => "dateTime",
Time => "time",
Date => "date",
GYearMonth => "gYearMonth",
GYear => "gYear",
GMonthDay => "gMonthDay",
GDay => "gDay",
GMonth => "gMonth",
HexBinary => "hexBinary",
Base64Binary => "base64Binary",
AnyUri => "anyURI",
QName => "QName",
Other(s) => s.as_ref(),
}
}
pub fn curie(&self) -> &str {
use PrimitiveType::*;
match self {
String => "xsd:string",
Boolean => "xsd:boolean",
Decimal => "xsd:decimal",
Float => "xsd:float",
Double => "xsd:double",
Duration => "xsd:duration",
DateTime => "xsd:dateTime",
Time => "xsd:time",
Date => "xsd:date",
GYearMonth => "xsd:gYearMonth",
GYear => "xsd:gYear",
GMonthDay => "xsd:gMonthDay",
GDay => "xsd:gDay",
GMonth => "xsd:gMonth",
HexBinary => "xsd:hexBinary",
Base64Binary => "xsd:base64Binary",
AnyUri => "xsd:anyURI",
QName => "xsd:QName",
Other(s) => s.as_ref(),
}
}
#[cfg(feature = "alloc")]
pub fn iri_string(&self) -> Cow<'_, str> {
Cow::Owned(format!("{}{}", crate::BASE_URI, self))
}
}
#[allow(unused)]
static PRIMITIVE_TYPES: phf::Map<&'static str, PrimitiveType> = phf_map! {
"string" => PrimitiveType::String,
"boolean" => PrimitiveType::Boolean,
"decimal" => PrimitiveType::Decimal,
"float" => PrimitiveType::Float,
"double" => PrimitiveType::Double,
"duration" => PrimitiveType::Duration,
"dateTime" => PrimitiveType::DateTime,
"time" => PrimitiveType::Time,
"date" => PrimitiveType::Date,
"gYearMonth" => PrimitiveType::GYearMonth,
"gYear" => PrimitiveType::GYear,
"gMonthDay" => PrimitiveType::GMonthDay,
"gDay" => PrimitiveType::GDay,
"gMonth" => PrimitiveType::GMonth,
"hexBinary" => PrimitiveType::HexBinary,
"base64Binary" => PrimitiveType::Base64Binary,
"anyURI" => PrimitiveType::AnyUri,
"QName" => PrimitiveType::QName,
};