@@ -6,14 +6,15 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
6
6
match data {
7
7
Data :: Struct ( data) => {
8
8
let is_named = data. fields . iter ( ) . any ( |field| field. ident . is_some ( ) ) ;
9
- let names = data. fields . iter ( )
9
+ let names = data
10
+ . fields
11
+ . iter ( )
10
12
. enumerate ( )
11
13
. map ( |( idx, field) | match field. ident . as_ref ( ) {
12
14
Some ( name) => name. clone ( ) ,
13
15
None => {
14
16
let name = format ! ( "unit{idx}" ) ;
15
- let name = syn:: Ident :: new ( & name, field. span ( ) ) ;
16
- name
17
+ syn:: Ident :: new ( & name, field. span ( ) )
17
18
}
18
19
} )
19
20
. collect :: < Vec < _ > > ( ) ;
@@ -31,13 +32,11 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
31
32
let #field_name = #value;
32
33
}
33
34
} ) ;
34
- let build_struct: syn:: Expr = is_named
35
- . then ( || syn:: parse_quote! {
36
- #ident { #( #names) , * }
37
- } )
38
- . unwrap_or_else ( || syn:: parse_quote! {
39
- #ident ( #( #names) , * )
40
- } ) ;
35
+ let build_struct: syn:: Expr = if is_named {
36
+ syn:: parse_quote! { #ident { #( #names) , * } }
37
+ } else {
38
+ syn:: parse_quote! { #ident ( #( #names) , * ) }
39
+ } ;
41
40
42
41
let count = data. fields . len ( ) ;
43
42
let head: Option < syn:: Expr > = ( count != 1 ) . then ( || {
@@ -61,28 +60,30 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
61
60
}
62
61
}
63
62
Data :: Enum ( data) => {
64
- let enum_type = data. variants . iter ( )
65
- . filter ( |v| ! is_unknown ( & v . attrs ) )
66
- . fold ( None , |mut sum, next| {
63
+ let enum_type = data. variants . iter ( ) . filter ( |v| ! is_unknown ( & v . attrs ) ) . fold (
64
+ None ,
65
+ |mut sum, next| {
67
66
let ty = match & next. fields {
68
67
syn:: Fields :: Named ( _) => EnumType :: Struct ,
69
68
syn:: Fields :: Unnamed ( fields) if fields. unnamed . len ( ) == 1 => EnumType :: One ,
70
69
syn:: Fields :: Unit => EnumType :: Unit ,
71
- syn:: Fields :: Unnamed ( _) => panic ! ( "more than 1 unnamed member field are not allowed" )
70
+ syn:: Fields :: Unnamed ( _) => {
71
+ panic ! ( "more than 1 unnamed member field are not allowed" )
72
+ }
72
73
} ;
73
74
match ( * sum. get_or_insert ( ty) , ty) {
74
75
( EnumType :: Struct , EnumType :: Struct )
75
- | ( EnumType :: Struct , EnumType :: Unit )
76
- | ( EnumType :: Unit , EnumType :: Unit )
77
- | ( EnumType :: One , EnumType :: One )
78
- => ( ) ,
76
+ | ( EnumType :: Struct , EnumType :: Unit )
77
+ | ( EnumType :: Unit , EnumType :: Unit )
78
+ | ( EnumType :: One , EnumType :: One ) => ( ) ,
79
79
( EnumType :: Unit , EnumType :: One )
80
80
| ( EnumType :: One , EnumType :: Unit )
81
81
| ( _, EnumType :: Struct ) => sum = Some ( EnumType :: Struct ) ,
82
- _ => panic ! ( "enum member types must be consistent: {:?} {:?} " , sum, ty) ,
82
+ _ => panic ! ( "enum member types must be consistent: {:?}" , ( sum, ty) ) ,
83
83
}
84
84
sum
85
- } ) ;
85
+ } ,
86
+ ) ;
86
87
let enum_type = enum_type. expect ( "enum cannot be empty" ) ;
87
88
let mut iter = data. variants . iter ( ) . peekable ( ) ;
88
89
@@ -126,7 +127,10 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
126
127
} ) ;
127
128
128
129
if matches ! ( enum_type, EnumType :: Struct ) {
129
- assert ! ( unknown_arm. is_none( ) , "struct enum does not allow unknown variants" ) ;
130
+ assert ! (
131
+ unknown_arm. is_none( ) ,
132
+ "struct enum does not allow unknown variants"
133
+ ) ;
130
134
}
131
135
132
136
let fields = iter
@@ -167,8 +171,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
167
171
Some ( name) => name. clone ( ) ,
168
172
None => {
169
173
let name = format ! ( "unit{idx}" ) ;
170
- let name = syn:: Ident :: new ( & name, field. span ( ) ) ;
171
- name
174
+ syn:: Ident :: new ( & name, field. span ( ) )
172
175
}
173
176
} )
174
177
. collect :: < Vec < _ > > ( ) ;
@@ -187,22 +190,20 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
187
190
}
188
191
}
189
192
} ) ;
190
- let build_struct: syn:: Expr = is_named
191
- . then ( || syn:: parse_quote! {
192
- #ident:: #name { #( #names) , * }
193
- } )
194
- . unwrap_or_else ( || syn:: parse_quote! {
195
- #ident:: #name ( #( #names) , * )
196
- } ) ;
197
-
193
+ let build_struct: syn:: Expr = if is_named {
194
+ syn:: parse_quote! { #ident:: #name { #( #names) , * } }
195
+ } else {
196
+ syn:: parse_quote! { #ident:: #name ( #( #names) , * ) }
197
+ } ;
198
+
198
199
syn:: parse_quote!{
199
200
#idx => {
200
201
let len = cbor4ii:: core:: types:: Array :: len( reader) ?;
201
202
debug_assert_eq!( len, Some ( #num) ) ;
202
203
#( #stmt) *
203
204
#build_struct
204
205
} ,
205
- }
206
+ }
206
207
}
207
208
}
208
209
} ) ;
0 commit comments