]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Fixes issue #43205: ICE in Rvalue::Len evaluation.
[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     Union           = 19,
44 }
45
46
47 #[derive(Copy, Eq, PartialEq, Clone)]
48 pub enum NameSpace {
49     Type,
50     Value,
51     Macro,
52 }
53
54 impl<'a> From<&'a clean::Item> for ItemType {
55     fn from(item: &'a clean::Item) -> ItemType {
56         let inner = match item.inner {
57             clean::StrippedItem(box ref item) => item,
58             ref inner@_ => inner,
59         };
60
61         match *inner {
62             clean::ModuleItem(..)          => ItemType::Module,
63             clean::ExternCrateItem(..)     => ItemType::ExternCrate,
64             clean::ImportItem(..)          => ItemType::Import,
65             clean::StructItem(..)          => ItemType::Struct,
66             clean::UnionItem(..)           => ItemType::Union,
67             clean::EnumItem(..)            => ItemType::Enum,
68             clean::FunctionItem(..)        => ItemType::Function,
69             clean::TypedefItem(..)         => ItemType::Typedef,
70             clean::StaticItem(..)          => ItemType::Static,
71             clean::ConstantItem(..)        => ItemType::Constant,
72             clean::TraitItem(..)           => ItemType::Trait,
73             clean::ImplItem(..)            => ItemType::Impl,
74             clean::TyMethodItem(..)        => ItemType::TyMethod,
75             clean::MethodItem(..)          => ItemType::Method,
76             clean::StructFieldItem(..)     => ItemType::StructField,
77             clean::VariantItem(..)         => ItemType::Variant,
78             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
79             clean::ForeignStaticItem(..)   => ItemType::Static, // no ForeignStatic
80             clean::MacroItem(..)           => ItemType::Macro,
81             clean::PrimitiveItem(..)       => ItemType::Primitive,
82             clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
83             clean::AssociatedTypeItem(..)  => ItemType::AssociatedType,
84             clean::DefaultImplItem(..)     => ItemType::Impl,
85             clean::StrippedItem(..)        => unreachable!(),
86         }
87     }
88 }
89
90 impl From<clean::TypeKind> for ItemType {
91     fn from(kind: clean::TypeKind) -> ItemType {
92         match kind {
93             clean::TypeKind::Struct   => ItemType::Struct,
94             clean::TypeKind::Union    => ItemType::Union,
95             clean::TypeKind::Enum     => ItemType::Enum,
96             clean::TypeKind::Function => ItemType::Function,
97             clean::TypeKind::Trait    => ItemType::Trait,
98             clean::TypeKind::Module   => ItemType::Module,
99             clean::TypeKind::Static   => ItemType::Static,
100             clean::TypeKind::Const    => ItemType::Constant,
101             clean::TypeKind::Variant  => ItemType::Variant,
102             clean::TypeKind::Typedef  => ItemType::Typedef,
103         }
104     }
105 }
106
107 impl ItemType {
108     pub fn css_class(&self) -> &'static str {
109         match *self {
110             ItemType::Module          => "mod",
111             ItemType::ExternCrate     => "externcrate",
112             ItemType::Import          => "import",
113             ItemType::Struct          => "struct",
114             ItemType::Union           => "union",
115             ItemType::Enum            => "enum",
116             ItemType::Function        => "fn",
117             ItemType::Typedef         => "type",
118             ItemType::Static          => "static",
119             ItemType::Trait           => "trait",
120             ItemType::Impl            => "impl",
121             ItemType::TyMethod        => "tymethod",
122             ItemType::Method          => "method",
123             ItemType::StructField     => "structfield",
124             ItemType::Variant         => "variant",
125             ItemType::Macro           => "macro",
126             ItemType::Primitive       => "primitive",
127             ItemType::AssociatedType  => "associatedtype",
128             ItemType::Constant        => "constant",
129             ItemType::AssociatedConst => "associatedconstant",
130         }
131     }
132
133     pub fn name_space(&self) -> NameSpace {
134         match *self {
135             ItemType::Struct |
136             ItemType::Union |
137             ItemType::Enum |
138             ItemType::Module |
139             ItemType::Typedef |
140             ItemType::Trait |
141             ItemType::Primitive |
142             ItemType::AssociatedType => NameSpace::Type,
143
144             ItemType::ExternCrate |
145             ItemType::Import |
146             ItemType::Function |
147             ItemType::Static |
148             ItemType::Impl |
149             ItemType::TyMethod |
150             ItemType::Method |
151             ItemType::StructField |
152             ItemType::Variant |
153             ItemType::Constant |
154             ItemType::AssociatedConst => NameSpace::Value,
155
156             ItemType::Macro => NameSpace::Macro,
157         }
158     }
159 }
160
161 impl fmt::Display for ItemType {
162     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
163         self.css_class().fmt(f)
164     }
165 }
166
167 pub const NAMESPACE_TYPE: &'static str = "t";
168 pub const NAMESPACE_VALUE: &'static str = "v";
169 pub const NAMESPACE_MACRO: &'static str = "m";
170
171 impl NameSpace {
172     pub fn to_static_str(&self) -> &'static str {
173         match *self {
174             NameSpace::Type => NAMESPACE_TYPE,
175             NameSpace::Value => NAMESPACE_VALUE,
176             NameSpace::Macro => NAMESPACE_MACRO,
177         }
178     }
179 }
180
181 impl fmt::Display for NameSpace {
182     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183         self.to_static_str().fmt(f)
184     }
185 }