]>
git.proxmox.com Git - rustc.git/blob - src/librustdoc/formats/item_type.rs
5 use serde
::{Serialize, Serializer}
;
7 use rustc_hir
::def
::DefKind
;
8 use rustc_span
::hygiene
::MacroKind
;
12 /// Item type. Corresponds to `clean::ItemEnum` variants.
14 /// The search index uses item types encoded as smaller numbers which equal to
15 /// discriminants. JavaScript then is used to decode them into the original value.
16 /// Consequently, every change to this type should be synchronized to
17 /// the `itemTypes` mapping table in `html/static/js/search.js`.
19 /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
20 /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
21 /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
22 /// ordering based on a helper function inside `item_module`, in the same file.
23 #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
25 pub(crate) enum ItemType
{
54 impl Serialize
for ItemType
{
55 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
59 (*self as u8).serialize(serializer
)
63 impl<'a
> From
<&'a clean
::Item
> for ItemType
{
64 fn from(item
: &'a clean
::Item
) -> ItemType
{
65 let kind
= match *item
.kind
{
66 clean
::StrippedItem(box ref item
) => item
,
71 clean
::ModuleItem(..) => ItemType
::Module
,
72 clean
::ExternCrateItem { .. }
=> ItemType
::ExternCrate
,
73 clean
::ImportItem(..) => ItemType
::Import
,
74 clean
::StructItem(..) => ItemType
::Struct
,
75 clean
::UnionItem(..) => ItemType
::Union
,
76 clean
::EnumItem(..) => ItemType
::Enum
,
77 clean
::FunctionItem(..) => ItemType
::Function
,
78 clean
::TypedefItem(..) => ItemType
::Typedef
,
79 clean
::OpaqueTyItem(..) => ItemType
::OpaqueTy
,
80 clean
::StaticItem(..) => ItemType
::Static
,
81 clean
::ConstantItem(..) => ItemType
::Constant
,
82 clean
::TraitItem(..) => ItemType
::Trait
,
83 clean
::ImplItem(..) => ItemType
::Impl
,
84 clean
::TyMethodItem(..) => ItemType
::TyMethod
,
85 clean
::MethodItem(..) => ItemType
::Method
,
86 clean
::StructFieldItem(..) => ItemType
::StructField
,
87 clean
::VariantItem(..) => ItemType
::Variant
,
88 clean
::ForeignFunctionItem(..) => ItemType
::Function
, // no ForeignFunction
89 clean
::ForeignStaticItem(..) => ItemType
::Static
, // no ForeignStatic
90 clean
::MacroItem(..) => ItemType
::Macro
,
91 clean
::PrimitiveItem(..) => ItemType
::Primitive
,
92 clean
::TyAssocConstItem(..) | clean
::AssocConstItem(..) => ItemType
::AssocConst
,
93 clean
::TyAssocTypeItem(..) | clean
::AssocTypeItem(..) => ItemType
::AssocType
,
94 clean
::ForeignTypeItem
=> ItemType
::ForeignType
,
95 clean
::KeywordItem
=> ItemType
::Keyword
,
96 clean
::TraitAliasItem(..) => ItemType
::TraitAlias
,
97 clean
::ProcMacroItem(ref mac
) => match mac
.kind
{
98 MacroKind
::Bang
=> ItemType
::Macro
,
99 MacroKind
::Attr
=> ItemType
::ProcAttribute
,
100 MacroKind
::Derive
=> ItemType
::ProcDerive
,
102 clean
::StrippedItem(..) => unreachable
!(),
107 impl From
<DefKind
> for ItemType
{
108 fn from(other
: DefKind
) -> Self {
110 DefKind
::Enum
=> Self::Enum
,
111 DefKind
::Fn
=> Self::Function
,
112 DefKind
::Mod
=> Self::Module
,
113 DefKind
::Const
=> Self::Constant
,
114 DefKind
::Static(_
) => Self::Static
,
115 DefKind
::Struct
=> Self::Struct
,
116 DefKind
::Union
=> Self::Union
,
117 DefKind
::Trait
=> Self::Trait
,
118 DefKind
::TyAlias
=> Self::Typedef
,
119 DefKind
::TraitAlias
=> Self::TraitAlias
,
120 DefKind
::Macro(kind
) => match kind
{
121 MacroKind
::Bang
=> ItemType
::Macro
,
122 MacroKind
::Attr
=> ItemType
::ProcAttribute
,
123 MacroKind
::Derive
=> ItemType
::ProcDerive
,
129 | DefKind
::ConstParam
132 | DefKind
::AssocConst
133 | DefKind
::ExternCrate
135 | DefKind
::ForeignMod
137 | DefKind
::InlineConst
139 | DefKind
::ImplTraitPlaceholder
141 | DefKind
::LifetimeParam
143 | DefKind
::Impl { .. }
145 | DefKind
::Generator
=> Self::ForeignType
,
151 pub(crate) fn as_str(&self) -> &'
static str {
153 ItemType
::Module
=> "mod",
154 ItemType
::ExternCrate
=> "externcrate",
155 ItemType
::Import
=> "import",
156 ItemType
::Struct
=> "struct",
157 ItemType
::Union
=> "union",
158 ItemType
::Enum
=> "enum",
159 ItemType
::Function
=> "fn",
160 ItemType
::Typedef
=> "type",
161 ItemType
::Static
=> "static",
162 ItemType
::Trait
=> "trait",
163 ItemType
::Impl
=> "impl",
164 ItemType
::TyMethod
=> "tymethod",
165 ItemType
::Method
=> "method",
166 ItemType
::StructField
=> "structfield",
167 ItemType
::Variant
=> "variant",
168 ItemType
::Macro
=> "macro",
169 ItemType
::Primitive
=> "primitive",
170 ItemType
::AssocType
=> "associatedtype",
171 ItemType
::Constant
=> "constant",
172 ItemType
::AssocConst
=> "associatedconstant",
173 ItemType
::ForeignType
=> "foreigntype",
174 ItemType
::Keyword
=> "keyword",
175 ItemType
::OpaqueTy
=> "opaque",
176 ItemType
::ProcAttribute
=> "attr",
177 ItemType
::ProcDerive
=> "derive",
178 ItemType
::TraitAlias
=> "traitalias",
181 pub(crate) fn is_method(&self) -> bool
{
182 matches
!(*self, ItemType
::Method
| ItemType
::TyMethod
)
186 impl fmt
::Display
for ItemType
{
187 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
188 f
.write_str(self.as_str())