]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | //! Item types. |
2 | ||
3 | use std::fmt; | |
60c5eb7d XL |
4 | |
5 | use serde::{Serialize, Serializer}; | |
6 | ||
cdc7bbd5 | 7 | use rustc_hir::def::DefKind; |
dfeec247 | 8 | use rustc_span::hygiene::MacroKind; |
60c5eb7d | 9 | |
9fa01778 | 10 | use crate::clean; |
1a4d82fc JJ |
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 | |
3dfed10e | 17 | /// the `itemTypes` mapping table in `html/static/main.js`. |
0bf4aa26 XL |
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. | |
cdc7bbd5 | 23 | #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)] |
fc512014 | 24 | crate enum ItemType { |
dfeec247 XL |
25 | Module = 0, |
26 | ExternCrate = 1, | |
27 | Import = 2, | |
28 | Struct = 3, | |
29 | Enum = 4, | |
30 | Function = 5, | |
31 | Typedef = 6, | |
32 | Static = 7, | |
33 | Trait = 8, | |
34 | Impl = 9, | |
35 | TyMethod = 10, | |
36 | Method = 11, | |
37 | StructField = 12, | |
38 | Variant = 13, | |
39 | Macro = 14, | |
40 | Primitive = 15, | |
41 | AssocType = 16, | |
42 | Constant = 17, | |
43 | AssocConst = 18, | |
44 | Union = 19, | |
45 | ForeignType = 20, | |
46 | Keyword = 21, | |
47 | OpaqueTy = 22, | |
48 | ProcAttribute = 23, | |
49 | ProcDerive = 24, | |
50 | TraitAlias = 25, | |
3c0e092e | 51 | Generic = 26, |
1a4d82fc JJ |
52 | } |
53 | ||
60c5eb7d XL |
54 | impl Serialize for ItemType { |
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 | } | |
9e0c209e | 62 | |
9e0c209e SL |
63 | impl<'a> From<&'a clean::Item> for ItemType { |
64 | fn from(item: &'a clean::Item) -> ItemType { | |
5869c6ff | 65 | let kind = match *item.kind { |
54a0048b | 66 | clean::StrippedItem(box ref item) => item, |
fc512014 | 67 | ref kind => kind, |
54a0048b SL |
68 | }; |
69 | ||
fc512014 | 70 | match *kind { |
dfeec247 | 71 | clean::ModuleItem(..) => ItemType::Module, |
6a06907d | 72 | clean::ExternCrateItem { .. } => ItemType::ExternCrate, |
dfeec247 XL |
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, | |
1a4d82fc | 88 | clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction |
dfeec247 XL |
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, | |
101 | }, | |
102 | clean::StrippedItem(..) => unreachable!(), | |
1a4d82fc JJ |
103 | } |
104 | } | |
9e0c209e | 105 | } |
1a4d82fc | 106 | |
cdc7bbd5 XL |
107 | impl From<DefKind> for ItemType { |
108 | 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, | |
ee023bcb | 114 | DefKind::Static(_) => Self::Static, |
cdc7bbd5 XL |
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 | |
3c0e092e | 137 | | DefKind::InlineConst |
cdc7bbd5 XL |
138 | | DefKind::OpaqueTy |
139 | | DefKind::Field | |
140 | | DefKind::LifetimeParam | |
141 | | DefKind::GlobalAsm | |
142 | | DefKind::Impl | |
143 | | DefKind::Closure | |
144 | | DefKind::Generator => Self::ForeignType, | |
1a4d82fc JJ |
145 | } |
146 | } | |
9e0c209e | 147 | } |
1a4d82fc | 148 | |
9e0c209e | 149 | impl ItemType { |
fc512014 | 150 | crate fn as_str(&self) -> &'static str { |
1a4d82fc | 151 | match *self { |
dfeec247 XL |
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", | |
3c0e092e | 178 | ItemType::Generic => "generic", |
1a4d82fc JJ |
179 | } |
180 | } | |
181 | } | |
182 | ||
85aaf69f | 183 | impl fmt::Display for ItemType { |
9fa01778 | 184 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
5869c6ff | 185 | f.write_str(self.as_str()) |
9e0c209e SL |
186 | } |
187 | } |