@@ -80,6 +80,52 @@ enum ArgMode {
8080 Into ( Type ) ,
8181}
8282
83+ impl FnArg {
84+ fn to_struct_member ( & self ) -> proc_macro2:: TokenStream {
85+ let Self {
86+ attrs : ArgAttrs { serde, serde_as } ,
87+ ident,
88+ colon_token,
89+ ..
90+ } = self ;
91+
92+ let ty = match & self . mode {
93+ ArgMode :: ByValue ( ty) => ty,
94+ ArgMode :: Clone ( ty) => ty,
95+ ArgMode :: Into ( ty) => ty,
96+ } ;
97+
98+ quote ! { #serde_as #serde #ident #colon_token #ty }
99+ }
100+
101+ fn to_arg ( & self ) -> proc_macro2:: TokenStream {
102+ let Self {
103+ ident,
104+ colon_token,
105+ ty,
106+ ..
107+ } = self ;
108+
109+ quote ! { #ident #colon_token #ty }
110+ }
111+
112+ fn to_initializer ( & self ) -> proc_macro2:: TokenStream {
113+ let Self {
114+ ident, colon_token, ..
115+ } = self ;
116+
117+ match & self . mode {
118+ ArgMode :: ByValue ( _) => quote ! { #ident } ,
119+ ArgMode :: Clone ( _) => quote ! {
120+ #ident #colon_token :: std:: clone:: Clone :: clone( #ident)
121+ } ,
122+ ArgMode :: Into ( _) => quote ! {
123+ #ident #colon_token :: std:: convert:: Into :: into( #ident)
124+ } ,
125+ }
126+ }
127+ }
128+
83129pub ( crate ) fn expand ( args : TokenStream , item : TokenStream ) -> TokenStream {
84130 let args = parse_macro_input ! ( args as MacroArgs ) ;
85131 let mod_ = parse_macro_input ! ( item as Mod ) ;
@@ -224,25 +270,7 @@ fn label_set_struct(foundations: &Path, fn_: &ItemFn) -> Option<proc_macro2::Tok
224270 let serde = quote ! { #foundations:: reexports_for_macros:: serde } ;
225271 let serde_str = LitStr :: new ( & serde. to_string ( ) , Span :: call_site ( ) ) ;
226272
227- let labels = args. iter ( ) . map ( |arg| {
228- let FnArg {
229- attrs : ArgAttrs {
230- serde, serde_as, ..
231- } ,
232- ident : label_name,
233- colon_token,
234- mode,
235- ..
236- } = arg;
237-
238- let label_type = match mode {
239- ArgMode :: ByValue ( ty) => ty,
240- ArgMode :: Clone ( ty) => ty,
241- ArgMode :: Into ( ty) => ty,
242- } ;
243-
244- quote ! { #serde_as #serde #label_name #colon_token #label_type }
245- } ) ;
273+ let labels = args. iter ( ) . map ( |arg| arg. to_struct_member ( ) ) ;
246274
247275 Some ( quote ! {
248276 #( #cfg) *
@@ -336,40 +364,15 @@ fn metric_fn(foundations: &Path, metrics_struct: &Ident, fn_: &ItemFn) -> proc_m
336364 ty : metric_type,
337365 } = fn_;
338366
339- let fn_args = args. iter ( ) . map ( |arg| {
340- let FnArg {
341- ident : arg_name,
342- colon_token,
343- ty : arg_ty,
344- ..
345- } = arg;
346-
347- quote ! { #arg_name #colon_token #arg_ty }
348- } ) ;
367+ let fn_args = args. iter ( ) . map ( |arg| arg. to_arg ( ) ) ;
349368
350369 let fn_body = if args. is_empty ( ) {
351370 quote ! {
352371 :: std:: clone:: Clone :: clone( & #metrics_struct. #metric_name)
353372 }
354373 } else {
355- let label_inits = args. iter ( ) . map ( |arg| {
356- let FnArg {
357- ident : arg_name,
358- colon_token,
359- mode,
360- ..
361- } = arg;
362-
363- match mode {
364- ArgMode :: ByValue ( _) => quote ! { #arg_name } ,
365- ArgMode :: Clone ( _) => {
366- quote ! { #arg_name #colon_token :: std:: clone:: Clone :: clone( #arg_name) }
367- }
368- ArgMode :: Into ( _) => {
369- quote ! { #arg_name #colon_token :: std:: convert:: Into :: into( #arg_name) }
370- }
371- }
372- } ) ;
374+ let label_inits = args. iter ( ) . map ( |arg| arg. to_initializer ( ) ) ;
375+
373376
374377 quote ! {
375378 :: std:: clone:: Clone :: clone(
0 commit comments