scale_serialization/
registry.rs1use alloc::string::String;
2use alloc::vec::Vec;
3use serde::{Deserialize, Serialize};
4
5pub type TypeId = u32;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Registry(Vec<TypeDef>);
12
13impl Registry {
14 pub fn new(types: Vec<TypeDef>) -> Self {
16 Self(types)
17 }
18
19 #[inline]
21 #[must_use]
22 pub fn resolve(&self, id: TypeId) -> Option<&TypeDef> {
23 self.0.get(id as usize)
24 }
25}
26
27#[rustfmt::skip]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub enum TypeDef {
31 Bool,
32 U8, U16, U32, U64, U128,
33 I8, I16, I32, I64, I128,
34 Char,
35 Str,
36 Bytes,
38 Sequence(TypeId),
40 Map(TypeId, TypeId),
42 Array(TypeId, u32),
44 Tuple(Vec<TypeId>),
46 StructUnit,
48 StructNewType(TypeId),
50 StructTuple(Vec<TypeId>),
52 Struct(Vec<Field>),
54 Variant(VariantDef),
56 Compact(TypeId),
58 BitSequence(TypeId, TypeId),
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct Field {
65 pub name: String,
66 pub ty: TypeId,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct VariantDef {
72 pub name: String,
74 pub variants: Vec<Variant>,
75}
76
77impl VariantDef {
78 pub fn variant(&self, index: u8) -> Result<&Variant, crate::Error> {
80 self.variants
81 .iter()
82 .find(|v| v.index == index)
83 .ok_or(crate::Error::InvalidVariant(index))
84 }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct Variant {
90 pub index: u8,
91 pub name: String,
92 pub fields: Fields,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
97pub enum Fields {
98 Unit,
99 NewType(TypeId),
100 Tuple(Vec<TypeId>),
101 Struct(Vec<Field>),
102}