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/main.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)]
54 impl Serialize for ItemType {
55 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
59 (*self as u8).serialize(serializer)
63 impl<'a> From<&'a clean::Item> for ItemType {
64 fn from(item: &'a clean::Item) -> ItemType {
65 let kind = match *item.kind {
66 clean::StrippedItem(box ref item) => item,
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::AssocConstItem(..) => ItemType::AssocConst,
93 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,
102 clean::StrippedItem(..) => unreachable!(),
107 impl From<DefKind> for ItemType {
108 fn from(other: DefKind) -> Self {
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,
129 | DefKind::ConstParam
132 | DefKind::AssocConst
133 | DefKind::ExternCrate
135 | DefKind::ForeignMod
137 | DefKind::InlineConst
140 | DefKind::LifetimeParam
144 | DefKind::Generator => Self::ForeignType,
150 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",
178 ItemType::Generic => "generic",
183 impl fmt::Display for ItemType {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185 f.write_str(self.as_str())