@@ -23,135 +23,125 @@ use std::str::FromStr;
2323use syn:: Ident ;
2424
2525pub fn expand_outer_config (
26- runtime : & Ident ,
27- pallet_decls : & [ Pallet ] ,
28- scrate : & TokenStream ,
26+ runtime : & Ident ,
27+ pallet_decls : & [ Pallet ] ,
28+ scrate : & TokenStream ,
2929) -> TokenStream {
30- let mut types = TokenStream :: new ( ) ;
31- let mut fields = TokenStream :: new ( ) ;
32- let mut genesis_build_calls = TokenStream :: new ( ) ;
33- let mut query_genesis_config_part_macros = Vec :: new ( ) ;
30+ let mut types = TokenStream :: new ( ) ;
31+ let mut fields = TokenStream :: new ( ) ;
32+ let mut genesis_build_calls = TokenStream :: new ( ) ;
33+ let mut query_genesis_config_part_macros = Vec :: new ( ) ;
3434
35- for decl in pallet_decls {
36- if let Some ( pallet_entry) = decl. find_part ( "Config" ) {
37- let path = & decl. path ;
38- let pallet_name = & decl. name ;
39- let path_str = path. into_token_stream ( ) . to_string ( ) ;
40- let config = format_ident ! ( "{}Config" , pallet_name) ;
41- let field_name =
42- & Ident :: new ( & pallet_name. to_string ( ) . to_snake_case ( ) , decl. name . span ( ) ) ;
43- let part_is_generic = !pallet_entry. generics . params . is_empty ( ) ;
44- let attr = & decl
45- . cfg_pattern
46- . iter ( )
47- . fold ( TokenStream :: new ( ) , |acc, pattern| {
48- let attr = TokenStream :: from_str ( & format ! ( "#[cfg({})]" , pattern. original( ) ) )
49- . expect ( "was successfully parsed before; qed" ) ;
50- quote ! {
51- #acc
52- #attr
53- }
54- } ) ;
35+ for decl in pallet_decls {
36+ if let Some ( pallet_entry) = decl. find_part ( "Config" ) {
37+ let path = & decl. path ;
38+ let pallet_name = & decl. name ;
39+ let path_str = path. into_token_stream ( ) . to_string ( ) ;
40+ let config = format_ident ! ( "{}Config" , pallet_name) ;
41+ let field_name =
42+ & Ident :: new ( & pallet_name. to_string ( ) . to_snake_case ( ) , decl. name . span ( ) ) ;
43+ let part_is_generic = !pallet_entry. generics . params . is_empty ( ) ;
44+ let attr = & decl. cfg_pattern . iter ( ) . fold ( TokenStream :: new ( ) , |acc, pattern| {
45+ let attr = TokenStream :: from_str ( & format ! ( "#[cfg({})]" , pattern. original( ) ) )
46+ . expect ( "was successfully parsed before; qed" ) ;
47+ quote ! {
48+ #acc
49+ #attr
50+ }
51+ } ) ;
5552
56- types. extend ( expand_config_types (
57- attr,
58- runtime,
59- decl,
60- & config,
61- part_is_generic,
62- ) ) ;
63- fields. extend ( quote ! ( #attr pub #field_name: #config, ) ) ;
64- genesis_build_calls. extend ( expand_config_build_storage_call (
65- scrate, & config, attr, field_name,
66- ) ) ;
67- query_genesis_config_part_macros. push ( quote ! {
53+ types. extend ( expand_config_types ( attr, runtime, decl, & config, part_is_generic) ) ;
54+ fields. extend ( quote ! ( #attr pub #field_name: #config, ) ) ;
55+ genesis_build_calls
56+ . extend ( expand_config_build_storage_call ( scrate, & config, attr, field_name) ) ;
57+ query_genesis_config_part_macros. push ( quote ! {
6858 #path:: __substrate_genesis_config_check:: is_genesis_config_defined!( #pallet_name) ;
6959 #[ cfg( feature = "std" ) ]
7060 #path:: __substrate_genesis_config_check:: is_std_enabled_for_genesis!( #pallet_name, #path_str) ;
7161 } ) ;
72- }
73- }
62+ }
63+ }
7464
75- quote ! {
76- #( #query_genesis_config_part_macros ) *
65+ quote ! {
66+ #( #query_genesis_config_part_macros ) *
7767
78- #types
68+ #types
7969
80- use #scrate:: __private:: serde as __genesis_config_serde_import__;
81- #[ derive( #scrate:: __private:: serde:: Serialize , #scrate:: __private:: serde:: Deserialize , Default ) ]
82- #[ serde( rename_all = "camelCase" ) ]
83- #[ serde( deny_unknown_fields) ]
84- #[ serde( crate = "__genesis_config_serde_import__" ) ]
85- pub struct RuntimeGenesisConfig {
86- #fields
87- }
70+ use #scrate:: __private:: serde as __genesis_config_serde_import__;
71+ #[ derive( #scrate:: __private:: serde:: Serialize , #scrate:: __private:: serde:: Deserialize , Default ) ]
72+ #[ serde( rename_all = "camelCase" ) ]
73+ #[ serde( deny_unknown_fields) ]
74+ #[ serde( crate = "__genesis_config_serde_import__" ) ]
75+ pub struct RuntimeGenesisConfig {
76+ #fields
77+ }
8878
89- #[ cfg( any( feature = "std" , test) ) ]
90- impl #scrate:: sp_runtime:: BuildStorage for RuntimeGenesisConfig {
91- fn assimilate_storage(
92- & self ,
93- storage: & mut #scrate:: sp_runtime:: Storage ,
94- ) -> std:: result:: Result <( ) , String > {
95- #scrate:: __private:: BasicExternalities :: execute_with_storage( storage, || {
96- <Self as #scrate:: traits:: BuildGenesisConfig >:: build( & self ) ;
97- Ok ( ( ) )
98- } )
99- }
100- }
79+ #[ cfg( any( feature = "std" , test) ) ]
80+ impl #scrate:: sp_runtime:: BuildStorage for RuntimeGenesisConfig {
81+ fn assimilate_storage(
82+ & self ,
83+ storage: & mut #scrate:: sp_runtime:: Storage ,
84+ ) -> std:: result:: Result <( ) , String > {
85+ #scrate:: __private:: BasicExternalities :: execute_with_storage( storage, || {
86+ <Self as #scrate:: traits:: BuildGenesisConfig >:: build( & self ) ;
87+ Ok ( ( ) )
88+ } )
89+ }
90+ }
10191
102- impl #scrate:: traits:: BuildGenesisConfig for RuntimeGenesisConfig {
103- fn build( & self ) {
104- #genesis_build_calls
105- <AllPalletsWithSystem as #scrate:: traits:: OnGenesis >:: on_genesis( ) ;
106- }
107- }
92+ impl #scrate:: traits:: BuildGenesisConfig for RuntimeGenesisConfig {
93+ fn build( & self ) {
94+ #genesis_build_calls
95+ <AllPalletsWithSystem as #scrate:: traits:: OnGenesis >:: on_genesis( ) ;
96+ }
97+ }
10898
109- /// Test the `Default` derive impl of the `RuntimeGenesisConfig`.
110- #[ cfg( test) ]
111- #[ test]
112- fn test_genesis_config_builds( ) {
113- #scrate:: __private:: sp_io:: TestExternalities :: default ( ) . execute_with( || {
114- <RuntimeGenesisConfig as #scrate:: traits:: BuildGenesisConfig >:: build(
115- & RuntimeGenesisConfig :: default ( )
116- ) ;
117- } ) ;
118- }
119- }
99+ /// Test the `Default` derive impl of the `RuntimeGenesisConfig`.
100+ #[ cfg( test) ]
101+ #[ test]
102+ fn test_genesis_config_builds( ) {
103+ #scrate:: __private:: sp_io:: TestExternalities :: default ( ) . execute_with( || {
104+ <RuntimeGenesisConfig as #scrate:: traits:: BuildGenesisConfig >:: build(
105+ & RuntimeGenesisConfig :: default ( )
106+ ) ;
107+ } ) ;
108+ }
109+ }
120110}
121111
122112fn expand_config_types (
123- attr : & TokenStream ,
124- runtime : & Ident ,
125- decl : & Pallet ,
126- config : & Ident ,
127- part_is_generic : bool ,
113+ attr : & TokenStream ,
114+ runtime : & Ident ,
115+ decl : & Pallet ,
116+ config : & Ident ,
117+ part_is_generic : bool ,
128118) -> TokenStream {
129- let path = & decl. path ;
119+ let path = & decl. path ;
130120
131- match ( decl. instance . as_ref ( ) , part_is_generic) {
132- ( Some ( inst) , true ) => quote ! {
133- #attr
134- pub type #config = #path:: GenesisConfig <#runtime, #path:: #inst>;
135- } ,
136- ( None , true ) => quote ! {
137- #attr
138- pub type #config = #path:: GenesisConfig <#runtime>;
139- } ,
140- ( _, false ) => quote ! {
141- #attr
142- pub type #config = #path:: GenesisConfig ;
143- } ,
144- }
121+ match ( decl. instance . as_ref ( ) , part_is_generic) {
122+ ( Some ( inst) , true ) => quote ! {
123+ #attr
124+ pub type #config = #path:: GenesisConfig <#runtime, #path:: #inst>;
125+ } ,
126+ ( None , true ) => quote ! {
127+ #attr
128+ pub type #config = #path:: GenesisConfig <#runtime>;
129+ } ,
130+ ( _, false ) => quote ! {
131+ #attr
132+ pub type #config = #path:: GenesisConfig ;
133+ } ,
134+ }
145135}
146136
147137fn expand_config_build_storage_call (
148- scrate : & TokenStream ,
149- pallet_genesis_config : & Ident ,
150- attr : & TokenStream ,
151- field_name : & Ident ,
138+ scrate : & TokenStream ,
139+ pallet_genesis_config : & Ident ,
140+ attr : & TokenStream ,
141+ field_name : & Ident ,
152142) -> TokenStream {
153- quote ! {
154- #attr
155- <#pallet_genesis_config as #scrate:: traits:: BuildGenesisConfig >:: build( & self . #field_name) ;
156- }
143+ quote ! {
144+ #attr
145+ <#pallet_genesis_config as #scrate:: traits:: BuildGenesisConfig >:: build( & self . #field_name) ;
146+ }
157147}
0 commit comments