1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate alloc;
#[cfg(feature = "experimental-serializer")]
mod serializer;
mod value;
#[cfg(feature = "experimental-serializer")]
pub use serializer::{to_bytes, to_bytes_with_info, to_vec, to_vec_with_info, Serializer};
pub use value::Value;
use prelude::*;
use scale_info::{Field, MetaType, Type, Variant};
mod prelude {
pub use alloc::{
collections::BTreeMap,
string::{String, ToString},
vec::Vec,
};
}
macro_rules! is_tuple {
($it:ident) => {
$it.fields().first().and_then(Field::name).is_none()
};
}
#[rustfmt::skip]
#[derive(Debug, Clone)]
pub enum SerdeType {
Bool,
U8, U16, U32, U64, U128,
I8, I16, I32, I64, I128,
Bytes,
Char,
Str,
Sequence(Type),
Map(Type, Type),
Tuple(TupleOrArray),
Struct(Vec<Field>), StructUnit, StructNewType(Type), StructTuple(Vec<Field>),
Variant(String, Vec<Variant>, Option<u8>),
}
impl From<&mut Type> for SerdeType {
fn from(ty: &mut Type) -> Self {
ty.clone().into()
}
}
impl From<&Type> for SerdeType {
fn from(ty: &Type) -> Self {
ty.clone().into()
}
}
impl From<Type> for SerdeType {
fn from(ty: Type) -> Self {
use scale_info::{TypeDef, TypeDef::*, TypeDefComposite, TypeDefPrimitive};
let name = ty.path().segments().last().copied().unwrap_or("");
#[inline]
fn is_map(ty: &Type) -> bool {
ty.path().segments() == ["BTreeMap"]
}
fn map_types(ty: &TypeDefComposite) -> (Type, Type) {
let field = ty.fields().first().expect("map");
if let TypeDef::Sequence(s) = field.ty().type_info().type_def() {
if let TypeDef::Tuple(t) = s.type_param().type_info().type_def() {
assert_eq!(t.fields().len(), 2);
let key_ty = t.fields().first().expect("key").type_info();
let val_ty = t.fields().last().expect("val").type_info();
return (key_ty, val_ty);
}
}
unreachable!()
}
match ty.type_def() {
Composite(c) => {
let fields = c.fields();
if fields.is_empty() {
Self::StructUnit
} else if is_map(&ty) {
let (k, v) = map_types(c);
Self::Map(k, v)
} else if fields.len() == 1 {
Self::StructNewType(fields.first().unwrap().ty().type_info())
} else if is_tuple!(c) {
Self::StructTuple(fields.into())
} else {
Self::Struct(fields.into())
}
}
Variant(v) => Self::Variant(name.into(), v.variants().into(), None),
Sequence(s) => {
let ty = s.type_param().type_info();
if ty.path().segments() != ["u8"] {
Self::Sequence(ty)
} else {
Self::Bytes
}
}
Array(a) => Self::Tuple(TupleOrArray::Array(a.type_param().type_info(), a.len())),
Tuple(t) => Self::Tuple(TupleOrArray::Tuple(
t.fields().iter().map(MetaType::type_info).collect(),
)),
Primitive(p) => match p {
TypeDefPrimitive::U8 => Self::U8,
TypeDefPrimitive::U16 => Self::U16,
TypeDefPrimitive::U32 => Self::U32,
TypeDefPrimitive::U64 => Self::U64,
TypeDefPrimitive::U128 => Self::U128,
TypeDefPrimitive::I8 => Self::I8,
TypeDefPrimitive::I16 => Self::I16,
TypeDefPrimitive::I32 => Self::I32,
TypeDefPrimitive::I64 => Self::I64,
TypeDefPrimitive::I128 => Self::I128,
TypeDefPrimitive::Bool => Self::Bool,
TypeDefPrimitive::Str => Self::Str,
TypeDefPrimitive::Char => Self::Char,
TypeDefPrimitive::U256 => unimplemented!(),
TypeDefPrimitive::I256 => unimplemented!(),
},
Compact(_c) => todo!(),
BitSequence(_b) => todo!(),
}
}
}
impl SerdeType {
fn pick(&self, index: u8) -> Self {
match self {
SerdeType::Variant(name, variant, Some(_)) => {
Self::Variant(name.to_string(), variant.to_vec(), Some(index))
}
SerdeType::Variant(name, variants, None) => {
let v = variants.iter().find(|v| v.index() == index).unwrap();
Self::Variant(name.clone(), vec![v.clone()], Some(index))
}
_ => panic!("Only for enum variants"),
}
}
#[cfg(feature = "experimental-serializer")]
fn pick_mut<F, A, B>(&mut self, selection: A, get_field: F) -> &Self
where
F: Fn(&Variant) -> B,
A: AsRef<[u8]> + PartialEq + core::fmt::Debug,
B: AsRef<[u8]> + PartialEq + core::fmt::Debug,
{
match self {
SerdeType::Variant(_, _, Some(_)) => self,
SerdeType::Variant(_, ref mut variants, idx @ None) => {
let i = variants
.iter()
.map(|v| get_field(v))
.position(|f| f.as_ref() == selection.as_ref())
.expect("index") as u8;
variants.retain(|v| v.index() == i);
*idx = Some(i);
self
}
_ => panic!("Only for enum variants"),
}
}
#[cfg(feature = "experimental-serializer")]
fn variant_id(&self) -> u8 {
match self {
SerdeType::Variant(_, _, Some(id)) => *id,
_ => panic!("Only for enum variants"),
}
}
}
#[derive(Debug)]
enum EnumVariant<'a> {
OptionNone,
OptionSome(Type),
Unit(u8, &'a str),
NewType(u8, &'a str, Type),
Tuple(u8, &'a str, Vec<Type>),
Struct(u8, &'a str, Vec<(&'a str, Type)>),
}
impl<'a> From<&SerdeType> for EnumVariant<'a> {
fn from(ty: &SerdeType) -> Self {
match ty {
SerdeType::Variant(name, variants, Some(idx)) => {
let variant = variants.first().expect("single variant");
let fields = variant.fields();
let vname = *variant.name();
if fields.is_empty() {
if name == "Option" && vname == "None" {
Self::OptionNone
} else {
Self::Unit(*idx, vname)
}
} else if is_tuple!(variant) {
if fields.len() == 1 {
let ty = fields.first().map(|f| f.ty().type_info()).unwrap();
return if name == "Option" && variant.name() == &"Some" {
Self::OptionSome(ty)
} else {
Self::NewType(*idx, vname, ty)
};
} else {
let fields = fields.iter().map(|f| f.ty().type_info()).collect();
Self::Tuple(*idx, vname, fields)
}
} else {
let fields = fields
.iter()
.map(|f| (*f.name().unwrap(), f.ty().type_info()))
.collect();
Self::Struct(*idx, vname, fields)
}
}
_ => panic!("Only for enum variants"),
}
}
}
#[derive(Debug, Clone)]
pub enum TupleOrArray {
Array(Type, u32),
Tuple(Vec<Type>),
}
impl TupleOrArray {
fn len(&self) -> usize {
match self {
Self::Array(_, len) => *len as usize,
Self::Tuple(fields) => fields.len(),
}
}
fn type_info(&self, i: usize) -> &Type {
match self {
Self::Array(ty, _) => ty,
Self::Tuple(fields) => &fields[i],
}
}
}