pub trait Attribute: Sized {
fn schema() -> serde_json::Value;
fn read(value: &serde_json::Value) -> Option<Self>;
fn write(&self) -> serde_json::Value;
fn required() -> bool {
true
}
}
pub struct Formatted<F: FormatName> {
value: String,
format: std::marker::PhantomData<F>,
}
impl<F: FormatName> Clone for Formatted<F> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
format: std::marker::PhantomData,
}
}
}
impl<F: FormatName> std::fmt::Debug for Formatted<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Formatted<{}>({:?})", F::NAME, self.value)
}
}
impl<F: FormatName> Default for Formatted<F> {
fn default() -> Self {
Self {
value: String::new(),
format: std::marker::PhantomData,
}
}
}
impl<F: FormatName> PartialEq for Formatted<F> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<F: FormatName> Eq for Formatted<F> {}
impl<F: FormatName> PartialOrd for Formatted<F> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<F: FormatName> Ord for Formatted<F> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.value.cmp(&other.value)
}
}
impl<F: FormatName> std::hash::Hash for Formatted<F> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
impl<F: FormatName> Formatted<F> {
pub fn as_str(&self) -> &str {
&self.value
}
pub fn into_inner(self) -> String {
self.value
}
}
impl<F: FormatName> std::fmt::Display for Formatted<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.value)
}
}
impl<F: FormatName> Attribute for Formatted<F> {
fn schema() -> serde_json::Value {
serde_json::json!({ "type": "string", "format": F::NAME })
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.as_str().map(|value| Self {
value: value.to_string(),
format: std::marker::PhantomData,
})
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(self.value.clone())
}
}
pub trait FormatName {
const NAME: &'static str;
}
macro_rules! formats {
($($(#[$doc:meta])* $name:ident => $format:literal),+ $(,)?) => {
$(
$(#[$doc])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct $name;
impl FormatName for $name {
const NAME: &'static str = $format;
}
)+
};
}
formats! {
DateTime => "date-time",
Date => "date",
Time => "time",
Duration => "duration",
Email => "email",
IdnEmail => "idn-email",
Hostname => "hostname",
Ipv4 => "ipv4",
Ipv6 => "ipv6",
Uuid => "uuid",
JsonPointer => "json-pointer",
RelativeJsonPointer => "relative-json-pointer",
}
impl Attribute for String {
fn schema() -> serde_json::Value {
serde_json::json!({ "type": "string" })
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.as_str().map(ToString::to_string)
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(self.clone())
}
}
impl Attribute for bool {
fn schema() -> serde_json::Value {
serde_json::json!({ "type": "boolean" })
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.as_bool()
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(*self)
}
}
macro_rules! integer_attribute {
($($ty:ty => $as:ident),+ $(,)?) => {
$(
impl Attribute for $ty {
fn schema() -> serde_json::Value {
serde_json::json!({
"type": "integer",
"minimum": <$ty>::MIN,
"maximum": <$ty>::MAX,
})
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.$as().and_then(|n| <$ty>::try_from(n).ok())
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(*self)
}
}
)+
};
}
integer_attribute! {
i8 => as_i64, i16 => as_i64, i32 => as_i64, i64 => as_i64,
u8 => as_u64, u16 => as_u64, u32 => as_u64, u64 => as_u64,
}
macro_rules! float_attribute {
($($ty:ty),+ $(,)?) => {
$(
impl Attribute for $ty {
fn schema() -> serde_json::Value {
serde_json::json!({ "type": "number" })
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.as_f64().map(|n| n as $ty)
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(*self)
}
}
)+
};
}
float_attribute!(f32, f64);
impl<T: Attribute> Attribute for Option<T> {
fn schema() -> serde_json::Value {
T::schema()
}
fn read(value: &serde_json::Value) -> Option<Self> {
if value.is_null() {
return Some(None);
}
T::read(value).map(Some)
}
fn write(&self) -> serde_json::Value {
match self {
Some(value) => value.write(),
None => serde_json::Value::Null,
}
}
fn required() -> bool {
false
}
}