Skip to content

Commit fa400ac

Browse files
committed
rustdoc: Create enum for sections holding items
1 parent 427eba2 commit fa400ac

File tree

2 files changed

+128
-37
lines changed

2 files changed

+128
-37
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 121 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,33 +2409,124 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
24092409
}
24102410
}
24112411

2412-
fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
2412+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2413+
enum ItemSection {
2414+
Reexports,
2415+
Modules,
2416+
Structs,
2417+
Unions,
2418+
Enums,
2419+
Functions,
2420+
TypeDefinitions,
2421+
Statics,
2422+
Constants,
2423+
Traits,
2424+
Implementations,
2425+
TypeMethods,
2426+
Methods,
2427+
StructFields,
2428+
Variants,
2429+
Macros,
2430+
PrimitiveTypes,
2431+
AssociatedTypes,
2432+
AssociatedConstants,
2433+
ForeignTypes,
2434+
Keywords,
2435+
OpaqueTypes,
2436+
AttributeMacros,
2437+
DeriveMacros,
2438+
TraitAliases,
2439+
}
2440+
2441+
impl ItemSection {
2442+
fn id(self) -> &'static str {
2443+
match self {
2444+
Self::Reexports => "reexports",
2445+
Self::Modules => "modules",
2446+
Self::Structs => "structs",
2447+
Self::Unions => "unions",
2448+
Self::Enums => "enums",
2449+
Self::Functions => "functions",
2450+
Self::TypeDefinitions => "types",
2451+
Self::Statics => "statics",
2452+
Self::Constants => "constants",
2453+
Self::Traits => "traits",
2454+
Self::Implementations => "impls",
2455+
Self::TypeMethods => "tymethods",
2456+
Self::Methods => "methods",
2457+
Self::StructFields => "fields",
2458+
Self::Variants => "variants",
2459+
Self::Macros => "macros",
2460+
Self::PrimitiveTypes => "primitives",
2461+
Self::AssociatedTypes => "associated-types",
2462+
Self::AssociatedConstants => "associated-consts",
2463+
Self::ForeignTypes => "foreign-types",
2464+
Self::Keywords => "keywords",
2465+
Self::OpaqueTypes => "opaque-types",
2466+
Self::AttributeMacros => "attributes",
2467+
Self::DeriveMacros => "derives",
2468+
Self::TraitAliases => "trait-aliases",
2469+
}
2470+
}
2471+
2472+
fn name(self) -> &'static str {
2473+
match self {
2474+
Self::Reexports => "Re-exports",
2475+
Self::Modules => "Modules",
2476+
Self::Structs => "Structs",
2477+
Self::Unions => "Unions",
2478+
Self::Enums => "Enums",
2479+
Self::Functions => "Functions",
2480+
Self::TypeDefinitions => "Type Definitions",
2481+
Self::Statics => "Statics",
2482+
Self::Constants => "Constants",
2483+
Self::Traits => "Traits",
2484+
Self::Implementations => "Implementations",
2485+
Self::TypeMethods => "Type Methods",
2486+
Self::Methods => "Methods",
2487+
Self::StructFields => "Struct Fields",
2488+
Self::Variants => "Variants",
2489+
Self::Macros => "Macros",
2490+
Self::PrimitiveTypes => "Primitive Types",
2491+
Self::AssociatedTypes => "Associated Types",
2492+
Self::AssociatedConstants => "Associated Constants",
2493+
Self::ForeignTypes => "Foreign Types",
2494+
Self::Keywords => "Keywords",
2495+
Self::OpaqueTypes => "Opaque Types",
2496+
Self::AttributeMacros => "Attribute Macros",
2497+
Self::DeriveMacros => "Derive Macros",
2498+
Self::TraitAliases => "Trait aliases",
2499+
}
2500+
}
2501+
}
2502+
2503+
fn item_ty_to_section(ty: ItemType) -> ItemSection {
24132504
match ty {
2414-
ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
2415-
ItemType::Module => ("modules", "Modules"),
2416-
ItemType::Struct => ("structs", "Structs"),
2417-
ItemType::Union => ("unions", "Unions"),
2418-
ItemType::Enum => ("enums", "Enums"),
2419-
ItemType::Function => ("functions", "Functions"),
2420-
ItemType::Typedef => ("types", "Type Definitions"),
2421-
ItemType::Static => ("statics", "Statics"),
2422-
ItemType::Constant => ("constants", "Constants"),
2423-
ItemType::Trait => ("traits", "Traits"),
2424-
ItemType::Impl => ("impls", "Implementations"),
2425-
ItemType::TyMethod => ("tymethods", "Type Methods"),
2426-
ItemType::Method => ("methods", "Methods"),
2427-
ItemType::StructField => ("fields", "Struct Fields"),
2428-
ItemType::Variant => ("variants", "Variants"),
2429-
ItemType::Macro => ("macros", "Macros"),
2430-
ItemType::Primitive => ("primitives", "Primitive Types"),
2431-
ItemType::AssocType => ("associated-types", "Associated Types"),
2432-
ItemType::AssocConst => ("associated-consts", "Associated Constants"),
2433-
ItemType::ForeignType => ("foreign-types", "Foreign Types"),
2434-
ItemType::Keyword => ("keywords", "Keywords"),
2435-
ItemType::OpaqueTy => ("opaque-types", "Opaque Types"),
2436-
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
2437-
ItemType::ProcDerive => ("derives", "Derive Macros"),
2438-
ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
2505+
ItemType::ExternCrate | ItemType::Import => ItemSection::Reexports,
2506+
ItemType::Module => ItemSection::Modules,
2507+
ItemType::Struct => ItemSection::Structs,
2508+
ItemType::Union => ItemSection::Unions,
2509+
ItemType::Enum => ItemSection::Enums,
2510+
ItemType::Function => ItemSection::Functions,
2511+
ItemType::Typedef => ItemSection::TypeDefinitions,
2512+
ItemType::Static => ItemSection::Statics,
2513+
ItemType::Constant => ItemSection::Constants,
2514+
ItemType::Trait => ItemSection::Traits,
2515+
ItemType::Impl => ItemSection::Implementations,
2516+
ItemType::TyMethod => ItemSection::TypeMethods,
2517+
ItemType::Method => ItemSection::Methods,
2518+
ItemType::StructField => ItemSection::StructFields,
2519+
ItemType::Variant => ItemSection::Variants,
2520+
ItemType::Macro => ItemSection::Macros,
2521+
ItemType::Primitive => ItemSection::PrimitiveTypes,
2522+
ItemType::AssocType => ItemSection::AssociatedTypes,
2523+
ItemType::AssocConst => ItemSection::AssociatedConstants,
2524+
ItemType::ForeignType => ItemSection::ForeignTypes,
2525+
ItemType::Keyword => ItemSection::Keywords,
2526+
ItemType::OpaqueTy => ItemSection::OpaqueTypes,
2527+
ItemType::ProcAttribute => ItemSection::AttributeMacros,
2528+
ItemType::ProcDerive => ItemSection::DeriveMacros,
2529+
ItemType::TraitAlias => ItemSection::TraitAliases,
24392530
ItemType::Generic => unreachable!(),
24402531
}
24412532
}
@@ -2449,8 +2540,8 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
24492540
&& (it.type_() == ItemType::ExternCrate
24502541
|| (it.type_() == ItemType::Import && !it.is_stripped()))
24512542
}) {
2452-
let (id, name) = item_ty_to_strs(ItemType::Import);
2453-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
2543+
let sec = item_ty_to_section(ItemType::Import);
2544+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
24542545
}
24552546

24562547
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
@@ -2478,8 +2569,8 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
24782569
ItemType::Keyword,
24792570
] {
24802571
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
2481-
let (id, name) = item_ty_to_strs(myty);
2482-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
2572+
let sec = item_ty_to_section(myty);
2573+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
24832574
}
24842575
}
24852576

src/librustdoc/html/render/print_item.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use rustc_span::symbol::{kw, sym, Symbol};
1616
use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
1717

1818
use super::{
19-
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
20-
render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre,
21-
render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context,
22-
ImplRenderingParameters,
19+
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
20+
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
21+
render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
22+
AssocItemLink, Context, ImplRenderingParameters,
2323
};
2424
use crate::clean;
2525
use crate::formats::item_type::ItemType;
@@ -288,15 +288,15 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
288288
w.write_str(ITEM_TABLE_CLOSE);
289289
}
290290
curty = myty;
291-
let (short, name) = item_ty_to_strs(myty.unwrap());
291+
let sec = item_ty_to_section(myty.unwrap());
292292
write!(
293293
w,
294294
"<h2 id=\"{id}\" class=\"small-section-header\">\
295295
<a href=\"#{id}\">{name}</a>\
296296
</h2>\n{}",
297297
ITEM_TABLE_OPEN,
298-
id = cx.derive_id(short.to_owned()),
299-
name = name
298+
id = cx.derive_id(sec.id().to_owned()),
299+
name = sec.name(),
300300
);
301301
}
302302

0 commit comments

Comments
 (0)