1 //! Item types.
2 
3 use std::fmt;
4 
5 use serde::{Serialize, Serializer};
6 
7 use rustc_hir::def::DefKind;
8 use rustc_span::hygiene::MacroKind;
9 
10 use crate::clean;
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
17 /// the `itemTypes` mapping table in `html/static/main.js`.
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.
23 #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
24 crate enum ItemType {
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,
51     Generic = 26,
52 }
53 
54 impl Serialize for ItemType {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,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 }
62 
63 impl<'a> From<&'a clean::Item> for ItemType {
from(item: &'a clean::Item) -> ItemType64     fn from(item: &'a clean::Item) -> ItemType {
65         let kind = match *item.kind {
66             clean::StrippedItem(box ref item) => item,
67             ref kind => kind,
68         };
69 
70         match *kind {
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,
101             },
102             clean::StrippedItem(..) => unreachable!(),
103         }
104     }
105 }
106 
107 impl From<DefKind> for ItemType {
from(other: DefKind) -> Self108     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,
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,
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
137             | DefKind::InlineConst
138             | DefKind::OpaqueTy
139             | DefKind::Field
140             | DefKind::LifetimeParam
141             | DefKind::GlobalAsm
142             | DefKind::Impl
143             | DefKind::Closure
144             | DefKind::Generator => Self::ForeignType,
145         }
146     }
147 }
148 
149 impl ItemType {
as_str(&self) -> &'static str150     crate fn as_str(&self) -> &'static str {
151         match *self {
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",
179         }
180     }
181 }
182 
183 impl fmt::Display for ItemType {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result184     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185         f.write_str(self.as_str())
186     }
187 }
188