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
use super::super::{DeclStorageDefExt, StorageLineTypeDef};
use frame_support_procedural_tools::syn_ext as ext;
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
pub struct BuilderDef {
pub blocks: Vec<TokenStream>,
pub is_generic: bool,
}
impl BuilderDef {
pub fn from_def(scrate: &TokenStream, def: &DeclStorageDefExt) -> Self {
let mut blocks = Vec::new();
let mut is_generic = false;
for line in def.storage_lines.iter() {
let storage_struct = &line.storage_struct;
let storage_trait = &line.storage_trait;
let value_type = &line.value_type;
let mut data = None;
if let Some(builder) = &line.build {
is_generic |= ext::expr_contains_ident(&builder, &def.module_runtime_generic);
is_generic |= line.is_generic;
data = Some(match &line.storage_type {
StorageLineTypeDef::Simple(_) if line.is_option => {
quote_spanned!(builder.span() =>
let builder: fn(&Self) -> _ = #builder;
let data = builder(self);
let data = Option::as_ref(&data);
)
},
_ => quote_spanned!(builder.span() =>
let builder: fn(&Self) -> _ = #builder;
let data = &builder(self);
),
});
} else if let Some(config) = &line.config {
is_generic |= line.is_generic;
data = Some(match &line.storage_type {
StorageLineTypeDef::Simple(_) if line.is_option => {
quote!( let data = Some(&self.#config); )
},
_ => quote!( let data = &self.#config; ),
});
};
if let Some(data) = data {
blocks.push(match &line.storage_type {
StorageLineTypeDef::Simple(_) if line.is_option => {
quote! {{
#data
let v: Option<&#value_type>= data;
if let Some(v) = v {
<#storage_struct as #scrate::#storage_trait>::put::<&#value_type>(v);
}
}}
},
StorageLineTypeDef::Simple(_) if !line.is_option => {
quote! {{
#data
let v: &#value_type = data;
<#storage_struct as #scrate::#storage_trait>::put::<&#value_type>(v);
}}
},
StorageLineTypeDef::Simple(_) => unreachable!(),
StorageLineTypeDef::Map(map) => {
let key = &map.key;
quote! {{
#data
let data: &#scrate::sp_std::vec::Vec<(#key, #value_type)> = data;
data.iter().for_each(|(k, v)| {
<#storage_struct as #scrate::#storage_trait>::insert::<
&#key, &#value_type
>(k, v);
});
}}
},
StorageLineTypeDef::DoubleMap(map) => {
let key1 = &map.key1;
let key2 = &map.key2;
quote! {{
#data
let data: &#scrate::sp_std::vec::Vec<(#key1, #key2, #value_type)> = data;
data.iter().for_each(|(k1, k2, v)| {
<#storage_struct as #scrate::#storage_trait>::insert::<
&#key1, &#key2, &#value_type
>(k1, k2, v);
});
}}
},
StorageLineTypeDef::NMap(map) => {
let key_tuple = map.to_key_tuple();
let key_arg = if map.keys.len() == 1 { quote!((k,)) } else { quote!(k) };
quote! {{
#data
let data: &#scrate::sp_std::vec::Vec<(#key_tuple, #value_type)> = data;
data.iter().for_each(|(k, v)| {
<#storage_struct as #scrate::#storage_trait>::insert(#key_arg, v);
});
}}
},
});
}
}
if let Some(builder) = def.extra_genesis_build.as_ref() {
is_generic |= ext::expr_contains_ident(&builder, &def.module_runtime_generic);
blocks.push(quote_spanned! { builder.span() =>
let extra_genesis_builder: fn(&Self) = #builder;
extra_genesis_builder(self);
});
}
Self { blocks, is_generic }
}
}