use std::borrow::{Cow, ToOwned};
use std::collections::{BTreeMap, BTreeSet, LinkedList, VecDeque};
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Arc;
use super::{
__private, Direction, EnumDesc, EnumTag, FieldDesc, Html, Presence, Shape, Text, TupleElement,
TypeDesc, TypeRegistry, VariantDesc,
};
macro_rules! scalar {
($shape:ident: $($ty:ty),+ $(,)?) => {$(
impl Shape for $ty {
fn describe(_: &mut TypeRegistry, _: Direction) -> TypeDesc {
TypeDesc::$shape
}
}
)+};
}
scalar!(Null: ());
scalar!(Bool: bool);
scalar!(Int:
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,
std::num::NonZeroI8, std::num::NonZeroI16, std::num::NonZeroI32, std::num::NonZeroI64,
std::num::NonZeroI128, std::num::NonZeroIsize, std::num::NonZeroU8,
std::num::NonZeroU16, std::num::NonZeroU32, std::num::NonZeroU64, std::num::NonZeroU128,
std::num::NonZeroUsize,
);
scalar!(Float: f32, f64);
scalar!(String:
str, String, char, std::path::Path, std::path::PathBuf, std::net::IpAddr, std::net::Ipv4Addr,
std::net::Ipv6Addr, std::net::SocketAddr, std::net::SocketAddrV4, std::net::SocketAddrV6,
);
scalar!(Any: serde_json::Value);
scalar!(Text: Text);
impl Shape for Html {
fn describe(_: &mut TypeRegistry, direction: Direction) -> TypeDesc {
match direction {
Direction::Serialize => TypeDesc::Html,
Direction::Deserialize => TypeDesc::Union(vec![]),
}
}
}
impl<T> Shape for PhantomData<T> {
fn describe(_: &mut TypeRegistry, _: Direction) -> TypeDesc {
TypeDesc::Null
}
}
macro_rules! forward {
($($wrapper:ty),+ $(,)?) => {$(
impl<T: Shape + ?Sized> Shape for $wrapper {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
T::describe(registry, direction)
}
}
)+};
}
forward!(&T, &mut T, Box<T>, Rc<T>, Arc<T>);
impl<T: Shape + ToOwned + ?Sized> Shape for Cow<'_, T> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
T::describe(registry, direction)
}
}
impl<T: Shape> Shape for Option<T> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
TypeDesc::Option(Box::new(T::describe(registry, direction)))
}
}
fn list<T: Shape>(
registry: &mut TypeRegistry,
direction: Direction,
length: Option<usize>,
) -> TypeDesc {
TypeDesc::List {
element: Box::new(T::describe(registry, direction)),
length,
}
}
macro_rules! list_shapes {
($($ty:ty),+ $(,)?) => {$(
impl<T: Shape> Shape for $ty {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
list::<T>(registry, direction, None)
}
}
)+};
}
list_shapes!([T], Vec<T>, VecDeque<T>, LinkedList<T>, BTreeSet<T>);
impl<T: Shape, const N: usize> Shape for [T; N] {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
list::<T>(registry, direction, Some(N))
}
}
fn map<K: Shape, V: Shape>(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
TypeDesc::Map {
key: Box::new(K::describe(registry, direction)),
value: Box::new(V::describe(registry, direction)),
}
}
impl<K: Shape, V: Shape> Shape for BTreeMap<K, V> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
map::<K, V>(registry, direction)
}
}
impl<K: Shape, V: Shape, S> Shape for indexmap::IndexMap<K, V, S> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
map::<K, V>(registry, direction)
}
}
impl<T: Shape, S> Shape for indexmap::IndexSet<T, S> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
list::<T>(registry, direction, None)
}
}
macro_rules! tuples {
($next:ident $(,$rest:ident)*) => {
impl<$next: Shape, $($rest: Shape),*> Shape for ($next, $($rest,)*) {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
TypeDesc::Tuple(vec![
TupleElement { ty: $next::describe(registry, direction), omittable: false },
$(TupleElement { ty: $rest::describe(registry, direction), omittable: false },)*
])
}
}
tuples!($($rest),*);
};
() => {};
}
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
const BUILTIN: super::SourceLocation = __private::source(file!(), line!(), column!());
impl<T: Shape, E: Shape> Shape for Result<T, E> {
fn describe(registry: &mut TypeRegistry, direction: Direction) -> TypeDesc {
TypeDesc::Enum(EnumDesc {
tag: EnumTag::External,
variants: [
("Ok", T::describe(registry, direction)),
("Err", E::describe(registry, direction)),
]
.into_iter()
.map(|(name, ty)| VariantDesc {
name,
aliases: vec![],
untagged: false,
payload: Some(ty),
payload_omittable: false,
source: BUILTIN,
})
.collect(),
})
}
}
fn record<const N: usize>(fields: [(&'static str, TypeDesc); N]) -> TypeDesc {
TypeDesc::Struct(
fields
.into_iter()
.map(|(name, ty)| FieldDesc {
name,
aliases: vec![],
presence: Presence::Required,
flatten: false,
ty,
source: BUILTIN,
})
.collect(),
)
}
impl Shape for std::time::Duration {
fn describe(_: &mut TypeRegistry, _: Direction) -> TypeDesc {
record([("secs", TypeDesc::Int), ("nanos", TypeDesc::Int)])
}
}
impl Shape for std::time::SystemTime {
fn describe(_: &mut TypeRegistry, _: Direction) -> TypeDesc {
record([
("secs_since_epoch", TypeDesc::Int),
("nanos_since_epoch", TypeDesc::Int),
])
}
}
#[cfg(feature = "jiff")]
scalar!(Date:
jiff::Timestamp, jiff::ZonedDateTime, jiff::civil::DateTime, jiff::civil::Date,
jiff::civil::Time, jiff::civil::ISOWeekDate,
);
#[cfg(feature = "jiff")]
scalar!(String: jiff::Span, jiff::SignedDuration);
#[cfg(feature = "uuid")]
scalar!(String: uuid::Uuid, uuid::NonNilUuid);
#[cfg(feature = "ulid")]
scalar!(String: ulid::Ulid);
#[cfg(feature = "rust_decimal")]
impl Shape for rust_decimal::Decimal {
fn describe(_: &mut TypeRegistry, direction: Direction) -> TypeDesc {
match direction {
Direction::Serialize => TypeDesc::String,
Direction::Deserialize => {
TypeDesc::Union(vec![TypeDesc::String, TypeDesc::Int, TypeDesc::Float])
}
}
}
}