rustdoc/formats/
item_type.rs

1//! Item types.
2
3use std::fmt;
4
5use rustc_hir::def::{CtorOf, DefKind};
6use rustc_span::hygiene::MacroKind;
7use serde::{Serialize, Serializer};
8
9use crate::clean;
10
11/// Item type. Corresponds to `clean::ItemEnum` variants.
12///
13/// The search index uses item types encoded as smaller numbers which equal to
14/// discriminants. JavaScript then is used to decode them into the original value.
15/// Consequently, every change to this type should be synchronized to
16/// the `itemTypes` mapping table in `html/static/js/search.js`.
17///
18/// The search engine in search.js also uses item type numbers as a tie breaker when
19/// sorting results. Keywords and primitives are given first because we want them to be easily
20/// found by new users who don't know about advanced features like type filters. The rest are
21/// mostly in an arbitrary order, but it's easier to test the search engine when
22/// it's deterministic, and these are strictly finer-grained than language namespaces, so
23/// using the path and the item type together to sort ensures that search sorting is stable.
24///
25/// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
26/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
27/// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
28/// ordering based on a helper function inside `item_module`, in the same file.
29#[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    // OpaqueTy used to be here, but it was removed in #127276
55    ProcAttribute = 23,
56    ProcDerive = 24,
57    TraitAlias = 25,
58    // This number is reserved for use in JavaScript
59    // Generic = 26,
60}
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 item) => item,
75            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, // no ForeignFunction
96            clean::ForeignStaticItem(..) => ItemType::Static,     // no ForeignStatic
97            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(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    /// Depending on the parent kind, some variants have a different translation (like a `Method`
124    /// becoming a `TyMethod`).
125    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(MacroKind::Bang) => ItemType::Macro,
138            DefKind::Macro(MacroKind::Attr) => ItemType::ProcAttribute,
139            DefKind::Macro(MacroKind::Derive) => ItemType::ProcDerive,
140            DefKind::ForeignTy => Self::ForeignType,
141            DefKind::Variant => Self::Variant,
142            DefKind::Field => Self::StructField,
143            DefKind::AssocTy => Self::AssocType,
144            DefKind::AssocFn if let Some(DefKind::Trait) = parent_kind => Self::TyMethod,
145            DefKind::AssocFn => Self::Method,
146            DefKind::Ctor(CtorOf::Struct, _) => Self::Struct,
147            DefKind::Ctor(CtorOf::Variant, _) => Self::Variant,
148            DefKind::AssocConst => Self::AssocConst,
149            DefKind::TyParam
150            | DefKind::ConstParam
151            | DefKind::ExternCrate
152            | DefKind::Use
153            | DefKind::ForeignMod
154            | DefKind::AnonConst
155            | DefKind::InlineConst
156            | DefKind::OpaqueTy
157            | DefKind::LifetimeParam
158            | DefKind::GlobalAsm
159            | DefKind::Impl { .. }
160            | DefKind::Closure
161            | DefKind::SyntheticCoroutineBody => Self::ForeignType,
162        }
163    }
164
165    pub(crate) fn as_str(&self) -> &'static str {
166        match self {
167            ItemType::Module => "mod",
168            ItemType::ExternCrate => "externcrate",
169            ItemType::Import => "import",
170            ItemType::Struct => "struct",
171            ItemType::Union => "union",
172            ItemType::Enum => "enum",
173            ItemType::Function => "fn",
174            ItemType::TypeAlias => "type",
175            ItemType::Static => "static",
176            ItemType::Trait => "trait",
177            ItemType::Impl => "impl",
178            ItemType::TyMethod => "tymethod",
179            ItemType::Method => "method",
180            ItemType::StructField => "structfield",
181            ItemType::Variant => "variant",
182            ItemType::Macro => "macro",
183            ItemType::Primitive => "primitive",
184            ItemType::AssocType => "associatedtype",
185            ItemType::Constant => "constant",
186            ItemType::AssocConst => "associatedconstant",
187            ItemType::ForeignType => "foreigntype",
188            ItemType::Keyword => "keyword",
189            ItemType::ProcAttribute => "attr",
190            ItemType::ProcDerive => "derive",
191            ItemType::TraitAlias => "traitalias",
192        }
193    }
194    pub(crate) fn is_method(&self) -> bool {
195        matches!(self, ItemType::Method | ItemType::TyMethod)
196    }
197    pub(crate) fn is_adt(&self) -> bool {
198        matches!(self, ItemType::Struct | ItemType::Union | ItemType::Enum)
199    }
200}
201
202impl fmt::Display for ItemType {
203    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204        f.write_str(self.as_str())
205    }
206}