@@ -23,125 +23,135 @@ 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. 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- } ) ;
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+ } ) ;
5255
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 ! {
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 ! {
5868 #path:: __substrate_genesis_config_check:: is_genesis_config_defined!( #pallet_name) ;
5969 #[ cfg( feature = "std" ) ]
6070 #path:: __substrate_genesis_config_check:: is_std_enabled_for_genesis!( #pallet_name, #path_str) ;
6171 } ) ;
62- }
63- }
72+ }
73+ }
6474
65- quote ! {
66- #( #query_genesis_config_part_macros ) *
75+ quote ! {
76+ #( #query_genesis_config_part_macros ) *
6777
68- #types
78+ #types
6979
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- }
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+ }
7888
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- }
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+ }
91101
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- }
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+ }
98108
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- }
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+ }
110120}
111121
112122fn expand_config_types (
113- attr : & TokenStream ,
114- runtime : & Ident ,
115- decl : & Pallet ,
116- config : & Ident ,
117- part_is_generic : bool ,
123+ attr : & TokenStream ,
124+ runtime : & Ident ,
125+ decl : & Pallet ,
126+ config : & Ident ,
127+ part_is_generic : bool ,
118128) -> TokenStream {
119- let path = & decl. path ;
129+ let path = & decl. path ;
120130
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- }
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+ }
135145}
136146
137147fn expand_config_build_storage_call (
138- scrate : & TokenStream ,
139- pallet_genesis_config : & Ident ,
140- attr : & TokenStream ,
141- field_name : & Ident ,
148+ scrate : & TokenStream ,
149+ pallet_genesis_config : & Ident ,
150+ attr : & TokenStream ,
151+ field_name : & Ident ,
142152) -> TokenStream {
143- quote ! {
144- #attr
145- <#pallet_genesis_config as #scrate:: traits:: BuildGenesisConfig >:: build( & self . #field_name) ;
146- }
153+ quote ! {
154+ #attr
155+ <#pallet_genesis_config as #scrate:: traits:: BuildGenesisConfig >:: build( & self . #field_name) ;
156+ }
147157}
0 commit comments