1use std::fmt;
4
5use rustc_hir::def::{CtorOf, DefKind};
6use rustc_span::hygiene::MacroKind;
7use serde::{Serialize, Serializer};
8
9use crate::clean;
10
11#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
30#[repr(u8)]
31pub(crate) enum ItemType {
32 Keyword = 0,
33 Primitive = 1,
34 Module = 2,
35 ExternCrate = 3,
36 Import = 4,
37 Struct = 5,
38 Enum = 6,
39 Function = 7,
40 TypeAlias = 8,
41 Static = 9,
42 Trait = 10,
43 Impl = 11,
44 TyMethod = 12,
45 Method = 13,
46 StructField = 14,
47 Variant = 15,
48 Macro = 16,
49 AssocType = 17,
50 Constant = 18,
51 AssocConst = 19,
52 Union = 20,
53 ForeignType = 21,
54 ProcAttribute = 23,
56 ProcDerive = 24,
57 TraitAlias = 25,
58 }
61
62impl Serialize for ItemType {
63 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64 where
65 S: Serializer,
66 {
67 (*self as u8).serialize(serializer)
68 }
69}
70
71impl<'a> From<&'a clean::Item> for ItemType {
72 fn from(item: &'a clean::Item) -> ItemType {
73 let kind = match item.kind {
74 clean::StrippedItem(box ref item) => item,
75 ref kind => kind,
76 };
77
78 match *kind {
79 clean::ModuleItem(..) => ItemType::Module,
80 clean::ExternCrateItem { .. } => ItemType::ExternCrate,
81 clean::ImportItem(..) => ItemType::Import,
82 clean::StructItem(..) => ItemType::Struct,
83 clean::UnionItem(..) => ItemType::Union,
84 clean::EnumItem(..) => ItemType::Enum,
85 clean::FunctionItem(..) => ItemType::Function,
86 clean::TypeAliasItem(..) => ItemType::TypeAlias,
87 clean::StaticItem(..) => ItemType::Static,
88 clean::ConstantItem(..) => ItemType::Constant,
89 clean::TraitItem(..) => ItemType::Trait,
90 clean::ImplItem(..) => ItemType::Impl,
91 clean::RequiredMethodItem(..) => ItemType::TyMethod,
92 clean::MethodItem(..) => ItemType::Method,
93 clean::StructFieldItem(..) => ItemType::StructField,
94 clean::VariantItem(..) => ItemType::Variant,
95 clean::ForeignFunctionItem(..) => ItemType::Function, clean::ForeignStaticItem(..) => ItemType::Static, clean::MacroItem(..) => ItemType::Macro,
98 clean::PrimitiveItem(..) => ItemType::Primitive,
99 clean::RequiredAssocConstItem(..)
100 | clean::ProvidedAssocConstItem(..)
101 | clean::ImplAssocConstItem(..) => ItemType::AssocConst,
102 clean::RequiredAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
103 clean::ForeignTypeItem => ItemType::ForeignType,
104 clean::KeywordItem => ItemType::Keyword,
105 clean::TraitAliasItem(..) => ItemType::TraitAlias,
106 clean::ProcMacroItem(ref mac) => match mac.kind {
107 MacroKind::Bang => ItemType::Macro,
108 MacroKind::Attr => ItemType::ProcAttribute,
109 MacroKind::Derive => ItemType::ProcDerive,
110 },
111 clean::StrippedItem(..) => unreachable!(),
112 }
113 }
114}
115
116impl From<DefKind> for ItemType {
117 fn from(other: DefKind) -> Self {
118 Self::from_def_kind(other, None)
119 }
120}
121
122impl ItemType {
123 pub(crate) fn from_def_kind(kind: DefKind, parent_kind: Option<DefKind>) -> Self {
126 match kind {
127 DefKind::Enum => Self::Enum,
128 DefKind::Fn => Self::Function,
129 DefKind::Mod => Self::Module,
130 DefKind::Const => Self::Constant,
131 DefKind::Static { .. } => Self::Static,
132 DefKind::Struct => Self::Struct,
133 DefKind::Union => Self::Union,
134 DefKind::Trait => Self::Trait,
135 DefKind::TyAlias => Self::TypeAlias,
136 DefKind::TraitAlias => Self::TraitAlias,
137 DefKind::Macro(kind) => match kind {
138 MacroKind::Bang => ItemType::Macro,
139 MacroKind::Attr => ItemType::ProcAttribute,
140 MacroKind::Derive => ItemType::ProcDerive,
141 },
142 DefKind::ForeignTy => Self::ForeignType,
143 DefKind::Variant => Self::Variant,
144 DefKind::Field => Self::StructField,
145 DefKind::AssocTy => Self::AssocType,
146 DefKind::AssocFn => {
147 if let Some(DefKind::Trait) = parent_kind {
148 Self::TyMethod
149 } else {
150 Self::Method
151 }
152 }
153 DefKind::Ctor(CtorOf::Struct, _) => Self::Struct,
154 DefKind::Ctor(CtorOf::Variant, _) => Self::Variant,
155 DefKind::AssocConst => Self::AssocConst,
156 DefKind::TyParam
157 | DefKind::ConstParam
158 | DefKind::ExternCrate
159 | DefKind::Use
160 | DefKind::ForeignMod
161 | DefKind::AnonConst
162 | DefKind::InlineConst
163 | DefKind::OpaqueTy
164 | DefKind::LifetimeParam
165 | DefKind::GlobalAsm
166 | DefKind::Impl { .. }
167 | DefKind::Closure
168 | DefKind::SyntheticCoroutineBody => Self::ForeignType,
169 }
170 }
171
172 pub(crate) fn as_str(&self) -> &'static str {
173 match *self {
174 ItemType::Module => "mod",
175 ItemType::ExternCrate => "externcrate",
176 ItemType::Import => "import",
177 ItemType::Struct => "struct",
178 ItemType::Union => "union",
179 ItemType::Enum => "enum",
180 ItemType::Function => "fn",
181 ItemType::TypeAlias => "type",
182 ItemType::Static => "static",
183 ItemType::Trait => "trait",
184 ItemType::Impl => "impl",
185 ItemType::TyMethod => "tymethod",
186 ItemType::Method => "method",
187 ItemType::StructField => "structfield",
188 ItemType::Variant => "variant",
189 ItemType::Macro => "macro",
190 ItemType::Primitive => "primitive",
191 ItemType::AssocType => "associatedtype",
192 ItemType::Constant => "constant",
193 ItemType::AssocConst => "associatedconstant",
194 ItemType::ForeignType => "foreigntype",
195 ItemType::Keyword => "keyword",
196 ItemType::ProcAttribute => "attr",
197 ItemType::ProcDerive => "derive",
198 ItemType::TraitAlias => "traitalias",
199 }
200 }
201 pub(crate) fn is_method(&self) -> bool {
202 matches!(*self, ItemType::Method | ItemType::TyMethod)
203 }
204 pub(crate) fn is_adt(&self) -> bool {
205 matches!(*self, ItemType::Struct | ItemType::Union | ItemType::Enum)
206 }
207}
208
209impl fmt::Display for ItemType {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211 f.write_str(self.as_str())
212 }
213}