1 //! Item types. 2 3 use std::fmt; 4 5 use serde::{Serialize, Serializer}; 6 7 use rustc_hir::def::DefKind; 8 use rustc_span::hygiene::MacroKind; 9 10 use crate::clean; 11 12 /// Item type. Corresponds to `clean::ItemEnum` variants. 13 /// 14 /// The search index uses item types encoded as smaller numbers which equal to 15 /// discriminants. JavaScript then is used to decode them into the original value. 16 /// Consequently, every change to this type should be synchronized to 17 /// the `itemTypes` mapping table in `html/static/js/search.js`. 18 /// 19 /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and 20 /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints 21 /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an 22 /// ordering based on a helper function inside `item_module`, in the same file. 23 #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)] 24 #[repr(u8)] 25 pub(crate) enum ItemType { 26 Module = 0, 27 ExternCrate = 1, 28 Import = 2, 29 Struct = 3, 30 Enum = 4, 31 Function = 5, 32 Typedef = 6, 33 Static = 7, 34 Trait = 8, 35 Impl = 9, 36 TyMethod = 10, 37 Method = 11, 38 StructField = 12, 39 Variant = 13, 40 Macro = 14, 41 Primitive = 15, 42 AssocType = 16, 43 Constant = 17, 44 AssocConst = 18, 45 Union = 19, 46 ForeignType = 20, 47 Keyword = 21, 48 OpaqueTy = 22, 49 ProcAttribute = 23, 50 ProcDerive = 24, 51 TraitAlias = 25, 52 } 53 54 impl Serialize for ItemType { serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,55 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 56 where 57 S: Serializer, 58 { 59 (*self as u8).serialize(serializer) 60 } 61 } 62 63 impl<'a> From<&'a clean::Item> for ItemType { from(item: &'a clean::Item) -> ItemType64 fn from(item: &'a clean::Item) -> ItemType { 65 let kind = match *item.kind { 66 clean::StrippedItem(box ref item) => item, 67 ref kind => kind, 68 }; 69 70 match *kind { 71 clean::ModuleItem(..) => ItemType::Module, 72 clean::ExternCrateItem { .. } => ItemType::ExternCrate, 73 clean::ImportItem(..) => ItemType::Import, 74 clean::StructItem(..) => ItemType::Struct, 75 clean::UnionItem(..) => ItemType::Union, 76 clean::EnumItem(..) => ItemType::Enum, 77 clean::FunctionItem(..) => ItemType::Function, 78 clean::TypedefItem(..) => ItemType::Typedef, 79 clean::OpaqueTyItem(..) => ItemType::OpaqueTy, 80 clean::StaticItem(..) => ItemType::Static, 81 clean::ConstantItem(..) => ItemType::Constant, 82 clean::TraitItem(..) => ItemType::Trait, 83 clean::ImplItem(..) => ItemType::Impl, 84 clean::TyMethodItem(..) => ItemType::TyMethod, 85 clean::MethodItem(..) => ItemType::Method, 86 clean::StructFieldItem(..) => ItemType::StructField, 87 clean::VariantItem(..) => ItemType::Variant, 88 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction 89 clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic 90 clean::MacroItem(..) => ItemType::Macro, 91 clean::PrimitiveItem(..) => ItemType::Primitive, 92 clean::TyAssocConstItem(..) | clean::AssocConstItem(..) => ItemType::AssocConst, 93 clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType, 94 clean::ForeignTypeItem => ItemType::ForeignType, 95 clean::KeywordItem => ItemType::Keyword, 96 clean::TraitAliasItem(..) => ItemType::TraitAlias, 97 clean::ProcMacroItem(ref mac) => match mac.kind { 98 MacroKind::Bang => ItemType::Macro, 99 MacroKind::Attr => ItemType::ProcAttribute, 100 MacroKind::Derive => ItemType::ProcDerive, 101 }, 102 clean::StrippedItem(..) => unreachable!(), 103 } 104 } 105 } 106 107 impl From<DefKind> for ItemType { from(other: DefKind) -> Self108 fn from(other: DefKind) -> Self { 109 match other { 110 DefKind::Enum => Self::Enum, 111 DefKind::Fn => Self::Function, 112 DefKind::Mod => Self::Module, 113 DefKind::Const => Self::Constant, 114 DefKind::Static(_) => Self::Static, 115 DefKind::Struct => Self::Struct, 116 DefKind::Union => Self::Union, 117 DefKind::Trait => Self::Trait, 118 DefKind::TyAlias => Self::Typedef, 119 DefKind::TraitAlias => Self::TraitAlias, 120 DefKind::Macro(kind) => match kind { 121 MacroKind::Bang => ItemType::Macro, 122 MacroKind::Attr => ItemType::ProcAttribute, 123 MacroKind::Derive => ItemType::ProcDerive, 124 }, 125 DefKind::ForeignTy 126 | DefKind::Variant 127 | DefKind::AssocTy 128 | DefKind::TyParam 129 | DefKind::ConstParam 130 | DefKind::Ctor(..) 131 | DefKind::AssocFn 132 | DefKind::AssocConst 133 | DefKind::ExternCrate 134 | DefKind::Use 135 | DefKind::ForeignMod 136 | DefKind::AnonConst 137 | DefKind::InlineConst 138 | DefKind::OpaqueTy 139 | DefKind::ImplTraitPlaceholder 140 | DefKind::Field 141 | DefKind::LifetimeParam 142 | DefKind::GlobalAsm 143 | DefKind::Impl { .. } 144 | DefKind::Closure 145 | DefKind::Generator => Self::ForeignType, 146 } 147 } 148 } 149 150 impl ItemType { as_str(&self) -> &'static str151 pub(crate) fn as_str(&self) -> &'static str { 152 match *self { 153 ItemType::Module => "mod", 154 ItemType::ExternCrate => "externcrate", 155 ItemType::Import => "import", 156 ItemType::Struct => "struct", 157 ItemType::Union => "union", 158 ItemType::Enum => "enum", 159 ItemType::Function => "fn", 160 ItemType::Typedef => "type", 161 ItemType::Static => "static", 162 ItemType::Trait => "trait", 163 ItemType::Impl => "impl", 164 ItemType::TyMethod => "tymethod", 165 ItemType::Method => "method", 166 ItemType::StructField => "structfield", 167 ItemType::Variant => "variant", 168 ItemType::Macro => "macro", 169 ItemType::Primitive => "primitive", 170 ItemType::AssocType => "associatedtype", 171 ItemType::Constant => "constant", 172 ItemType::AssocConst => "associatedconstant", 173 ItemType::ForeignType => "foreigntype", 174 ItemType::Keyword => "keyword", 175 ItemType::OpaqueTy => "opaque", 176 ItemType::ProcAttribute => "attr", 177 ItemType::ProcDerive => "derive", 178 ItemType::TraitAlias => "traitalias", 179 } 180 } is_method(&self) -> bool181 pub(crate) fn is_method(&self) -> bool { 182 matches!(*self, ItemType::Method | ItemType::TyMethod) 183 } 184 } 185 186 impl fmt::Display for ItemType { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result187 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 188 f.write_str(self.as_str()) 189 } 190 } 191