]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/html/item_type.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustdoc / html / item_type.rs
CommitLineData
1a4d82fc
JJ
1//! Item types.
2
3use std::fmt;
60c5eb7d
XL
4
5use serde::{Serialize, Serializer};
6
dfeec247 7use rustc_span::hygiene::MacroKind;
60c5eb7d 8
9fa01778 9use crate::clean;
1a4d82fc
JJ
10
11/// Item type. Corresponds to `clean::ItemEnum` variants.
12///
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`.
0bf4aa26
XL
17///
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.
532ac7d7 22#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
1a4d82fc 23pub enum ItemType {
dfeec247
XL
24 Module = 0,
25 ExternCrate = 1,
26 Import = 2,
27 Struct = 3,
28 Enum = 4,
29 Function = 5,
30 Typedef = 6,
31 Static = 7,
32 Trait = 8,
33 Impl = 9,
34 TyMethod = 10,
35 Method = 11,
36 StructField = 12,
37 Variant = 13,
38 Macro = 14,
39 Primitive = 15,
40 AssocType = 16,
41 Constant = 17,
42 AssocConst = 18,
43 Union = 19,
44 ForeignType = 20,
45 Keyword = 21,
46 OpaqueTy = 22,
47 ProcAttribute = 23,
48 ProcDerive = 24,
49 TraitAlias = 25,
1a4d82fc
JJ
50}
51
60c5eb7d
XL
52impl Serialize for ItemType {
53 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
54 where
55 S: Serializer,
56 {
57 (*self as u8).serialize(serializer)
58 }
59}
9e0c209e 60
9e0c209e
SL
61impl<'a> From<&'a clean::Item> for ItemType {
62 fn from(item: &'a clean::Item) -> ItemType {
54a0048b
SL
63 let inner = match item.inner {
64 clean::StrippedItem(box ref item) => item,
74b04a01 65 ref inner => inner,
54a0048b
SL
66 };
67
68 match *inner {
dfeec247
XL
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,
1a4d82fc 86 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
dfeec247
XL
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,
99 },
100 clean::StrippedItem(..) => unreachable!(),
1a4d82fc
JJ
101 }
102 }
9e0c209e 103}
1a4d82fc 104
9e0c209e
SL
105impl From<clean::TypeKind> for ItemType {
106 fn from(kind: clean::TypeKind) -> ItemType {
1a4d82fc 107 match kind {
dfeec247
XL
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,
9fa01778 121 clean::TypeKind::TraitAlias => ItemType::TraitAlias,
1a4d82fc
JJ
122 }
123 }
9e0c209e 124}
1a4d82fc 125
9e0c209e 126impl ItemType {
e74abb32 127 pub fn as_str(&self) -> &'static str {
1a4d82fc 128 match *self {
dfeec247
XL
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",
1a4d82fc
JJ
155 }
156 }
9e0c209e 157
e74abb32 158 pub fn name_space(&self) -> &'static str {
9e0c209e 159 match *self {
dfeec247
XL
160 ItemType::Struct
161 | ItemType::Union
162 | ItemType::Enum
163 | ItemType::Module
164 | ItemType::Typedef
165 | ItemType::Trait
166 | ItemType::Primitive
167 | ItemType::AssocType
168 | ItemType::OpaqueTy
169 | ItemType::TraitAlias
170 | ItemType::ForeignType => NAMESPACE_TYPE,
171
172 ItemType::ExternCrate
173 | ItemType::Import
174 | ItemType::Function
175 | ItemType::Static
176 | ItemType::Impl
177 | ItemType::TyMethod
178 | ItemType::Method
179 | ItemType::StructField
180 | ItemType::Variant
181 | ItemType::Constant
182 | ItemType::AssocConst => NAMESPACE_VALUE,
183
184 ItemType::Macro | ItemType::ProcAttribute | ItemType::ProcDerive => NAMESPACE_MACRO,
94b46f34 185
e74abb32 186 ItemType::Keyword => NAMESPACE_KEYWORD,
9e0c209e
SL
187 }
188 }
1a4d82fc
JJ
189}
190
85aaf69f 191impl fmt::Display for ItemType {
9fa01778 192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
e74abb32 193 write!(f, "{}", self.as_str())
9e0c209e
SL
194 }
195}
196
74b04a01
XL
197pub const NAMESPACE_TYPE: &str = "t";
198pub const NAMESPACE_VALUE: &str = "v";
199pub const NAMESPACE_MACRO: &str = "m";
200pub const NAMESPACE_KEYWORD: &str = "k";