@@ -21,124 +21,7 @@ use macro_magic::core::pretty_print;
21
21
use proc_macro2:: TokenStream as TokenStream2 ;
22
22
use quote:: { quote, ToTokens } ;
23
23
use std:: collections:: HashSet ;
24
- use syn:: {
25
- braced, bracketed,
26
- parse:: { Parse , ParseStream } ,
27
- parse2, parse_quote,
28
- punctuated:: Punctuated ,
29
- token:: { Brace , Bracket } ,
30
- Ident , ImplItem , ItemImpl , Path , Result , Token , TypePath ,
31
- } ;
32
-
33
- mod keywords {
34
- use syn:: custom_keyword;
35
-
36
- custom_keyword ! ( derive_impl) ;
37
- custom_keyword ! ( partial_impl_block) ;
38
- custom_keyword ! ( implementing_type) ;
39
- custom_keyword ! ( type_items) ;
40
- custom_keyword ! ( fn_items) ;
41
- custom_keyword ! ( const_items) ;
42
- }
43
-
44
- pub struct DeriveImplDef {
45
- /// The partial impl block that the user provides. This should be interpreted as "override".
46
- partial_impl_block : ItemImpl ,
47
- /// The full path to the type that can be used to receive defaults form.
48
- implementing_type : TypePath ,
49
- /// All of the associated type items that we must eventually implement.
50
- type_items : Punctuated < Ident , Token ! [ , ] > ,
51
- /// All of the function items that we must eventually implement.
52
- fn_items : Punctuated < Ident , Token ! [ , ] > ,
53
- /// All of the constant items that we must eventually implement.
54
- const_items : Punctuated < Ident , Token ! [ , ] > ,
55
- }
56
-
57
- impl Parse for DeriveImplDef {
58
- fn parse ( input : ParseStream ) -> Result < Self > {
59
- // NOTE: unfortunately, the order the keywords here must match what the pallet macro
60
- // expands. We can probably used a shared set of keywords later.
61
- let mut partial_impl_block;
62
- let _ = input. parse :: < keywords:: partial_impl_block > ( ) ?;
63
- let _ = input. parse :: < Token ! [ =] > ( ) ?;
64
- let _replace_with_bracket: Bracket = bracketed ! ( partial_impl_block in input) ;
65
- let _replace_with_brace: Brace = braced ! ( partial_impl_block in partial_impl_block) ;
66
- let partial_impl_block = partial_impl_block. parse ( ) ?;
67
-
68
- let mut implementing_type;
69
- let _ = input. parse :: < keywords:: implementing_type > ( ) ?;
70
- let _ = input. parse :: < Token ! [ =] > ( ) ?;
71
- let _replace_with_bracket: Bracket = bracketed ! ( implementing_type in input) ;
72
- let _replace_with_brace: Brace = braced ! ( implementing_type in implementing_type) ;
73
- let implementing_type = implementing_type. parse ( ) ?;
74
-
75
- let mut type_items;
76
- let _ = input. parse :: < keywords:: type_items > ( ) ?;
77
- let _ = input. parse :: < Token ! [ =] > ( ) ?;
78
- let _replace_with_bracket: Bracket = bracketed ! ( type_items in input) ;
79
- let _replace_with_brace: Brace = braced ! ( type_items in type_items) ;
80
- let type_items = Punctuated :: < Ident , Token ! [ , ] > :: parse_terminated ( & type_items) ?;
81
-
82
- let mut fn_items;
83
- let _ = input. parse :: < keywords:: fn_items > ( ) ?;
84
- let _ = input. parse :: < Token ! [ =] > ( ) ?;
85
- let _replace_with_bracket: Bracket = bracketed ! ( fn_items in input) ;
86
- let _replace_with_brace: Brace = braced ! ( fn_items in fn_items) ;
87
- let fn_items = Punctuated :: < Ident , Token ! [ , ] > :: parse_terminated ( & fn_items) ?;
88
-
89
- let mut const_items;
90
- let _ = input. parse :: < keywords:: const_items > ( ) ?;
91
- let _ = input. parse :: < Token ! [ =] > ( ) ?;
92
- let _replace_with_bracket: Bracket = bracketed ! ( const_items in input) ;
93
- let _replace_with_brace: Brace = braced ! ( const_items in const_items) ;
94
- let const_items = Punctuated :: < Ident , Token ! [ , ] > :: parse_terminated ( & const_items) ?;
95
-
96
- Ok ( Self { partial_impl_block, type_items, fn_items, const_items, implementing_type } )
97
- }
98
- }
99
-
100
- pub ( crate ) fn derive_impl_inner ( input : TokenStream2 ) -> Result < TokenStream2 > {
101
- println ! ( "input: {}" , input) ;
102
- let DeriveImplDef { partial_impl_block, implementing_type, type_items, .. } = parse2 ( input) ?;
103
-
104
- let type_item_name = |i : & ImplItem | {
105
- if let ImplItem :: Type ( t) = i {
106
- t. ident . clone ( )
107
- } else {
108
- panic ! ( "only support type items for now" )
109
- }
110
- } ;
111
-
112
- // might be able to mutate `partial_impl_block` along the way, but easier like this for now.
113
- let mut final_impl_block = partial_impl_block. clone ( ) ;
114
- let source_crate_path = implementing_type. path . segments . first ( ) . unwrap ( ) . ident . clone ( ) ;
115
-
116
- // TODO: ensure type ident specified in `partial_impl_block` is beyond union(type_items,
117
- // const_items, fn_items).
118
- assert ! (
119
- partial_impl_block
120
- . items
121
- . iter( )
122
- . all( |i| { type_items. iter( ) . find( |tt| tt == &&type_item_name( i) ) . is_some( ) } ) ,
123
- "some item in the partial_impl_block is unexpected"
124
- ) ;
125
-
126
- // for each item that is in `type_items` but not present in `partial_impl_block`, fill it in.
127
- type_items. iter ( ) . for_each ( |ident| {
128
- if partial_impl_block. items . iter ( ) . any ( |i| & type_item_name ( i) == ident) {
129
- // this is already present in the partial impl block -- noop
130
- } else {
131
- // add it
132
- let tokens = quote:: quote!( type #ident = <#implementing_type as #source_crate_path:: pallet:: DefaultConfig >:: #ident; ) ;
133
- let parsed: ImplItem = parse2 ( tokens) . expect ( "it is a valid type item" ) ;
134
- debug_assert ! ( matches!( parsed, ImplItem :: Type ( _) ) ) ;
135
-
136
- final_impl_block. items . push ( parsed)
137
- }
138
- } ) ;
139
-
140
- Ok ( quote:: quote!( #final_impl_block) )
141
- }
24
+ use syn:: { parse2, parse_quote, Ident , ImplItem , ItemImpl , Path , Result } ;
142
25
143
26
fn impl_item_ident ( impl_item : & ImplItem ) -> Option < Ident > {
144
27
match impl_item {
@@ -161,6 +44,8 @@ fn combine_impls(local_impl: ItemImpl, foreign_impl: ItemImpl, foreign_path: Pat
161
44
. filter ( |impl_item| impl_item_ident ( impl_item) . is_none ( ) )
162
45
. cloned ( )
163
46
. collect ( ) ;
47
+ // // TODO: may not be accurate.
48
+ // // source_crate_path = frame_system
164
49
let source_crate_path = foreign_path. segments . first ( ) . unwrap ( ) . ident . clone ( ) ;
165
50
let mut final_impl = local_impl;
166
51
final_impl. items . extend (
@@ -220,41 +105,4 @@ pub fn derive_impl(
220
105
pretty_print ( & combined_impl. to_token_stream ( ) ) ;
221
106
222
107
Ok ( quote ! ( #combined_impl) )
223
- // attr: frame_system::preludes::testing::Impl
224
- // tokens:
225
- // impl frame_system::Config for Test {
226
- // // These are all defined by system as mandatory.
227
- // type BaseCallFilter = frame_support::traits::Everything;
228
- // type RuntimeEvent = RuntimeEvent;
229
- // type RuntimeCall = RuntimeCall;
230
- // type RuntimeOrigin = RuntimeOrigin;
231
- // type OnSetCode = frame_system::DefaultSetCode<Self>;
232
- // type PalletInfo = PalletInfo;
233
- // type Header = Header;
234
- // // We decide to override this one.
235
- // type AccountData = pallet_balances::AccountData<u64>;
236
- // }
237
- // let implementing_type: TypePath = parse2(attrs.clone())?;
238
- // // ideas for sam:
239
- // // let other_path_tokens = magic_macro!(path_to_other_path_token);
240
- // // let foreign_trait_tokens = import_tokens_indirect!(frame_system::testing::DefaultConfig);
241
- // // println!("{}", foreign_trait_tokens.to_string());
242
-
243
- // let frame_support = generate_crate_access_2018("frame-support")?;
244
- // // TODO: may not be accurate.
245
- // let source_crate_path = implementing_type.path.segments.first().unwrap().ident.clone();
246
- // // source_crate_path = frame_system
247
-
248
- // //let tokens = import_tokens_indirect!(::pallet_example_basic::pallet::Config);
249
-
250
- // Ok(quote::quote!(
251
- // #frame_support::tt_call! {
252
- // macro = [{ #source_crate_path::tt_config_items }] // frame_system::tt_config_items
253
- // frame_support = [{ #frame_support }] // ::frame_support
254
- // ~~> #frame_support::derive_impl_inner! {
255
- // partial_impl_block = [{ #input }]
256
- // implementing_type = [{ #attrs }]
257
- // }
258
- // }
259
- // ))
260
108
}
0 commit comments