use super::{GenerationError, SchemaLimits, ValidatedRegistry};
use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, string::String, vec::Vec};
use serde_json::{Value, json};
mod adapters;
#[cfg(feature = "platform-types")]
mod platform;
mod registry;
pub use adapters::PackedField;
pub use registry::SchemaRegistry;
pub trait Wire {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError>;
}
#[derive(Debug)]
pub struct SchemaContext {
remaining: usize,
depth: usize,
active: Vec<&'static str>,
failed: bool,
names: Option<Box<registry::Names>>,
}
impl SchemaContext {
#[doc(hidden)]
pub fn describe_bytes<T: SerdeBytesField + ?Sized>(
&mut self,
) -> Result<Value, GenerationError> {
self.describe::<T::Layout>()
}
pub fn describe<T: Wire + ?Sized>(&mut self) -> Result<Value, GenerationError> {
if self.failed {
return Err(GenerationError::Schema);
}
self.failed = true;
let name = core::any::type_name::<T>();
if self.names.is_none() && self.active.contains(&name) {
return Err(GenerationError::Schema);
}
if self.active.len() > self.depth {
return Err(GenerationError::Complexity);
}
self.remaining = self
.remaining
.checked_sub(1)
.ok_or(GenerationError::Complexity)?;
self.active.push(name);
self.failed = false;
let result = if self.names.is_some() {
self.named::<T>()
} else {
T::schema(self)
};
self.active.pop();
if self.failed && result.is_ok() {
return Err(GenerationError::Schema);
}
if result.is_err() {
self.failed = true;
}
result
}
}
impl ValidatedRegistry {
pub fn from_type<T: Wire + ?Sized>(
root: &str,
limits: SchemaLimits,
) -> Result<Self, GenerationError> {
super::identifier(root)?;
if limits.depth > 64 || root.len() > limits.names_bytes {
return Err(GenerationError::Complexity);
}
let mut context = SchemaContext {
remaining: limits.nodes,
depth: limits.depth,
active: Vec::new(),
failed: false,
names: None,
};
let schema = context.describe::<T>()?;
Self::from_metadata(&BTreeMap::from([(String::from(root), schema)]), limits)
}
}
macro_rules! scalar {
($($ty:ty => $value:expr),* $(,)?) => { $(
impl Wire for $ty {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> { Ok($value) }
}
)* };
}
scalar! {
crate::FiniteF32 => json!({"k":"float","bits":32}),
crate::FiniteF64 => json!({"k":"float","bits":64}),
() => json!({"k":"unit"}), bool => json!({"k":"bool"}),
u8 => json!({"k":"u","bits":8}), u16 => json!({"k":"u","bits":16}),
u32 => json!({"k":"u","bits":32}), u64 => json!({"k":"u","bits":64}), u128 => json!({"k":"u","bits":128}),
i8 => json!({"k":"i","bits":8}), i16 => json!({"k":"i","bits":16}),
i32 => json!({"k":"i","bits":32}), i64 => json!({"k":"i","bits":64}), i128 => json!({"k":"i","bits":128}),
str => json!({"k":"text"}), String => json!({"k":"text"}),
serde_bytes::ByteBuf => json!({"k":"bytes"}), serde_bytes::Bytes => json!({"k":"bytes"}),
}
impl<const N: usize> Wire for serde_bytes::ByteArray<N> {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"bytes","min":N,"max":N}))
}
}
impl<T: crate::PackedElement, const N: usize> Wire for crate::Packed<T, N> {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> {
let mut value = json!({"k":"packed","word":T::SCHEMA});
if N != usize::MAX {
value["max"] = json!(N);
}
Ok(value)
}
}
mod byte_field {
pub trait Sealed {
type Layout: super::Wire;
}
impl Sealed for alloc::vec::Vec<u8> {
type Layout = serde_bytes::ByteBuf;
}
impl Sealed for [u8] {
type Layout = serde_bytes::ByteBuf;
}
impl Sealed for serde_bytes::ByteBuf {
type Layout = Self;
}
impl Sealed for serde_bytes::Bytes {
type Layout = serde_bytes::ByteBuf;
}
impl<const N: usize> Sealed for [u8; N] {
type Layout = serde_bytes::ByteArray<N>;
}
impl<const N: usize> Sealed for serde_bytes::ByteArray<N> {
type Layout = Self;
}
impl<T: Sealed + ?Sized> Sealed for &T {
type Layout = T::Layout;
}
impl<T: Sealed> Sealed for Option<T> {
type Layout = Option<T::Layout>;
}
}
#[doc(hidden)]
pub trait SerdeBytesField: byte_field::Sealed {}
impl<T: byte_field::Sealed + ?Sized> SerdeBytesField for T {}
impl<T: Wire> Wire for Option<T> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"option","item":context.describe::<T>()?}))
}
}
impl<T: Wire, E: Wire> Wire for Result<T, E> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"result","ok":context.describe::<T>()?,"err":context.describe::<E>()?}))
}
}
impl<T: Wire, const N: usize> Wire for [T; N]
where
[T; N]: serde::Serialize,
{
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"array","max":N,"item":context.describe::<T>()?}))
}
}
impl<T: Wire> Wire for [T] {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"list","item":context.describe::<T>()?}))
}
}
impl Wire for Cow<'_, str> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
context.describe::<str>()
}
}
macro_rules! tuple {
($($ty:ident),+) => {
impl<$($ty: Wire),+> Wire for ($($ty,)+) {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"tuple","items":[$(context.describe::<$ty>()?),+]}))
}
}
};
}
tuple!(A);
tuple!(A, B);
tuple!(A, B, C);
tuple!(A, B, C, D);
impl<T: Wire> Wire for Vec<T> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"list","item":context.describe::<T>()?}))
}
}
impl<T: Wire + ?Sized> Wire for Box<T> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
context.describe::<T>()
}
}
impl<T: Wire + ?Sized> Wire for &T {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
context.describe::<T>()
}
}
impl<T: Wire, const N: usize> Wire for crate::List<T, N> {
fn schema(context: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"list","max":N,"item":context.describe::<T>()?}))
}
}
macro_rules! bounded {
($($ty:ident => $kind:literal),* $(,)?) => { $(
impl<const N: usize> Wire for crate::$ty<N> {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> { Ok(json!({"k":$kind,"max":N})) }
}
)* };
}
bounded! { Bytes => "bytes", Text => "text" }
impl<const N: usize> Wire for crate::BytesRef<'_, N> {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"bytes","max":N}))
}
}
impl<const N: usize> Wire for crate::TextRef<'_, N> {
fn schema(_: &mut SchemaContext) -> Result<Value, GenerationError> {
Ok(json!({"k":"text","max":N}))
}
}