]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Rollup merge of #66771 - SimonSapin:panic-stability, r=KodrAus
[rust.git] / src / librustdoc / html / item_type.rs
1 //! Item types.
2
3 use std::fmt;
4
5 use serde::{Serialize, Serializer};
6
7 use syntax_pos::hygiene::MacroKind;
8
9 use crate::clean;
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`.
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.
22 #[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
23 pub enum ItemType {
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,
50 }
51
52 impl 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 }
60
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,
66         };
67
68         match *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,
99             }
100             clean::StrippedItem(..)        => unreachable!(),
101         }
102     }
103 }
104
105 impl From<clean::TypeKind> for ItemType {
106     fn from(kind: clean::TypeKind) -> ItemType {
107         match kind {
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,
122         }
123     }
124 }
125
126 impl ItemType {
127     pub fn as_str(&self) -> &'static str {
128         match *self {
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",
155         }
156     }
157
158     pub fn name_space(&self) -> &'static str {
159         match *self {
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 |
185             ItemType::ProcAttribute |
186             ItemType::ProcDerive => NAMESPACE_MACRO,
187
188             ItemType::Keyword => NAMESPACE_KEYWORD,
189         }
190     }
191 }
192
193 impl fmt::Display for ItemType {
194     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195         write!(f, "{}", self.as_str())
196     }
197 }
198
199 pub const NAMESPACE_TYPE: &'static str = "t";
200 pub const NAMESPACE_VALUE: &'static str = "v";
201 pub const NAMESPACE_MACRO: &'static str = "m";
202 pub const NAMESPACE_KEYWORD: &'static str = "k";