]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/item_type.rs
auto merge of #15727 : fhahn/rust/remove-some-unwraps, r=alexcrichton
[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 #[deriving(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     ForeignFunction = 13,
38     ForeignStatic   = 14,
39     Macro           = 15,
40     Primitive       = 16,
41 }
42
43 impl ItemType {
44     pub fn to_static_str(&self) -> &'static str {
45         match *self {
46             Module          => "mod",
47             Struct          => "struct",
48             Enum            => "type",
49             Function        => "fn",
50             Typedef         => "type",
51             Static          => "static",
52             Trait           => "trait",
53             Impl            => "impl",
54             ViewItem        => "viewitem",
55             TyMethod        => "tymethod",
56             Method          => "method",
57             StructField     => "structfield",
58             Variant         => "variant",
59             ForeignFunction => "ffi",
60             ForeignStatic   => "ffs",
61             Macro           => "macro",
62             Primitive       => "primitive",
63         }
64     }
65 }
66
67 impl fmt::Show for ItemType {
68     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69         self.to_static_str().fmt(f)
70     }
71 }
72
73 impl fmt::Unsigned for ItemType {
74     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75         (*self as uint).fmt(f)
76     }
77 }
78
79 pub fn shortty(item: &clean::Item) -> ItemType {
80     match item.inner {
81         clean::ModuleItem(..)          => Module,
82         clean::StructItem(..)          => Struct,
83         clean::EnumItem(..)            => Enum,
84         clean::FunctionItem(..)        => Function,
85         clean::TypedefItem(..)         => Typedef,
86         clean::StaticItem(..)          => Static,
87         clean::TraitItem(..)           => Trait,
88         clean::ImplItem(..)            => Impl,
89         clean::ViewItemItem(..)        => ViewItem,
90         clean::TyMethodItem(..)        => TyMethod,
91         clean::MethodItem(..)          => Method,
92         clean::StructFieldItem(..)     => StructField,
93         clean::VariantItem(..)         => Variant,
94         clean::ForeignFunctionItem(..) => ForeignFunction,
95         clean::ForeignStaticItem(..)   => ForeignStatic,
96         clean::MacroItem(..)           => Macro,
97         clean::PrimitiveItem(..)       => Primitive,
98     }
99 }
100