@@ -22,7 +22,8 @@ pub(crate) struct DeriveValueOrd {
22
22
/// Fields of structs or enum variants.
23
23
fields : Vec < ValueField > ,
24
24
25
- is_enum : bool ,
25
+ /// Type of input provided (`enum` or `struct`).
26
+ input_type : InputType ,
26
27
}
27
28
28
29
impl DeriveValueOrd {
@@ -38,20 +39,20 @@ impl DeriveValueOrd {
38
39
. next ( )
39
40
. map ( |lt| lt. lifetime . clone ( ) ) ;
40
41
41
- let ( fields, is_enum ) = match input. data {
42
+ let ( fields, input_type ) = match input. data {
42
43
syn:: Data :: Enum ( data) => (
43
44
data. variants
44
45
. into_iter ( )
45
46
. map ( |variant| ValueField :: new_enum ( variant, & type_attrs) )
46
47
. collect ( ) ,
47
- true ,
48
+ InputType :: Enum ,
48
49
) ,
49
50
syn:: Data :: Struct ( data) => (
50
51
data. fields
51
52
. into_iter ( )
52
53
. map ( |field| ValueField :: new_struct ( field, & type_attrs) )
53
54
. collect ( ) ,
54
- false ,
55
+ InputType :: Struct ,
55
56
) ,
56
57
_ => abort ! (
57
58
ident,
@@ -64,7 +65,7 @@ impl DeriveValueOrd {
64
65
ident,
65
66
lifetime,
66
67
fields,
67
- is_enum ,
68
+ input_type ,
68
69
}
69
70
}
70
71
@@ -86,23 +87,26 @@ impl DeriveValueOrd {
86
87
body. push ( field. to_tokens ( ) ) ;
87
88
}
88
89
89
- let body = if self . is_enum {
90
- quote ! {
91
- #[ allow( unused_imports) ]
92
- use :: der:: ValueOrd ;
93
- match ( self , other) {
94
- #( #body) *
95
- _ => unreachable!( ) ,
90
+ let body = match self . input_type {
91
+ InputType :: Enum => {
92
+ quote ! {
93
+ #[ allow( unused_imports) ]
94
+ use :: der:: ValueOrd ;
95
+ match ( self , other) {
96
+ #( #body) *
97
+ _ => unreachable!( ) ,
98
+ }
96
99
}
97
100
}
98
- } else {
99
- quote ! {
100
- #[ allow( unused_imports) ]
101
- use :: der:: { DerOrd , ValueOrd } ;
101
+ InputType :: Struct => {
102
+ quote ! {
103
+ #[ allow( unused_imports) ]
104
+ use :: der:: { DerOrd , ValueOrd } ;
102
105
103
- #( #body) *
106
+ #( #body) *
104
107
105
- Ok ( :: core:: cmp:: Ordering :: Equal )
108
+ Ok ( :: core:: cmp:: Ordering :: Equal )
109
+ }
106
110
}
107
111
} ;
108
112
@@ -116,6 +120,16 @@ impl DeriveValueOrd {
116
120
}
117
121
}
118
122
123
+ /// What kind of input was provided (i.e. `enum` or `struct`).
124
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
125
+ enum InputType {
126
+ /// Input is an `enum`.
127
+ Enum ,
128
+
129
+ /// Input is a `struct`.
130
+ Struct ,
131
+ }
132
+
119
133
struct ValueField {
120
134
/// Name of the field
121
135
ident : Ident ,
0 commit comments