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
use frame_support_procedural_tools::syn_ext as ext;
use proc_macro2::TokenStream;
use syn::{spanned::Spanned, parse_quote};
use quote::quote;
use super::super::{DeclStorageDefExt, StorageLineTypeDef};
pub struct GenesisConfigFieldDef {
pub name: syn::Ident,
pub typ: syn::Type,
pub attrs: Vec<syn::Meta>,
pub default: TokenStream,
}
pub struct GenesisConfigDef {
pub is_generic: bool,
pub fields: Vec<GenesisConfigFieldDef>,
pub genesis_struct_decl: TokenStream,
pub genesis_struct: TokenStream,
pub genesis_impl: TokenStream,
pub genesis_where_clause: Option<syn::WhereClause>,
}
impl GenesisConfigDef {
pub fn from_def(def: &DeclStorageDefExt) -> syn::Result<Self> {
let fields = Self::get_genesis_config_field_defs(def)?;
let is_generic = fields.iter()
.any(|field| ext::type_contains_ident(&field.typ, &def.module_runtime_generic));
let (
genesis_struct_decl,
genesis_impl,
genesis_struct,
genesis_where_clause
) = if is_generic {
let runtime_generic = &def.module_runtime_generic;
let runtime_trait = &def.module_runtime_trait;
let optional_instance = &def.optional_instance;
let optional_instance_bound = &def.optional_instance_bound;
let optional_instance_bound_optional_default = &def.optional_instance_bound_optional_default;
let where_clause = &def.where_clause;
(
quote!(<#runtime_generic: #runtime_trait, #optional_instance_bound_optional_default>),
quote!(<#runtime_generic: #runtime_trait, #optional_instance_bound>),
quote!(<#runtime_generic, #optional_instance>),
where_clause.clone(),
)
} else {
(quote!(), quote!(), quote!(), None)
};
Ok(Self {
is_generic,
fields,
genesis_struct_decl,
genesis_struct,
genesis_impl,
genesis_where_clause,
})
}
fn get_genesis_config_field_defs(def: &DeclStorageDefExt)
-> syn::Result<Vec<GenesisConfigFieldDef>>
{
let mut config_field_defs = Vec::new();
for (config_field, line) in def.storage_lines.iter()
.filter_map(|line| line.config.as_ref().map(|config_field| (config_field.clone(), line)))
{
let value_type = &line.value_type;
let typ = match &line.storage_type {
StorageLineTypeDef::Simple(_) => (*value_type).clone(),
StorageLineTypeDef::Map(map) => {
let key = &map.key;
parse_quote!( Vec<(#key, #value_type)> )
},
StorageLineTypeDef::DoubleMap(map) => {
let key1 = &map.key1;
let key2 = &map.key2;
parse_quote!( Vec<(#key1, #key2, #value_type)> )
},
};
let default = line.default_value.as_ref()
.map(|d| {
if line.is_option {
quote!( #d.unwrap_or_default() )
} else {
quote!( #d )
}
})
.unwrap_or_else(|| quote!( Default::default() ));
config_field_defs.push(GenesisConfigFieldDef {
name: config_field,
typ,
attrs: line.doc_attrs.clone(),
default,
});
}
for line in &def.extra_genesis_config_lines {
let attrs = line.attrs.iter()
.map(|attr| {
let meta = attr.parse_meta()?;
if meta.path().is_ident("cfg") {
return Err(syn::Error::new(
meta.span(),
"extra genesis config items do not support `cfg` attribute"
));
}
Ok(meta)
})
.collect::<syn::Result<_>>()?;
let default = line.default.as_ref().map(|e| quote!( #e ))
.unwrap_or_else(|| quote!( Default::default() ));
config_field_defs.push(GenesisConfigFieldDef {
name: line.name.clone(),
typ: line.typ.clone(),
attrs,
default,
});
}
Ok(config_field_defs)
}
}