5 use serde::{Serialize, Serializer};
7 use rustc_span::hygiene::MacroKind;
11 /// Item type. Corresponds to `clean::ItemEnum` variants.
13 /// The search index uses item types encoded as smaller numbers which equal to
14 /// discriminants. JavaScript then is used to decode them into the original value.
15 /// Consequently, every change to this type should be synchronized to
16 /// the `itemTypes` mapping table in `static/main.js`.
18 /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
19 /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
20 /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
21 /// ordering based on a helper function inside `item_module`, in the same file.
22 #[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
52 impl Serialize for ItemType {
53 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57 (*self as u8).serialize(serializer)
61 impl<'a> From<&'a clean::Item> for ItemType {
62 fn from(item: &'a clean::Item) -> ItemType {
63 let inner = match item.inner {
64 clean::StrippedItem(box ref item) => item,
65 ref inner @ _ => inner,
69 clean::ModuleItem(..) => ItemType::Module,
70 clean::ExternCrateItem(..) => ItemType::ExternCrate,
71 clean::ImportItem(..) => ItemType::Import,
72 clean::StructItem(..) => ItemType::Struct,
73 clean::UnionItem(..) => ItemType::Union,
74 clean::EnumItem(..) => ItemType::Enum,
75 clean::FunctionItem(..) => ItemType::Function,
76 clean::TypedefItem(..) => ItemType::Typedef,
77 clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
78 clean::StaticItem(..) => ItemType::Static,
79 clean::ConstantItem(..) => ItemType::Constant,
80 clean::TraitItem(..) => ItemType::Trait,
81 clean::ImplItem(..) => ItemType::Impl,
82 clean::TyMethodItem(..) => ItemType::TyMethod,
83 clean::MethodItem(..) => ItemType::Method,
84 clean::StructFieldItem(..) => ItemType::StructField,
85 clean::VariantItem(..) => ItemType::Variant,
86 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
87 clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
88 clean::MacroItem(..) => ItemType::Macro,
89 clean::PrimitiveItem(..) => ItemType::Primitive,
90 clean::AssocConstItem(..) => ItemType::AssocConst,
91 clean::AssocTypeItem(..) => ItemType::AssocType,
92 clean::ForeignTypeItem => ItemType::ForeignType,
93 clean::KeywordItem(..) => ItemType::Keyword,
94 clean::TraitAliasItem(..) => ItemType::TraitAlias,
95 clean::ProcMacroItem(ref mac) => match mac.kind {
96 MacroKind::Bang => ItemType::Macro,
97 MacroKind::Attr => ItemType::ProcAttribute,
98 MacroKind::Derive => ItemType::ProcDerive,
100 clean::StrippedItem(..) => unreachable!(),
105 impl From<clean::TypeKind> for ItemType {
106 fn from(kind: clean::TypeKind) -> ItemType {
108 clean::TypeKind::Struct => ItemType::Struct,
109 clean::TypeKind::Union => ItemType::Union,
110 clean::TypeKind::Enum => ItemType::Enum,
111 clean::TypeKind::Function => ItemType::Function,
112 clean::TypeKind::Trait => ItemType::Trait,
113 clean::TypeKind::Module => ItemType::Module,
114 clean::TypeKind::Static => ItemType::Static,
115 clean::TypeKind::Const => ItemType::Constant,
116 clean::TypeKind::Typedef => ItemType::Typedef,
117 clean::TypeKind::Foreign => ItemType::ForeignType,
118 clean::TypeKind::Macro => ItemType::Macro,
119 clean::TypeKind::Attr => ItemType::ProcAttribute,
120 clean::TypeKind::Derive => ItemType::ProcDerive,
121 clean::TypeKind::TraitAlias => ItemType::TraitAlias,
127 pub fn as_str(&self) -> &'static str {
129 ItemType::Module => "mod",
130 ItemType::ExternCrate => "externcrate",
131 ItemType::Import => "import",
132 ItemType::Struct => "struct",
133 ItemType::Union => "union",
134 ItemType::Enum => "enum",
135 ItemType::Function => "fn",
136 ItemType::Typedef => "type",
137 ItemType::Static => "static",
138 ItemType::Trait => "trait",
139 ItemType::Impl => "impl",
140 ItemType::TyMethod => "tymethod",
141 ItemType::Method => "method",
142 ItemType::StructField => "structfield",
143 ItemType::Variant => "variant",
144 ItemType::Macro => "macro",
145 ItemType::Primitive => "primitive",
146 ItemType::AssocType => "associatedtype",
147 ItemType::Constant => "constant",
148 ItemType::AssocConst => "associatedconstant",
149 ItemType::ForeignType => "foreigntype",
150 ItemType::Keyword => "keyword",
151 ItemType::OpaqueTy => "opaque",
152 ItemType::ProcAttribute => "attr",
153 ItemType::ProcDerive => "derive",
154 ItemType::TraitAlias => "traitalias",
158 pub fn name_space(&self) -> &'static str {
166 | ItemType::Primitive
167 | ItemType::AssocType
169 | ItemType::TraitAlias
170 | ItemType::ForeignType => NAMESPACE_TYPE,
172 ItemType::ExternCrate
179 | ItemType::StructField
182 | ItemType::AssocConst => NAMESPACE_VALUE,
184 ItemType::Macro | ItemType::ProcAttribute | ItemType::ProcDerive => NAMESPACE_MACRO,
186 ItemType::Keyword => NAMESPACE_KEYWORD,
191 impl fmt::Display for ItemType {
192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193 write!(f, "{}", self.as_str())
197 pub const NAMESPACE_TYPE: &'static str = "t";
198 pub const NAMESPACE_VALUE: &'static str = "v";
199 pub const NAMESPACE_MACRO: &'static str = "m";
200 pub const NAMESPACE_KEYWORD: &'static str = "k";