4 use syntax_pos::hygiene::MacroKind;
7 /// Item type. Corresponds to `clean::ItemEnum` variants.
9 /// The search index uses item types encoded as smaller numbers which equal to
10 /// discriminants. JavaScript then is used to decode them into the original value.
11 /// Consequently, every change to this type should be synchronized to
12 /// the `itemTypes` mapping table in `static/main.js`.
14 /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
15 /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
16 /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
17 /// ordering based on a helper function inside `item_module`, in the same file.
18 #[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
49 impl<'a> From<&'a clean::Item> for ItemType {
50 fn from(item: &'a clean::Item) -> ItemType {
51 let inner = match item.inner {
52 clean::StrippedItem(box ref item) => item,
57 clean::ModuleItem(..) => ItemType::Module,
58 clean::ExternCrateItem(..) => ItemType::ExternCrate,
59 clean::ImportItem(..) => ItemType::Import,
60 clean::StructItem(..) => ItemType::Struct,
61 clean::UnionItem(..) => ItemType::Union,
62 clean::EnumItem(..) => ItemType::Enum,
63 clean::FunctionItem(..) => ItemType::Function,
64 clean::TypedefItem(..) => ItemType::Typedef,
65 clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
66 clean::StaticItem(..) => ItemType::Static,
67 clean::ConstantItem(..) => ItemType::Constant,
68 clean::TraitItem(..) => ItemType::Trait,
69 clean::ImplItem(..) => ItemType::Impl,
70 clean::TyMethodItem(..) => ItemType::TyMethod,
71 clean::MethodItem(..) => ItemType::Method,
72 clean::StructFieldItem(..) => ItemType::StructField,
73 clean::VariantItem(..) => ItemType::Variant,
74 clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
75 clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
76 clean::MacroItem(..) => ItemType::Macro,
77 clean::PrimitiveItem(..) => ItemType::Primitive,
78 clean::AssocConstItem(..) => ItemType::AssocConst,
79 clean::AssocTypeItem(..) => ItemType::AssocType,
80 clean::ForeignTypeItem => ItemType::ForeignType,
81 clean::KeywordItem(..) => ItemType::Keyword,
82 clean::TraitAliasItem(..) => ItemType::TraitAlias,
83 clean::ProcMacroItem(ref mac) => match mac.kind {
84 MacroKind::Bang => ItemType::Macro,
85 MacroKind::Attr => ItemType::ProcAttribute,
86 MacroKind::Derive => ItemType::ProcDerive,
88 clean::StrippedItem(..) => unreachable!(),
93 impl From<clean::TypeKind> for ItemType {
94 fn from(kind: clean::TypeKind) -> ItemType {
96 clean::TypeKind::Struct => ItemType::Struct,
97 clean::TypeKind::Union => ItemType::Union,
98 clean::TypeKind::Enum => ItemType::Enum,
99 clean::TypeKind::Function => ItemType::Function,
100 clean::TypeKind::Trait => ItemType::Trait,
101 clean::TypeKind::Module => ItemType::Module,
102 clean::TypeKind::Static => ItemType::Static,
103 clean::TypeKind::Const => ItemType::Constant,
104 clean::TypeKind::Typedef => ItemType::Typedef,
105 clean::TypeKind::Foreign => ItemType::ForeignType,
106 clean::TypeKind::Macro => ItemType::Macro,
107 clean::TypeKind::Attr => ItemType::ProcAttribute,
108 clean::TypeKind::Derive => ItemType::ProcDerive,
109 clean::TypeKind::TraitAlias => ItemType::TraitAlias,
115 pub fn as_str(&self) -> &'static str {
117 ItemType::Module => "mod",
118 ItemType::ExternCrate => "externcrate",
119 ItemType::Import => "import",
120 ItemType::Struct => "struct",
121 ItemType::Union => "union",
122 ItemType::Enum => "enum",
123 ItemType::Function => "fn",
124 ItemType::Typedef => "type",
125 ItemType::Static => "static",
126 ItemType::Trait => "trait",
127 ItemType::Impl => "impl",
128 ItemType::TyMethod => "tymethod",
129 ItemType::Method => "method",
130 ItemType::StructField => "structfield",
131 ItemType::Variant => "variant",
132 ItemType::Macro => "macro",
133 ItemType::Primitive => "primitive",
134 ItemType::AssocType => "associatedtype",
135 ItemType::Constant => "constant",
136 ItemType::AssocConst => "associatedconstant",
137 ItemType::ForeignType => "foreigntype",
138 ItemType::Keyword => "keyword",
139 ItemType::OpaqueTy => "opaque",
140 ItemType::ProcAttribute => "attr",
141 ItemType::ProcDerive => "derive",
142 ItemType::TraitAlias => "traitalias",
146 pub fn name_space(&self) -> &'static str {
154 ItemType::Primitive |
155 ItemType::AssocType |
157 ItemType::TraitAlias |
158 ItemType::ForeignType => NAMESPACE_TYPE,
160 ItemType::ExternCrate |
167 ItemType::StructField |
170 ItemType::AssocConst => NAMESPACE_VALUE,
173 ItemType::ProcAttribute |
174 ItemType::ProcDerive => NAMESPACE_MACRO,
176 ItemType::Keyword => NAMESPACE_KEYWORD,
181 impl fmt::Display for ItemType {
182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183 write!(f, "{}", self.as_str())
187 pub const NAMESPACE_TYPE: &'static str = "t";
188 pub const NAMESPACE_VALUE: &'static str = "v";
189 pub const NAMESPACE_MACRO: &'static str = "m";
190 pub const NAMESPACE_KEYWORD: &'static str = "k";