use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
pub type TypeId = u32;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Registry(Vec<TypeDef>);
impl Registry {
pub fn new(types: Vec<TypeDef>) -> Self {
Self(types)
}
#[inline]
#[must_use]
pub fn resolve(&self, id: TypeId) -> Option<&TypeDef> {
self.0.get(id as usize)
}
}
#[rustfmt::skip]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TypeDef {
Bool,
U8, U16, U32, U64, U128,
I8, I16, I32, I64, I128,
Char,
Str,
Bytes,
Sequence(TypeId),
Map(TypeId, TypeId),
Array(TypeId, u32),
Tuple(Vec<TypeId>),
StructUnit,
StructNewType(TypeId),
StructTuple(Vec<TypeId>),
Struct(Vec<Field>),
Variant(VariantDef),
Compact(TypeId),
BitSequence(TypeId, TypeId),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Field {
pub name: String,
pub ty: TypeId,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariantDef {
pub name: String,
pub variants: Vec<Variant>,
}
impl VariantDef {
pub fn variant(&self, index: u8) -> Result<&Variant, crate::Error> {
self.variants
.iter()
.find(|v| v.index == index)
.ok_or(crate::Error::InvalidVariant(index))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Variant {
pub index: u8,
pub name: String,
pub fields: Fields,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Fields {
Unit,
NewType(TypeId),
Tuple(Vec<TypeId>),
Struct(Vec<Field>),
}