]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Auto merge of #56951 - oli-obk:auto_toolstate_issue, r=kennytm
[rust.git] / src / librustdoc / html / item_type.rs
1 //! Item types.
2
3 use std::fmt;
4 use syntax::ext::base::MacroKind;
5 use 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, Clone, Debug)]
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     AssociatedType  = 16,
37     Constant        = 17,
38     AssociatedConst = 18,
39     Union           = 19,
40     ForeignType     = 20,
41     Keyword         = 21,
42     Existential     = 22,
43     ProcAttribute   = 23,
44     ProcDerive      = 24,
45     TraitAlias      = 25,
46 }
47
48
49 #[derive(Copy, Eq, PartialEq, Clone)]
50 pub enum NameSpace {
51     Type,
52     Value,
53     Macro,
54     Keyword,
55 }
56
57 impl<'a> From<&'a clean::Item> for ItemType {
58     fn from(item: &'a clean::Item) -> ItemType {
59         let inner = match item.inner {
60             clean::StrippedItem(box ref item) => item,
61             ref inner@_ => inner,
62         };
63
64         match *inner {
65             clean::ModuleItem(..)          => ItemType::Module,
66             clean::ExternCrateItem(..)     => ItemType::ExternCrate,
67             clean::ImportItem(..)          => ItemType::Import,
68             clean::StructItem(..)          => ItemType::Struct,
69             clean::UnionItem(..)           => ItemType::Union,
70             clean::EnumItem(..)            => ItemType::Enum,
71             clean::FunctionItem(..)        => ItemType::Function,
72             clean::TypedefItem(..)         => ItemType::Typedef,
73             clean::ExistentialItem(..)     => ItemType::Existential,
74             clean::StaticItem(..)          => ItemType::Static,
75             clean::ConstantItem(..)        => ItemType::Constant,
76             clean::TraitItem(..)           => ItemType::Trait,
77             clean::ImplItem(..)            => ItemType::Impl,
78             clean::TyMethodItem(..)        => ItemType::TyMethod,
79             clean::MethodItem(..)          => ItemType::Method,
80             clean::StructFieldItem(..)     => ItemType::StructField,
81             clean::VariantItem(..)         => ItemType::Variant,
82             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
83             clean::ForeignStaticItem(..)   => ItemType::Static, // no ForeignStatic
84             clean::MacroItem(..)           => ItemType::Macro,
85             clean::PrimitiveItem(..)       => ItemType::Primitive,
86             clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
87             clean::AssociatedTypeItem(..)  => ItemType::AssociatedType,
88             clean::ForeignTypeItem         => ItemType::ForeignType,
89             clean::KeywordItem(..)         => ItemType::Keyword,
90             clean::TraitAliasItem(..)      => ItemType::TraitAlias,
91             clean::ProcMacroItem(ref mac)  => match mac.kind {
92                 MacroKind::Bang            => ItemType::Macro,
93                 MacroKind::Attr            => ItemType::ProcAttribute,
94                 MacroKind::Derive          => ItemType::ProcDerive,
95                 MacroKind::ProcMacroStub   => unreachable!(),
96             }
97             clean::StrippedItem(..)        => unreachable!(),
98         }
99     }
100 }
101
102 impl From<clean::TypeKind> for ItemType {
103     fn from(kind: clean::TypeKind) -> ItemType {
104         match kind {
105             clean::TypeKind::Struct     => ItemType::Struct,
106             clean::TypeKind::Union      => ItemType::Union,
107             clean::TypeKind::Enum       => ItemType::Enum,
108             clean::TypeKind::Function   => ItemType::Function,
109             clean::TypeKind::Trait      => ItemType::Trait,
110             clean::TypeKind::Module     => ItemType::Module,
111             clean::TypeKind::Static     => ItemType::Static,
112             clean::TypeKind::Const      => ItemType::Constant,
113             clean::TypeKind::Variant    => ItemType::Variant,
114             clean::TypeKind::Typedef    => ItemType::Typedef,
115             clean::TypeKind::Foreign    => ItemType::ForeignType,
116             clean::TypeKind::Macro      => ItemType::Macro,
117             clean::TypeKind::Attr       => ItemType::ProcAttribute,
118             clean::TypeKind::Derive     => ItemType::ProcDerive,
119             clean::TypeKind::TraitAlias => ItemType::TraitAlias,
120         }
121     }
122 }
123
124 impl ItemType {
125     pub fn css_class(&self) -> &'static str {
126         match *self {
127             ItemType::Module          => "mod",
128             ItemType::ExternCrate     => "externcrate",
129             ItemType::Import          => "import",
130             ItemType::Struct          => "struct",
131             ItemType::Union           => "union",
132             ItemType::Enum            => "enum",
133             ItemType::Function        => "fn",
134             ItemType::Typedef         => "type",
135             ItemType::Static          => "static",
136             ItemType::Trait           => "trait",
137             ItemType::Impl            => "impl",
138             ItemType::TyMethod        => "tymethod",
139             ItemType::Method          => "method",
140             ItemType::StructField     => "structfield",
141             ItemType::Variant         => "variant",
142             ItemType::Macro           => "macro",
143             ItemType::Primitive       => "primitive",
144             ItemType::AssociatedType  => "associatedtype",
145             ItemType::Constant        => "constant",
146             ItemType::AssociatedConst => "associatedconstant",
147             ItemType::ForeignType     => "foreigntype",
148             ItemType::Keyword         => "keyword",
149             ItemType::Existential     => "existential",
150             ItemType::ProcAttribute   => "attr",
151             ItemType::ProcDerive      => "derive",
152             ItemType::TraitAlias      => "traitalias",
153         }
154     }
155
156     pub fn name_space(&self) -> NameSpace {
157         match *self {
158             ItemType::Struct |
159             ItemType::Union |
160             ItemType::Enum |
161             ItemType::Module |
162             ItemType::Typedef |
163             ItemType::Trait |
164             ItemType::Primitive |
165             ItemType::AssociatedType |
166             ItemType::Existential |
167             ItemType::TraitAlias |
168             ItemType::ForeignType => NameSpace::Type,
169
170             ItemType::ExternCrate |
171             ItemType::Import |
172             ItemType::Function |
173             ItemType::Static |
174             ItemType::Impl |
175             ItemType::TyMethod |
176             ItemType::Method |
177             ItemType::StructField |
178             ItemType::Variant |
179             ItemType::Constant |
180             ItemType::AssociatedConst => NameSpace::Value,
181
182             ItemType::Macro |
183             ItemType::ProcAttribute |
184             ItemType::ProcDerive => NameSpace::Macro,
185
186             ItemType::Keyword => NameSpace::Keyword,
187         }
188     }
189 }
190
191 impl fmt::Display for ItemType {
192     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
193         self.css_class().fmt(f)
194     }
195 }
196
197 pub const NAMESPACE_TYPE: &'static str = "t";
198 pub const NAMESPACE_VALUE: &'static str = "v";
199 pub const NAMESPACE_MACRO: &'static str = "m";
200 pub const NAMESPACE_KEYWORD: &'static str = "k";
201
202 impl NameSpace {
203     pub fn to_static_str(&self) -> &'static str {
204         match *self {
205             NameSpace::Type => NAMESPACE_TYPE,
206             NameSpace::Value => NAMESPACE_VALUE,
207             NameSpace::Macro => NAMESPACE_MACRO,
208             NameSpace::Keyword => NAMESPACE_KEYWORD,
209         }
210     }
211 }
212
213 impl fmt::Display for NameSpace {
214     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
215         self.to_static_str().fmt(f)
216     }
217 }