Skip to content

Commit ad2e096

Browse files
Make it impossible to forget a conversion for the rustdoc ItemType enum
1 parent 7b35d8e commit ad2e096

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

src/librustdoc/formats/item_type.rs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
88

99
use crate::clean;
1010

11+
macro_rules! item_type {
12+
($($variant:ident = $number:literal,)+) => {
13+
1114
/// Item type. Corresponds to `clean::ItemEnum` variants.
1215
///
1316
/// The search index uses item types encoded as smaller numbers which equal to
@@ -29,35 +32,7 @@ use crate::clean;
2932
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
3033
#[repr(u8)]
3134
pub(crate) enum ItemType {
32-
Keyword = 0,
33-
Primitive = 1,
34-
Module = 2,
35-
ExternCrate = 3,
36-
Import = 4,
37-
Struct = 5,
38-
Enum = 6,
39-
Function = 7,
40-
TypeAlias = 8,
41-
Static = 9,
42-
Trait = 10,
43-
Impl = 11,
44-
TyMethod = 12,
45-
Method = 13,
46-
StructField = 14,
47-
Variant = 15,
48-
Macro = 16,
49-
AssocType = 17,
50-
Constant = 18,
51-
AssocConst = 19,
52-
Union = 20,
53-
ForeignType = 21,
54-
// OpaqueTy used to be here, but it was removed in #127276
55-
ProcAttribute = 23,
56-
ProcDerive = 24,
57-
TraitAlias = 25,
58-
// This number is reserved for use in JavaScript
59-
// Generic = 26,
60-
Attribute = 27,
35+
$($variant = $number,)+
6136
}
6237

6338
impl Serialize for ItemType {
@@ -82,32 +57,7 @@ impl<'de> Deserialize<'de> for ItemType {
8257
}
8358
fn visit_u64<E: de::Error>(self, v: u64) -> Result<ItemType, E> {
8459
Ok(match v {
85-
0 => ItemType::Keyword,
86-
1 => ItemType::Primitive,
87-
2 => ItemType::Module,
88-
3 => ItemType::ExternCrate,
89-
4 => ItemType::Import,
90-
5 => ItemType::Struct,
91-
6 => ItemType::Enum,
92-
7 => ItemType::Function,
93-
8 => ItemType::TypeAlias,
94-
9 => ItemType::Static,
95-
10 => ItemType::Trait,
96-
11 => ItemType::Impl,
97-
12 => ItemType::TyMethod,
98-
13 => ItemType::Method,
99-
14 => ItemType::StructField,
100-
15 => ItemType::Variant,
101-
16 => ItemType::Macro,
102-
17 => ItemType::AssocType,
103-
18 => ItemType::Constant,
104-
19 => ItemType::AssocConst,
105-
20 => ItemType::Union,
106-
21 => ItemType::ForeignType,
107-
23 => ItemType::ProcAttribute,
108-
24 => ItemType::ProcDerive,
109-
25 => ItemType::TraitAlias,
110-
27 => ItemType::Attribute,
60+
$($number => ItemType::$variant,)+
11161
_ => return Err(E::missing_field("unknown number for `ItemType` enum")),
11262
})
11363
}
@@ -116,6 +66,41 @@ impl<'de> Deserialize<'de> for ItemType {
11666
}
11767
}
11868

69+
}
70+
}
71+
72+
item_type! {
73+
Keyword = 0,
74+
Primitive = 1,
75+
Module = 2,
76+
ExternCrate = 3,
77+
Import = 4,
78+
Struct = 5,
79+
Enum = 6,
80+
Function = 7,
81+
TypeAlias = 8,
82+
Static = 9,
83+
Trait = 10,
84+
Impl = 11,
85+
TyMethod = 12,
86+
Method = 13,
87+
StructField = 14,
88+
Variant = 15,
89+
Macro = 16,
90+
AssocType = 17,
91+
Constant = 18,
92+
AssocConst = 19,
93+
Union = 20,
94+
ForeignType = 21,
95+
// OpaqueTy used to be here, but it was removed in #127276
96+
ProcAttribute = 23,
97+
ProcDerive = 24,
98+
TraitAlias = 25,
99+
// This number is reserved for use in JavaScript
100+
// Generic = 26,
101+
Attribute = 27,
102+
}
103+
119104
impl<'a> From<&'a clean::Item> for ItemType {
120105
fn from(item: &'a clean::Item) -> ItemType {
121106
let kind = match &item.kind {

0 commit comments

Comments
 (0)