diff options
| -rw-r--r-- | src/librustdoc/formats/item_type.rs | 95 |
1 files changed, 40 insertions, 55 deletions
diff --git a/src/librustdoc/formats/item_type.rs b/src/librustdoc/formats/item_type.rs index b8b00ab582c..ab40c01cb36 100644 --- a/src/librustdoc/formats/item_type.rs +++ b/src/librustdoc/formats/item_type.rs @@ -8,6 +8,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; use crate::clean; +macro_rules! item_type { + ($($variant:ident = $number:literal,)+) => { + /// Item type. Corresponds to `clean::ItemEnum` variants. /// /// The search index uses item types encoded as smaller numbers which equal to @@ -29,35 +32,7 @@ use crate::clean; #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)] #[repr(u8)] pub(crate) enum ItemType { - Keyword = 0, - Primitive = 1, - Module = 2, - ExternCrate = 3, - Import = 4, - Struct = 5, - Enum = 6, - Function = 7, - TypeAlias = 8, - Static = 9, - Trait = 10, - Impl = 11, - TyMethod = 12, - Method = 13, - StructField = 14, - Variant = 15, - Macro = 16, - AssocType = 17, - Constant = 18, - AssocConst = 19, - Union = 20, - ForeignType = 21, - // OpaqueTy used to be here, but it was removed in #127276 - ProcAttribute = 23, - ProcDerive = 24, - TraitAlias = 25, - // This number is reserved for use in JavaScript - // Generic = 26, - Attribute = 27, + $($variant = $number,)+ } impl Serialize for ItemType { @@ -82,32 +57,7 @@ impl<'de> Deserialize<'de> for ItemType { } fn visit_u64<E: de::Error>(self, v: u64) -> Result<ItemType, E> { Ok(match v { - 0 => ItemType::Keyword, - 1 => ItemType::Primitive, - 2 => ItemType::Module, - 3 => ItemType::ExternCrate, - 4 => ItemType::Import, - 5 => ItemType::Struct, - 6 => ItemType::Enum, - 7 => ItemType::Function, - 8 => ItemType::TypeAlias, - 9 => ItemType::Static, - 10 => ItemType::Trait, - 11 => ItemType::Impl, - 12 => ItemType::TyMethod, - 13 => ItemType::Method, - 14 => ItemType::StructField, - 15 => ItemType::Variant, - 16 => ItemType::Macro, - 17 => ItemType::AssocType, - 18 => ItemType::Constant, - 19 => ItemType::AssocConst, - 20 => ItemType::Union, - 21 => ItemType::ForeignType, - 23 => ItemType::ProcAttribute, - 24 => ItemType::ProcDerive, - 25 => ItemType::TraitAlias, - 27 => ItemType::Attribute, + $($number => ItemType::$variant,)+ _ => return Err(E::missing_field("unknown number for `ItemType` enum")), }) } @@ -116,6 +66,41 @@ impl<'de> Deserialize<'de> for ItemType { } } + } +} + +item_type! { + Keyword = 0, + Primitive = 1, + Module = 2, + ExternCrate = 3, + Import = 4, + Struct = 5, + Enum = 6, + Function = 7, + TypeAlias = 8, + Static = 9, + Trait = 10, + Impl = 11, + TyMethod = 12, + Method = 13, + StructField = 14, + Variant = 15, + Macro = 16, + AssocType = 17, + Constant = 18, + AssocConst = 19, + Union = 20, + ForeignType = 21, + // OpaqueTy used to be here, but it was removed in #127276 + ProcAttribute = 23, + ProcDerive = 24, + TraitAlias = 25, + // This number is reserved for use in JavaScript + // Generic = 26, + Attribute = 27, +} + impl<'a> From<&'a clean::Item> for ItemType { fn from(item: &'a clean::Item) -> ItemType { let kind = match &item.kind { |
