]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
Merge pull request #20510 from tshepang/patch-6
[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     Struct          = 1,
26     Enum            = 2,
27     Function        = 3,
28     Typedef         = 4,
29     Static          = 5,
30     Trait           = 6,
31     Impl            = 7,
32     ViewItem        = 8,
33     TyMethod        = 9,
34     Method          = 10,
35     StructField     = 11,
36     Variant         = 12,
37     // we used to have ForeignFunction and ForeignStatic. they are retired now.
38     Macro           = 15,
39     Primitive       = 16,
40     AssociatedType  = 17,
41     Constant        = 18,
42 }
43
44 impl ItemType {
45     pub fn from_item(item: &clean::Item) -> ItemType {
46         match item.inner {
47             clean::ModuleItem(..)          => ItemType::Module,
48             clean::StructItem(..)          => ItemType::Struct,
49             clean::EnumItem(..)            => ItemType::Enum,
50             clean::FunctionItem(..)        => ItemType::Function,
51             clean::TypedefItem(..)         => ItemType::Typedef,
52             clean::StaticItem(..)          => ItemType::Static,
53             clean::ConstantItem(..)        => ItemType::Constant,
54             clean::TraitItem(..)           => ItemType::Trait,
55             clean::ImplItem(..)            => ItemType::Impl,
56             clean::ViewItemItem(..)        => ItemType::ViewItem,
57             clean::TyMethodItem(..)        => ItemType::TyMethod,
58             clean::MethodItem(..)          => ItemType::Method,
59             clean::StructFieldItem(..)     => ItemType::StructField,
60             clean::VariantItem(..)         => ItemType::Variant,
61             clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
62             clean::ForeignStaticItem(..)   => ItemType::Static, // no ForeignStatic
63             clean::MacroItem(..)           => ItemType::Macro,
64             clean::PrimitiveItem(..)       => ItemType::Primitive,
65             clean::AssociatedTypeItem(..)  => ItemType::AssociatedType,
66         }
67     }
68
69     pub fn from_type_kind(kind: clean::TypeKind) -> ItemType {
70         match kind {
71             clean::TypeStruct   => ItemType::Struct,
72             clean::TypeEnum     => ItemType::Enum,
73             clean::TypeFunction => ItemType::Function,
74             clean::TypeTrait    => ItemType::Trait,
75             clean::TypeModule   => ItemType::Module,
76             clean::TypeStatic   => ItemType::Static,
77             clean::TypeConst    => ItemType::Constant,
78             clean::TypeVariant  => ItemType::Variant,
79             clean::TypeTypedef  => ItemType::Typedef,
80         }
81     }
82
83     pub fn to_static_str(&self) -> &'static str {
84         match *self {
85             ItemType::Module          => "mod",
86             ItemType::Struct          => "struct",
87             ItemType::Enum            => "enum",
88             ItemType::Function        => "fn",
89             ItemType::Typedef         => "type",
90             ItemType::Static          => "static",
91             ItemType::Trait           => "trait",
92             ItemType::Impl            => "impl",
93             ItemType::ViewItem        => "viewitem",
94             ItemType::TyMethod        => "tymethod",
95             ItemType::Method          => "method",
96             ItemType::StructField     => "structfield",
97             ItemType::Variant         => "variant",
98             ItemType::Macro           => "macro",
99             ItemType::Primitive       => "primitive",
100             ItemType::AssociatedType  => "associatedtype",
101             ItemType::Constant        => "constant",
102         }
103     }
104 }
105
106 impl fmt::Show for ItemType {
107     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108         self.to_static_str().fmt(f)
109     }
110 }
111