]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Auto merge of #58281 - mark-i-m:synthesis, r=estebank
[rust.git] / src / librustdoc / html / item_type.rs
1 //! Item types.
2
3 use std::fmt;
4 use syntax_pos::hygiene::MacroKind;
5 use crate::clean;
6
7 /// Item type. Corresponds to `clean::ItemEnum` variants.
8 ///
9 /// The search index uses item types encoded as smaller numbers which equal to
10 /// discriminants. JavaScript then is used to decode them into the original value.
11 /// Consequently, every change to this type should be synchronized to
12 /// the `itemTypes` mapping table in `static/main.js`.
13 ///
14 /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
15 /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
16 /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
17 /// ordering based on a helper function inside `item_module`, in the same file.
18 #[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
19 pub enum ItemType {
20     Module          = 0,
21     ExternCrate     = 1,
22     Import          = 2,
23     Struct          = 3,
24     Enum            = 4,
25     Function        = 5,
26     Typedef         = 6,
27     Static          = 7,
28     Trait           = 8,
29     Impl            = 9,
30     TyMethod        = 10,
31     Method          = 11,
32     StructField     = 12,
33     Variant         = 13,
34     Macro           = 14,
35     Primitive       = 15,
36     AssocType       = 16,
37     Constant        = 17,
38     AssocConst      = 18,
39     Union           = 19,
40     ForeignType     = 20,
41     Keyword         = 21,
42     OpaqueTy        = 22,
43     ProcAttribute   = 23,
44     ProcDerive      = 24,
45     TraitAlias      = 25,
46 }
47
48
49 impl<'a> From<&'a clean::Item> for ItemType {
50     fn from(item: &'a clean::Item) -> ItemType {
51         let inner = match item.inner {
52             clean::StrippedItem(box ref item) => item,
53             ref inner@_ => inner,
54         };
55
56         match *inner {
57             clean::ModuleItem(..)          => ItemType::Module,
58             clean::ExternCrateItem(..)     => ItemType::ExternCrate,
59             clean::ImportItem(..)          => ItemType::Import,
60             clean::StructItem(..)          => ItemType::Struct,
61             clean::UnionItem(..)           => ItemType::Union,
62             clean::EnumItem(..)            => ItemType::Enum,
63             clean::FunctionItem(..)        => ItemType::Function,
64             clean::TypedefItem(..)         => ItemType::Typedef,
65             clean::OpaqueTyItem(..)        => ItemType::OpaqueTy,
66             clean::StaticItem(..)          => ItemType::Static,
67             clean::ConstantItem(..)        => ItemType::Constant,
68             clean::TraitItem(..)           => ItemType::Trait,
69             clean::ImplItem(..)            => ItemType::Impl,
70             clean::TyMethodItem(..)        => ItemType::TyMethod,
71             clean::MethodItem(..)          => ItemType::Method,
72             clean::StructFieldItem(..)     => ItemType::StructField,
73             clean::VariantItem(..)         => ItemType::Variant,
74             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
75             clean::ForeignStaticItem(..)   => ItemType::Static, // no ForeignStatic
76             clean::MacroItem(..)           => ItemType::Macro,
77             clean::PrimitiveItem(..)       => ItemType::Primitive,
78             clean::AssocConstItem(..)      => ItemType::AssocConst,
79             clean::AssocTypeItem(..)       => ItemType::AssocType,
80             clean::ForeignTypeItem         => ItemType::ForeignType,
81             clean::KeywordItem(..)         => ItemType::Keyword,
82             clean::TraitAliasItem(..)      => ItemType::TraitAlias,
83             clean::ProcMacroItem(ref mac)  => match mac.kind {
84                 MacroKind::Bang            => ItemType::Macro,
85                 MacroKind::Attr            => ItemType::ProcAttribute,
86                 MacroKind::Derive          => ItemType::ProcDerive,
87             }
88             clean::StrippedItem(..)        => unreachable!(),
89         }
90     }
91 }
92
93 impl From<clean::TypeKind> for ItemType {
94     fn from(kind: clean::TypeKind) -> ItemType {
95         match kind {
96             clean::TypeKind::Struct     => ItemType::Struct,
97             clean::TypeKind::Union      => ItemType::Union,
98             clean::TypeKind::Enum       => ItemType::Enum,
99             clean::TypeKind::Function   => ItemType::Function,
100             clean::TypeKind::Trait      => ItemType::Trait,
101             clean::TypeKind::Module     => ItemType::Module,
102             clean::TypeKind::Static     => ItemType::Static,
103             clean::TypeKind::Const      => ItemType::Constant,
104             clean::TypeKind::Typedef    => ItemType::Typedef,
105             clean::TypeKind::Foreign    => ItemType::ForeignType,
106             clean::TypeKind::Macro      => ItemType::Macro,
107             clean::TypeKind::Attr       => ItemType::ProcAttribute,
108             clean::TypeKind::Derive     => ItemType::ProcDerive,
109             clean::TypeKind::TraitAlias => ItemType::TraitAlias,
110         }
111     }
112 }
113
114 impl ItemType {
115     pub fn as_str(&self) -> &'static str {
116         match *self {
117             ItemType::Module          => "mod",
118             ItemType::ExternCrate     => "externcrate",
119             ItemType::Import          => "import",
120             ItemType::Struct          => "struct",
121             ItemType::Union           => "union",
122             ItemType::Enum            => "enum",
123             ItemType::Function        => "fn",
124             ItemType::Typedef         => "type",
125             ItemType::Static          => "static",
126             ItemType::Trait           => "trait",
127             ItemType::Impl            => "impl",
128             ItemType::TyMethod        => "tymethod",
129             ItemType::Method          => "method",
130             ItemType::StructField     => "structfield",
131             ItemType::Variant         => "variant",
132             ItemType::Macro           => "macro",
133             ItemType::Primitive       => "primitive",
134             ItemType::AssocType       => "associatedtype",
135             ItemType::Constant        => "constant",
136             ItemType::AssocConst      => "associatedconstant",
137             ItemType::ForeignType     => "foreigntype",
138             ItemType::Keyword         => "keyword",
139             ItemType::OpaqueTy        => "opaque",
140             ItemType::ProcAttribute   => "attr",
141             ItemType::ProcDerive      => "derive",
142             ItemType::TraitAlias      => "traitalias",
143         }
144     }
145
146     pub fn name_space(&self) -> &'static str {
147         match *self {
148             ItemType::Struct |
149             ItemType::Union |
150             ItemType::Enum |
151             ItemType::Module |
152             ItemType::Typedef |
153             ItemType::Trait |
154             ItemType::Primitive |
155             ItemType::AssocType |
156             ItemType::OpaqueTy |
157             ItemType::TraitAlias |
158             ItemType::ForeignType => NAMESPACE_TYPE,
159
160             ItemType::ExternCrate |
161             ItemType::Import |
162             ItemType::Function |
163             ItemType::Static |
164             ItemType::Impl |
165             ItemType::TyMethod |
166             ItemType::Method |
167             ItemType::StructField |
168             ItemType::Variant |
169             ItemType::Constant |
170             ItemType::AssocConst => NAMESPACE_VALUE,
171
172             ItemType::Macro |
173             ItemType::ProcAttribute |
174             ItemType::ProcDerive => NAMESPACE_MACRO,
175
176             ItemType::Keyword => NAMESPACE_KEYWORD,
177         }
178     }
179 }
180
181 impl fmt::Display for ItemType {
182     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183         write!(f, "{}", self.as_str())
184     }
185 }
186
187 pub const NAMESPACE_TYPE: &'static str = "t";
188 pub const NAMESPACE_VALUE: &'static str = "v";
189 pub const NAMESPACE_MACRO: &'static str = "m";
190 pub const NAMESPACE_KEYWORD: &'static str = "k";