5 use serde::{Serialize, Serializer};
7 use rustc_hir::def::DefKind;
8 use rustc_span::hygiene::MacroKind;
12 /// Item type. Corresponds to `clean::ItemEnum` variants.
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`.
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 pub(crate) enum ItemType {
53 impl Serialize for ItemType {
54 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58 (*self as u8).serialize(serializer)
62 impl<'a> From<&'a clean::Item> for ItemType {
63 fn from(item: &'a clean::Item) -> ItemType {
64 let kind = match *item.kind {
65 clean::StrippedItem(box ref item) => item,
70 clean::ModuleItem(..) => ItemType::Module,
71 clean::ExternCrateItem { .. } => ItemType::ExternCrate,
72 clean::ImportItem(..) => ItemType::Import,
73 clean::StructItem(..) => ItemType::Struct,
74 clean::UnionItem(..) => ItemType::Union,
75 clean::EnumItem(..) => ItemType::Enum,
76 clean::FunctionItem(..) => ItemType::Function,
77 clean::TypedefItem(..) => ItemType::Typedef,
78 clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
79 clean::StaticItem(..) => ItemType::Static,
80 clean::ConstantItem(..) => ItemType::Constant,
81 clean::TraitItem(..) => ItemType::Trait,
82 clean::ImplItem(..) => ItemType::Impl,
83 clean::TyMethodItem(..) => ItemType::TyMethod,
84 clean::MethodItem(..) => ItemType::Method,
85 clean::StructFieldItem(..) => ItemType::StructField,
86 clean::VariantItem(..) => ItemType::Variant,
87 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
88 clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
89 clean::MacroItem(..) => ItemType::Macro,
90 clean::PrimitiveItem(..) => ItemType::Primitive,
91 clean::TyAssocConstItem(..) | clean::AssocConstItem(..) => ItemType::AssocConst,
92 clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
93 clean::ForeignTypeItem => ItemType::ForeignType,
94 clean::KeywordItem => ItemType::Keyword,
95 clean::TraitAliasItem(..) => ItemType::TraitAlias,
96 clean::ProcMacroItem(ref mac) => match mac.kind {
97 MacroKind::Bang => ItemType::Macro,
98 MacroKind::Attr => ItemType::ProcAttribute,
99 MacroKind::Derive => ItemType::ProcDerive,
101 clean::StrippedItem(..) => unreachable!(),
106 impl From<DefKind> for ItemType {
107 fn from(other: DefKind) -> Self {
109 DefKind::Enum => Self::Enum,
110 DefKind::Fn => Self::Function,
111 DefKind::Mod => Self::Module,
112 DefKind::Const => Self::Constant,
113 DefKind::Static(_) => Self::Static,
114 DefKind::Struct => Self::Struct,
115 DefKind::Union => Self::Union,
116 DefKind::Trait => Self::Trait,
117 DefKind::TyAlias => Self::Typedef,
118 DefKind::TraitAlias => Self::TraitAlias,
119 DefKind::Macro(kind) => match kind {
120 MacroKind::Bang => ItemType::Macro,
121 MacroKind::Attr => ItemType::ProcAttribute,
122 MacroKind::Derive => ItemType::ProcDerive,
128 | DefKind::ConstParam
131 | DefKind::AssocConst
132 | DefKind::ExternCrate
134 | DefKind::ForeignMod
136 | DefKind::InlineConst
138 | DefKind::ImplTraitPlaceholder
140 | DefKind::LifetimeParam
144 | DefKind::Generator => Self::ForeignType,
150 pub(crate) fn as_str(&self) -> &'static str {
152 ItemType::Module => "mod",
153 ItemType::ExternCrate => "externcrate",
154 ItemType::Import => "import",
155 ItemType::Struct => "struct",
156 ItemType::Union => "union",
157 ItemType::Enum => "enum",
158 ItemType::Function => "fn",
159 ItemType::Typedef => "type",
160 ItemType::Static => "static",
161 ItemType::Trait => "trait",
162 ItemType::Impl => "impl",
163 ItemType::TyMethod => "tymethod",
164 ItemType::Method => "method",
165 ItemType::StructField => "structfield",
166 ItemType::Variant => "variant",
167 ItemType::Macro => "macro",
168 ItemType::Primitive => "primitive",
169 ItemType::AssocType => "associatedtype",
170 ItemType::Constant => "constant",
171 ItemType::AssocConst => "associatedconstant",
172 ItemType::ForeignType => "foreigntype",
173 ItemType::Keyword => "keyword",
174 ItemType::OpaqueTy => "opaque",
175 ItemType::ProcAttribute => "attr",
176 ItemType::ProcDerive => "derive",
177 ItemType::TraitAlias => "traitalias",
180 pub(crate) fn is_method(&self) -> bool {
181 matches!(*self, ItemType::Method | ItemType::TyMethod)
185 impl fmt::Display for ItemType {
186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 f.write_str(self.as_str())