]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / librustdoc / html / item_type.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Item types.
12
13 use std::fmt;
14 use clean;
15
16 /// Item type. Corresponds to `clean::ItemEnum` variants.
17 ///
18 /// The search index uses item types encoded as smaller numbers which equal to
19 /// discriminants. JavaScript then is used to decode them into the original value.
20 /// Consequently, every change to this type should be synchronized to
21 /// the `itemTypes` mapping table in `static/main.js`.
22 #[derive(Copy, PartialEq, Clone)]
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     AssociatedType  = 16,
41     Constant        = 17,
42     AssociatedConst = 18,
43 }
44
45
46 #[derive(Copy, Eq, PartialEq, Clone)]
47 pub enum NameSpace {
48     Type,
49     Value,
50     Macro,
51 }
52
53 impl<'a> From<&'a clean::Item> for ItemType {
54     fn from(item: &'a clean::Item) -> ItemType {
55         let inner = match item.inner {
56             clean::StrippedItem(box ref item) => item,
57             ref inner@_ => inner,
58         };
59
60         match *inner {
61             clean::ModuleItem(..)          => ItemType::Module,
62             clean::ExternCrateItem(..)     => ItemType::ExternCrate,
63             clean::ImportItem(..)          => ItemType::Import,
64             clean::StructItem(..)          => ItemType::Struct,
65             clean::EnumItem(..)            => ItemType::Enum,
66             clean::FunctionItem(..)        => ItemType::Function,
67             clean::TypedefItem(..)         => ItemType::Typedef,
68             clean::StaticItem(..)          => ItemType::Static,
69             clean::ConstantItem(..)        => ItemType::Constant,
70             clean::TraitItem(..)           => ItemType::Trait,
71             clean::ImplItem(..)            => ItemType::Impl,
72             clean::TyMethodItem(..)        => ItemType::TyMethod,
73             clean::MethodItem(..)          => ItemType::Method,
74             clean::StructFieldItem(..)     => ItemType::StructField,
75             clean::VariantItem(..)         => ItemType::Variant,
76             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
77             clean::ForeignStaticItem(..)   => ItemType::Static, // no ForeignStatic
78             clean::MacroItem(..)           => ItemType::Macro,
79             clean::PrimitiveItem(..)       => ItemType::Primitive,
80             clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
81             clean::AssociatedTypeItem(..)  => ItemType::AssociatedType,
82             clean::DefaultImplItem(..)     => ItemType::Impl,
83             clean::StrippedItem(..)        => unreachable!(),
84         }
85     }
86 }
87
88 impl From<clean::TypeKind> for ItemType {
89     fn from(kind: clean::TypeKind) -> ItemType {
90         match kind {
91             clean::TypeStruct   => ItemType::Struct,
92             clean::TypeEnum     => ItemType::Enum,
93             clean::TypeFunction => ItemType::Function,
94             clean::TypeTrait    => ItemType::Trait,
95             clean::TypeModule   => ItemType::Module,
96             clean::TypeStatic   => ItemType::Static,
97             clean::TypeConst    => ItemType::Constant,
98             clean::TypeVariant  => ItemType::Variant,
99             clean::TypeTypedef  => ItemType::Typedef,
100         }
101     }
102 }
103
104 impl ItemType {
105     pub fn css_class(&self) -> &'static str {
106         match *self {
107             ItemType::Module          => "mod",
108             ItemType::ExternCrate     => "externcrate",
109             ItemType::Import          => "import",
110             ItemType::Struct          => "struct",
111             ItemType::Enum            => "enum",
112             ItemType::Function        => "fn",
113             ItemType::Typedef         => "type",
114             ItemType::Static          => "static",
115             ItemType::Trait           => "trait",
116             ItemType::Impl            => "impl",
117             ItemType::TyMethod        => "tymethod",
118             ItemType::Method          => "method",
119             ItemType::StructField     => "structfield",
120             ItemType::Variant         => "variant",
121             ItemType::Macro           => "macro",
122             ItemType::Primitive       => "primitive",
123             ItemType::AssociatedType  => "associatedtype",
124             ItemType::Constant        => "constant",
125             ItemType::AssociatedConst => "associatedconstant",
126         }
127     }
128
129     pub fn name_space(&self) -> NameSpace {
130         match *self {
131             ItemType::Struct |
132             ItemType::Enum |
133             ItemType::Module |
134             ItemType::Typedef |
135             ItemType::Trait |
136             ItemType::Primitive |
137             ItemType::AssociatedType => NameSpace::Type,
138
139             ItemType::ExternCrate |
140             ItemType::Import |
141             ItemType::Function |
142             ItemType::Static |
143             ItemType::Impl |
144             ItemType::TyMethod |
145             ItemType::Method |
146             ItemType::StructField |
147             ItemType::Variant |
148             ItemType::Constant |
149             ItemType::AssociatedConst => NameSpace::Value,
150
151             ItemType::Macro => NameSpace::Macro,
152         }
153     }
154 }
155
156 impl fmt::Display for ItemType {
157     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158         self.css_class().fmt(f)
159     }
160 }
161
162 pub const NAMESPACE_TYPE: &'static str = "t";
163 pub const NAMESPACE_VALUE: &'static str = "v";
164 pub const NAMESPACE_MACRO: &'static str = "m";
165
166 impl NameSpace {
167     pub fn to_static_str(&self) -> &'static str {
168         match *self {
169             NameSpace::Type => NAMESPACE_TYPE,
170             NameSpace::Value => NAMESPACE_VALUE,
171             NameSpace::Macro => NAMESPACE_MACRO,
172         }
173     }
174 }
175
176 impl fmt::Display for NameSpace {
177     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
178         self.to_static_str().fmt(f)
179     }
180 }