]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/html/item_type.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustdoc / html / item_type.rs
CommitLineData
1a4d82fc
JJ
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
13use std::fmt;
14use 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)]
23pub enum ItemType {
24 Module = 0,
85aaf69f
SL
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,
d9579d0f 42 AssociatedConst = 18,
1a4d82fc
JJ
43}
44
45impl ItemType {
46 pub fn from_item(item: &clean::Item) -> ItemType {
54a0048b
SL
47 let inner = match item.inner {
48 clean::StrippedItem(box ref item) => item,
49 ref inner@_ => inner,
50 };
51
52 match *inner {
1a4d82fc 53 clean::ModuleItem(..) => ItemType::Module,
85aaf69f
SL
54 clean::ExternCrateItem(..) => ItemType::ExternCrate,
55 clean::ImportItem(..) => ItemType::Import,
1a4d82fc
JJ
56 clean::StructItem(..) => ItemType::Struct,
57 clean::EnumItem(..) => ItemType::Enum,
58 clean::FunctionItem(..) => ItemType::Function,
59 clean::TypedefItem(..) => ItemType::Typedef,
60 clean::StaticItem(..) => ItemType::Static,
61 clean::ConstantItem(..) => ItemType::Constant,
62 clean::TraitItem(..) => ItemType::Trait,
63 clean::ImplItem(..) => ItemType::Impl,
1a4d82fc
JJ
64 clean::TyMethodItem(..) => ItemType::TyMethod,
65 clean::MethodItem(..) => ItemType::Method,
66 clean::StructFieldItem(..) => ItemType::StructField,
67 clean::VariantItem(..) => ItemType::Variant,
68 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
69 clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
70 clean::MacroItem(..) => ItemType::Macro,
71 clean::PrimitiveItem(..) => ItemType::Primitive,
d9579d0f 72 clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
1a4d82fc 73 clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
c34b1796 74 clean::DefaultImplItem(..) => ItemType::Impl,
54a0048b 75 clean::StrippedItem(..) => unreachable!(),
1a4d82fc
JJ
76 }
77 }
78
79 pub fn from_type_kind(kind: clean::TypeKind) -> ItemType {
80 match kind {
81 clean::TypeStruct => ItemType::Struct,
82 clean::TypeEnum => ItemType::Enum,
83 clean::TypeFunction => ItemType::Function,
84 clean::TypeTrait => ItemType::Trait,
85 clean::TypeModule => ItemType::Module,
86 clean::TypeStatic => ItemType::Static,
87 clean::TypeConst => ItemType::Constant,
88 clean::TypeVariant => ItemType::Variant,
89 clean::TypeTypedef => ItemType::Typedef,
90 }
91 }
92
93 pub fn to_static_str(&self) -> &'static str {
94 match *self {
95 ItemType::Module => "mod",
85aaf69f
SL
96 ItemType::ExternCrate => "externcrate",
97 ItemType::Import => "import",
1a4d82fc
JJ
98 ItemType::Struct => "struct",
99 ItemType::Enum => "enum",
100 ItemType::Function => "fn",
101 ItemType::Typedef => "type",
102 ItemType::Static => "static",
103 ItemType::Trait => "trait",
104 ItemType::Impl => "impl",
1a4d82fc
JJ
105 ItemType::TyMethod => "tymethod",
106 ItemType::Method => "method",
107 ItemType::StructField => "structfield",
108 ItemType::Variant => "variant",
109 ItemType::Macro => "macro",
110 ItemType::Primitive => "primitive",
111 ItemType::AssociatedType => "associatedtype",
112 ItemType::Constant => "constant",
d9579d0f 113 ItemType::AssociatedConst => "associatedconstant",
1a4d82fc
JJ
114 }
115 }
116}
117
85aaf69f 118impl fmt::Display for ItemType {
1a4d82fc
JJ
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 self.to_static_str().fmt(f)
121 }
122}