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
use scale_info::{Registry, PortableRegistry, TypeDef};
use crate::{ABIType, PrimitiveType, TypeEnum};
use scale_info::form::PortableForm;
use crate::TypeInfo as ABITypeInfo;
pub fn generate_abi_type(reg: Registry) -> Vec<ABIType<PortableForm>> {
let reg_portable: PortableRegistry = reg.into();
reg_portable
.types()
.into_iter()
.map(|tp| {
// construct a ABIType
match tp.ty().type_def() {
TypeDef::Composite(comp) => {
let f = comp
.fields()
.into_iter()
.map(|field| {
ABITypeInfo {
type_id: *field.ty(),
}
})
.collect::<Vec<_>>();
ABIType {
type_id: tp.id(),
tp: TypeEnum::Struct,
fields: Some(f),
primitive: None,
array_len: None,
variants: None,
}
}
TypeDef::Primitive(pri) => {
ABIType {
type_id: tp.id(),
tp: TypeEnum::Primitive,
fields: None,
primitive: Some(PrimitiveType::to_primitive_type(pri).to_string()),
array_len: None,
variants: None,
}
}
TypeDef::Variant(items) => {
if items.variants().len() > 256 {
panic!("items of Enum have exceed 256");
}
let variant_types = items.variants().into_iter().map(|x| {
if x.name() != "None" && x.fields().len() == 0 {
panic!("Enum should point out item type");
}
x.fields().into_iter().map(|x| {
ABITypeInfo {
type_id: *x.ty()
}
}).collect::<Vec<_>>()
}).collect::<Vec<_>>();
ABIType {
type_id: tp.id(),
tp: TypeEnum::Enum,
fields: None,
primitive: None,
array_len: None,
variants: Some(variant_types),
}
}
TypeDef::Sequence(seq) => {
let other_info = ABITypeInfo {
type_id: *seq.type_param(),
};
ABIType {
type_id: tp.id(),
tp: TypeEnum::Vec,
fields: Some(vec![other_info]),
primitive: None,
array_len: None,
variants: None,
}
}
TypeDef::Array(arr) => {
let other_info = ABITypeInfo {
type_id: *arr.type_param(),
};
ABIType {
type_id: tp.id(),
tp: TypeEnum::Array,
fields: Some(vec![other_info]),
primitive: None,
array_len: Some(arr.len()),
variants: None,
}
}
TypeDef::Tuple(items) => {
let f = items.fields()
.into_iter()
.map(|field| {
ABITypeInfo {
type_id: *field,
}
})
.collect();
ABIType {
type_id: tp.id(),
tp: TypeEnum::Tuple,
fields: Some(f),
primitive: None,
array_len: None,
variants: None,
}
}
TypeDef::Compact(_) => {
panic!("not support compact type for ABI now")
}
TypeDef::BitSequence(_) => {
panic!("not support BitSequence type for ABI now")
}
}
})
.collect::<Vec<_>>()
}