]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/formats/item_type.rs
review
[rust.git] / src / librustdoc / formats / item_type.rs
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/js/search.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 pub(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 }
52
53 impl Serialize for ItemType {
54     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
55     where
56         S: Serializer,
57     {
58         (*self as u8).serialize(serializer)
59     }
60 }
61
62 impl<'a> From<&'a clean::Item> for ItemType {
63     fn from(item: &'a clean::Item) -> ItemType {
64         let kind = match *item.kind {
65             clean::StrippedItem(box ref item) => item,
66             ref kind => kind,
67         };
68
69         match *kind {
70             clean::ModuleItem(..) => ItemType::Module,
71             clean::ExternCrateItem { .. } => ItemType::ExternCrate,
72             clean::ImportItem(..) => ItemType::Import,
73             clean::StructItem(..) => ItemType::Struct,
74             clean::UnionItem(..) => ItemType::Union,
75             clean::EnumItem(..) => ItemType::Enum,
76             clean::FunctionItem(..) => ItemType::Function,
77             clean::TypedefItem(..) => ItemType::Typedef,
78             clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
79             clean::StaticItem(..) => ItemType::Static,
80             clean::ConstantItem(..) => ItemType::Constant,
81             clean::TraitItem(..) => ItemType::Trait,
82             clean::ImplItem(..) => ItemType::Impl,
83             clean::TyMethodItem(..) => ItemType::TyMethod,
84             clean::MethodItem(..) => ItemType::Method,
85             clean::StructFieldItem(..) => ItemType::StructField,
86             clean::VariantItem(..) => ItemType::Variant,
87             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
88             clean::ForeignStaticItem(..) => ItemType::Static,     // no ForeignStatic
89             clean::MacroItem(..) => ItemType::Macro,
90             clean::PrimitiveItem(..) => ItemType::Primitive,
91             clean::TyAssocConstItem(..) | clean::AssocConstItem(..) => ItemType::AssocConst,
92             clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
93             clean::ForeignTypeItem => ItemType::ForeignType,
94             clean::KeywordItem => ItemType::Keyword,
95             clean::TraitAliasItem(..) => ItemType::TraitAlias,
96             clean::ProcMacroItem(ref mac) => match mac.kind {
97                 MacroKind::Bang => ItemType::Macro,
98                 MacroKind::Attr => ItemType::ProcAttribute,
99                 MacroKind::Derive => ItemType::ProcDerive,
100             },
101             clean::StrippedItem(..) => unreachable!(),
102         }
103     }
104 }
105
106 impl From<DefKind> for ItemType {
107     fn from(other: DefKind) -> Self {
108         match other {
109             DefKind::Enum => Self::Enum,
110             DefKind::Fn => Self::Function,
111             DefKind::Mod => Self::Module,
112             DefKind::Const => Self::Constant,
113             DefKind::Static(_) => Self::Static,
114             DefKind::Struct => Self::Struct,
115             DefKind::Union => Self::Union,
116             DefKind::Trait => Self::Trait,
117             DefKind::TyAlias => Self::Typedef,
118             DefKind::TraitAlias => Self::TraitAlias,
119             DefKind::Macro(kind) => match kind {
120                 MacroKind::Bang => ItemType::Macro,
121                 MacroKind::Attr => ItemType::ProcAttribute,
122                 MacroKind::Derive => ItemType::ProcDerive,
123             },
124             DefKind::ForeignTy
125             | DefKind::Variant
126             | DefKind::AssocTy
127             | DefKind::TyParam
128             | DefKind::ConstParam
129             | DefKind::Ctor(..)
130             | DefKind::AssocFn
131             | DefKind::AssocConst
132             | DefKind::ExternCrate
133             | DefKind::Use
134             | DefKind::ForeignMod
135             | DefKind::AnonConst
136             | DefKind::InlineConst
137             | DefKind::OpaqueTy
138             | DefKind::Field
139             | DefKind::LifetimeParam
140             | DefKind::GlobalAsm
141             | DefKind::Impl
142             | DefKind::Closure
143             | DefKind::Generator => Self::ForeignType,
144         }
145     }
146 }
147
148 impl ItemType {
149     pub(crate) fn as_str(&self) -> &'static str {
150         match *self {
151             ItemType::Module => "mod",
152             ItemType::ExternCrate => "externcrate",
153             ItemType::Import => "import",
154             ItemType::Struct => "struct",
155             ItemType::Union => "union",
156             ItemType::Enum => "enum",
157             ItemType::Function => "fn",
158             ItemType::Typedef => "type",
159             ItemType::Static => "static",
160             ItemType::Trait => "trait",
161             ItemType::Impl => "impl",
162             ItemType::TyMethod => "tymethod",
163             ItemType::Method => "method",
164             ItemType::StructField => "structfield",
165             ItemType::Variant => "variant",
166             ItemType::Macro => "macro",
167             ItemType::Primitive => "primitive",
168             ItemType::AssocType => "associatedtype",
169             ItemType::Constant => "constant",
170             ItemType::AssocConst => "associatedconstant",
171             ItemType::ForeignType => "foreigntype",
172             ItemType::Keyword => "keyword",
173             ItemType::OpaqueTy => "opaque",
174             ItemType::ProcAttribute => "attr",
175             ItemType::ProcDerive => "derive",
176             ItemType::TraitAlias => "traitalias",
177         }
178     }
179 }
180
181 impl fmt::Display for ItemType {
182     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183         f.write_str(self.as_str())
184     }
185 }