@@ -182,6 +182,29 @@ fn rust_type(input: &ParamType) -> syn::Ident {
182
182
}
183
183
}
184
184
185
+ fn template_param_type ( input : & ParamType , index : usize ) -> syn:: Ident {
186
+ match * input {
187
+ ParamType :: Address => format ! ( "T{}: Into<ethabi::Address>" , index) . into ( ) ,
188
+ ParamType :: Bytes => format ! ( "T{}: Into<ethabi::Bytes>" , index) . into ( ) ,
189
+ ParamType :: FixedBytes ( 32 ) => format ! ( "T{}: Into<ethabi::Hash>" , index) . into ( ) ,
190
+ ParamType :: FixedBytes ( size) => format ! ( "T{}: Into<[u8; {}]>" , index, size) . into ( ) ,
191
+ ParamType :: Int ( _) => format ! ( "T{}: Into<ethabi::Int>" , index) . into ( ) ,
192
+ ParamType :: Uint ( _) => format ! ( "T{}: Into<ethabi::Uint>" , index) . into ( ) ,
193
+ ParamType :: Bool => format ! ( "T{}: Into<bool>" , index) . into ( ) ,
194
+ ParamType :: String => format ! ( "T{}: Into<String>" , index) . into ( ) ,
195
+ ParamType :: Array ( ref kind) => format ! ( "T{}: IntoIterator<Item = U{}>, U{}: Into<{}>" , index, index, index, rust_type( & * kind) ) . into ( ) ,
196
+ ParamType :: FixedArray ( ref kind, size) => format ! ( "T{}: Into<[U{}; {}]>, U{}: Into<{}>" , index, index, size, index, rust_type( & * kind) ) . into ( ) ,
197
+ }
198
+ }
199
+
200
+ fn from_template_param ( input : & ParamType , name : & syn:: Ident ) -> syn:: Ident {
201
+ match * input {
202
+ ParamType :: Array ( _) => format ! ( "{}.into_iter().map(Into::into).collect::<Vec<_>>()" , name) . into ( ) ,
203
+ ParamType :: FixedArray ( _, _) => format ! ( "(Box::new({}.into()) as Box<[_]>).into_vec().into_iter().map(Into::into).collect::<Vec<_>>()" , name) . into ( ) ,
204
+ _ => format ! ( "{}.into()" , name) . into ( ) ,
205
+ }
206
+ }
207
+
185
208
fn to_token ( name : & syn:: Ident , kind : & ParamType ) -> quote:: Tokens {
186
209
match * kind {
187
210
ParamType :: Address => quote ! { ethabi:: Token :: Address ( #name) } ,
@@ -208,7 +231,7 @@ fn to_token(name: &syn::Ident, kind: &ParamType) -> quote::Tokens {
208
231
quote ! {
209
232
// note the double {{
210
233
{
211
- let v = #name. to_vec ( ) . into_iter( ) . map( |#inner_name| #inner_loop) . collect( ) ;
234
+ let v = #name. into_iter( ) . map( |#inner_name| #inner_loop) . collect( ) ;
212
235
ethabi:: Token :: FixedArray ( v)
213
236
}
214
237
}
@@ -271,6 +294,7 @@ fn impl_contract_event(event: &Event) -> quote::Tokens {
271
294
}
272
295
273
296
fn impl_contract_constructor ( constructor : & Constructor ) -> quote:: Tokens {
297
+ // [param0, hello_world, param2]
274
298
let names: Vec < _ > = constructor. inputs
275
299
. iter ( )
276
300
. enumerate ( )
@@ -279,21 +303,36 @@ fn impl_contract_constructor(constructor: &Constructor) -> quote::Tokens {
279
303
} else {
280
304
param. name . to_snake_case ( ) . into ( )
281
305
} ) . collect ( ) ;
306
+
307
+ // [Uint, Bytes, Vec<Uint>]
282
308
let kinds: Vec < _ > = constructor. inputs
283
309
. iter ( )
284
310
. map ( |param| rust_type ( & param. kind ) )
285
311
. collect ( ) ;
312
+
313
+ // [T0, T1, T2]
286
314
let template_names: Vec < _ > = kinds. iter ( ) . enumerate ( )
287
315
. map ( |( index, _) | syn:: Ident :: new ( format ! ( "T{}" , index) ) )
288
316
. collect ( ) ;
289
- let template_params: Vec < _ > = kinds. iter ( ) . zip ( template_names. iter ( ) )
290
- . map ( |( kind, template_name) | quote ! { #template_name: Into <#kind> } )
317
+
318
+ // [T0: Into<Uint>, T1: Into<Bytes>, T2: Into<Vec<Uint>>]
319
+ //let template_params: Vec<_> = kinds.iter().zip(template_names.iter())
320
+ //.map(|(kind, template_name)| quote! { #template_name: Into<#kind> })
321
+ //.collect();
322
+
323
+ // [T0: Into<Uint>, T1: Into<Bytes>, T2: IntoIterator<Item = U2>, U2 = Into<Uint>]
324
+ let template_params: Vec < _ > = constructor. inputs . iter ( ) . enumerate ( )
325
+ . map ( |( index, param) | template_param_type ( & param. kind , index) )
291
326
. collect ( ) ;
327
+
328
+ // [param0: T0, hello_world: T1, param2: T2]
292
329
let params: Vec < _ > = names. iter ( ) . zip ( template_names. iter ( ) )
293
330
. map ( |( param_name, template_name) | quote ! { #param_name: #template_name } )
294
331
. collect ( ) ;
332
+
333
+ // [Token::Uint(param0.into()), Token::Bytes(hello_world.into()), Token::Array(param2.into())]
295
334
let usage: Vec < _ > = names. iter ( ) . zip ( constructor. inputs . iter ( ) )
296
- . map ( |( param_name, param) | to_token ( & format ! ( "{}.into()" , param_name) . into ( ) , & param. kind ) )
335
+ . map ( |( param_name, param) | to_token ( & from_template_param ( & param . kind , param_name) , & param. kind ) )
297
336
. collect ( ) ;
298
337
299
338
quote ! {
@@ -381,6 +420,7 @@ fn declare_events(event: &Event) -> quote::Tokens {
381
420
. map ( |param| rust_type ( & param. kind ) )
382
421
. collect ( ) ;
383
422
423
+ // [T0, T1, T2]
384
424
let template_names: Vec < _ > = topic_kinds. iter ( ) . enumerate ( )
385
425
. map ( |( index, _) | syn:: Ident :: new ( format ! ( "T{}" , index) ) )
386
426
. collect ( ) ;
@@ -440,6 +480,8 @@ fn declare_events(event: &Event) -> quote::Tokens {
440
480
441
481
fn declare_functions ( function : & Function ) -> quote:: Tokens {
442
482
let name = syn:: Ident :: new ( function. name . to_camel_case ( ) ) ;
483
+
484
+ // [param0, hello_world, param2]
443
485
let names: Vec < _ > = function. inputs
444
486
. iter ( )
445
487
. enumerate ( )
@@ -448,21 +490,31 @@ fn declare_functions(function: &Function) -> quote::Tokens {
448
490
} else {
449
491
param. name . to_snake_case ( ) . into ( )
450
492
} ) . collect ( ) ;
493
+
494
+ // [Uint, Bytes, Vec<Uint>]
451
495
let kinds: Vec < _ > = function. inputs
452
496
. iter ( )
453
497
. map ( |param| rust_type ( & param. kind ) )
454
498
. collect ( ) ;
499
+
500
+ // [T0, T1, T2]
455
501
let template_names: Vec < _ > = kinds. iter ( ) . enumerate ( )
456
502
. map ( |( index, _) | syn:: Ident :: new ( format ! ( "T{}" , index) ) )
457
503
. collect ( ) ;
458
- let template_params: Vec < _ > = kinds. iter ( ) . zip ( template_names. iter ( ) )
459
- . map ( |( kind, template_name) | quote ! { #template_name: Into <#kind> } )
504
+
505
+ // [T0: Into<Uint>, T1: Into<Bytes>, T2: IntoIterator<Item = U2>, U2 = Into<Uint>]
506
+ let template_params: Vec < _ > = function. inputs . iter ( ) . enumerate ( )
507
+ . map ( |( index, param) | template_param_type ( & param. kind , index) )
460
508
. collect ( ) ;
509
+
510
+ // [param0: T0, hello_world: T1, param2: T2]
461
511
let params: Vec < _ > = names. iter ( ) . zip ( template_names. iter ( ) )
462
512
. map ( |( param_name, template_name) | quote ! { #param_name: #template_name } )
463
513
. collect ( ) ;
514
+
515
+ // [Token::Uint(param0.into()), Token::Bytes(hello_world.into()), Token::Array(param2.into_iter().map(Into::into).collect())]
464
516
let usage: Vec < _ > = names. iter ( ) . zip ( function. inputs . iter ( ) )
465
- . map ( |( param_name, param) | to_token ( & format ! ( "{}.into()" , param_name) . into ( ) , & param. kind ) )
517
+ . map ( |( param_name, param) | to_token ( & from_template_param ( & param . kind , param_name) , & param. kind ) )
466
518
. collect ( ) ;
467
519
468
520
let output_impl = if !function. constant {
0 commit comments