Skip to content

Commit d64bd5f

Browse files
authored
der_derive: use InputType enum for ValueOrd derive (#780)
Replaces `bool` flag with an `enum` which makes the code more readable
1 parent f1f48f1 commit d64bd5f

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

der/derive/src/value_ord.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub(crate) struct DeriveValueOrd {
2222
/// Fields of structs or enum variants.
2323
fields: Vec<ValueField>,
2424

25-
is_enum: bool,
25+
/// Type of input provided (`enum` or `struct`).
26+
input_type: InputType,
2627
}
2728

2829
impl DeriveValueOrd {
@@ -38,20 +39,20 @@ impl DeriveValueOrd {
3839
.next()
3940
.map(|lt| lt.lifetime.clone());
4041

41-
let (fields, is_enum) = match input.data {
42+
let (fields, input_type) = match input.data {
4243
syn::Data::Enum(data) => (
4344
data.variants
4445
.into_iter()
4546
.map(|variant| ValueField::new_enum(variant, &type_attrs))
4647
.collect(),
47-
true,
48+
InputType::Enum,
4849
),
4950
syn::Data::Struct(data) => (
5051
data.fields
5152
.into_iter()
5253
.map(|field| ValueField::new_struct(field, &type_attrs))
5354
.collect(),
54-
false,
55+
InputType::Struct,
5556
),
5657
_ => abort!(
5758
ident,
@@ -64,7 +65,7 @@ impl DeriveValueOrd {
6465
ident,
6566
lifetime,
6667
fields,
67-
is_enum,
68+
input_type,
6869
}
6970
}
7071

@@ -86,23 +87,26 @@ impl DeriveValueOrd {
8687
body.push(field.to_tokens());
8788
}
8889

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+
}
9699
}
97100
}
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};
102105

103-
#(#body)*
106+
#(#body)*
104107

105-
Ok(::core::cmp::Ordering::Equal)
108+
Ok(::core::cmp::Ordering::Equal)
109+
}
106110
}
107111
};
108112

@@ -116,6 +120,16 @@ impl DeriveValueOrd {
116120
}
117121
}
118122

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+
119133
struct ValueField {
120134
/// Name of the field
121135
ident: Ident,

0 commit comments

Comments
 (0)