use bytes::Bytes;
use serde_json::{Map, Value};
use std::collections::{BTreeMap, HashMap};
#[derive(Debug, Clone, Copy)]
pub struct Field {
pub name: &'static str,
pub attrs: &'static [&'static str],
pub shape: fn() -> Shape,
}
#[derive(Debug, Clone)]
pub enum Shape {
Unit,
String,
Bool,
Integer,
Number,
Json,
Native(&'static str),
Option(Box<Shape>),
Patch(Box<Shape>),
List(Box<Shape>),
Map(Box<Shape>, Box<Shape>),
Enum {
name: &'static str,
values: &'static [&'static str],
},
Struct {
name: &'static str,
attrs: &'static [&'static str],
fields: fn() -> Vec<Field>,
},
Union {
name: &'static str,
attrs: &'static [&'static str],
variants: fn() -> Vec<Field>,
},
}
pub trait Described {
fn shape() -> Shape;
}
macro_rules! leaf {
($shape:ident: $($ty:ty),*) => {
$(impl Described for $ty {
fn shape() -> Shape {
Shape::$shape
}
})*
};
}
leaf!(Unit: ());
leaf!(String: String);
leaf!(Bool: bool);
leaf!(Integer: u8, u16, u32, u64, usize, i8, i16, i32, i64);
leaf!(Number: f32, f64);
leaf!(Json: Value);
impl Described for Bytes {
fn shape() -> Shape {
Shape::Native("Bytes")
}
}
impl<T: Described> Described for Option<T> {
fn shape() -> Shape {
Shape::Option(Box::new(T::shape()))
}
}
impl<T: Described> Described for Box<T> {
fn shape() -> Shape {
T::shape()
}
}
impl<T: Described> Described for Vec<T> {
fn shape() -> Shape {
Shape::List(Box::new(T::shape()))
}
}
impl<T: Described> Described for super::Patch<T> {
fn shape() -> Shape {
Shape::Patch(Box::new(T::shape()))
}
}
impl<K: Described, V: Described, S> Described for HashMap<K, V, S> {
fn shape() -> Shape {
Shape::Map(Box::new(K::shape()), Box::new(V::shape()))
}
}
impl<K: Described, V: Described> Described for BTreeMap<K, V> {
fn shape() -> Shape {
Shape::Map(Box::new(K::shape()), Box::new(V::shape()))
}
}
impl Described for Map<String, Value> {
fn shape() -> Shape {
Shape::Map(Box::new(Shape::String), Box::new(Shape::Json))
}
}
impl Described for super::Content {
fn shape() -> Shape {
Shape::Native("Content")
}
}
impl Described for std::time::Duration {
fn shape() -> Shape {
Shape::Native("Duration")
}
}
impl Described for super::ImageTransformOptions {
fn shape() -> Shape {
Shape::Native("ImageTransformOptions")
}
}
impl Described for super::EventAsset {
fn shape() -> Shape {
Shape::Native("EventAsset")
}
}