about summary refs log tree commit diff
path: root/src/librustdoc/html/render
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2022-01-07 16:12:11 -0800
committerNoah Lev <camelidcamel@gmail.com>2022-02-09 11:39:12 -0800
commitfa400ace11f9db1e299fef3992d3301ccd985bf0 (patch)
tree7948c0d998a56fd73dfc85eb28d23fdfa05ac4b3 /src/librustdoc/html/render
parent427eba2f0bacdeaebc992a78eb2889564de7d7cf (diff)
downloadrust-fa400ace11f9db1e299fef3992d3301ccd985bf0.tar.gz
rust-fa400ace11f9db1e299fef3992d3301ccd985bf0.zip
rustdoc: Create enum for sections holding items
Diffstat (limited to 'src/librustdoc/html/render')
-rw-r--r--src/librustdoc/html/render/mod.rs151
-rw-r--r--src/librustdoc/html/render/print_item.rs14
2 files changed, 128 insertions, 37 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 32e4a829184..3fd94fb9dc5 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2409,33 +2409,124 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
     }
 }
 
-fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+enum ItemSection {
+    Reexports,
+    Modules,
+    Structs,
+    Unions,
+    Enums,
+    Functions,
+    TypeDefinitions,
+    Statics,
+    Constants,
+    Traits,
+    Implementations,
+    TypeMethods,
+    Methods,
+    StructFields,
+    Variants,
+    Macros,
+    PrimitiveTypes,
+    AssociatedTypes,
+    AssociatedConstants,
+    ForeignTypes,
+    Keywords,
+    OpaqueTypes,
+    AttributeMacros,
+    DeriveMacros,
+    TraitAliases,
+}
+
+impl ItemSection {
+    fn id(self) -> &'static str {
+        match self {
+            Self::Reexports => "reexports",
+            Self::Modules => "modules",
+            Self::Structs => "structs",
+            Self::Unions => "unions",
+            Self::Enums => "enums",
+            Self::Functions => "functions",
+            Self::TypeDefinitions => "types",
+            Self::Statics => "statics",
+            Self::Constants => "constants",
+            Self::Traits => "traits",
+            Self::Implementations => "impls",
+            Self::TypeMethods => "tymethods",
+            Self::Methods => "methods",
+            Self::StructFields => "fields",
+            Self::Variants => "variants",
+            Self::Macros => "macros",
+            Self::PrimitiveTypes => "primitives",
+            Self::AssociatedTypes => "associated-types",
+            Self::AssociatedConstants => "associated-consts",
+            Self::ForeignTypes => "foreign-types",
+            Self::Keywords => "keywords",
+            Self::OpaqueTypes => "opaque-types",
+            Self::AttributeMacros => "attributes",
+            Self::DeriveMacros => "derives",
+            Self::TraitAliases => "trait-aliases",
+        }
+    }
+
+    fn name(self) -> &'static str {
+        match self {
+            Self::Reexports => "Re-exports",
+            Self::Modules => "Modules",
+            Self::Structs => "Structs",
+            Self::Unions => "Unions",
+            Self::Enums => "Enums",
+            Self::Functions => "Functions",
+            Self::TypeDefinitions => "Type Definitions",
+            Self::Statics => "Statics",
+            Self::Constants => "Constants",
+            Self::Traits => "Traits",
+            Self::Implementations => "Implementations",
+            Self::TypeMethods => "Type Methods",
+            Self::Methods => "Methods",
+            Self::StructFields => "Struct Fields",
+            Self::Variants => "Variants",
+            Self::Macros => "Macros",
+            Self::PrimitiveTypes => "Primitive Types",
+            Self::AssociatedTypes => "Associated Types",
+            Self::AssociatedConstants => "Associated Constants",
+            Self::ForeignTypes => "Foreign Types",
+            Self::Keywords => "Keywords",
+            Self::OpaqueTypes => "Opaque Types",
+            Self::AttributeMacros => "Attribute Macros",
+            Self::DeriveMacros => "Derive Macros",
+            Self::TraitAliases => "Trait aliases",
+        }
+    }
+}
+
+fn item_ty_to_section(ty: ItemType) -> ItemSection {
     match ty {
-        ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
-        ItemType::Module => ("modules", "Modules"),
-        ItemType::Struct => ("structs", "Structs"),
-        ItemType::Union => ("unions", "Unions"),
-        ItemType::Enum => ("enums", "Enums"),
-        ItemType::Function => ("functions", "Functions"),
-        ItemType::Typedef => ("types", "Type Definitions"),
-        ItemType::Static => ("statics", "Statics"),
-        ItemType::Constant => ("constants", "Constants"),
-        ItemType::Trait => ("traits", "Traits"),
-        ItemType::Impl => ("impls", "Implementations"),
-        ItemType::TyMethod => ("tymethods", "Type Methods"),
-        ItemType::Method => ("methods", "Methods"),
-        ItemType::StructField => ("fields", "Struct Fields"),
-        ItemType::Variant => ("variants", "Variants"),
-        ItemType::Macro => ("macros", "Macros"),
-        ItemType::Primitive => ("primitives", "Primitive Types"),
-        ItemType::AssocType => ("associated-types", "Associated Types"),
-        ItemType::AssocConst => ("associated-consts", "Associated Constants"),
-        ItemType::ForeignType => ("foreign-types", "Foreign Types"),
-        ItemType::Keyword => ("keywords", "Keywords"),
-        ItemType::OpaqueTy => ("opaque-types", "Opaque Types"),
-        ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
-        ItemType::ProcDerive => ("derives", "Derive Macros"),
-        ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
+        ItemType::ExternCrate | ItemType::Import => ItemSection::Reexports,
+        ItemType::Module => ItemSection::Modules,
+        ItemType::Struct => ItemSection::Structs,
+        ItemType::Union => ItemSection::Unions,
+        ItemType::Enum => ItemSection::Enums,
+        ItemType::Function => ItemSection::Functions,
+        ItemType::Typedef => ItemSection::TypeDefinitions,
+        ItemType::Static => ItemSection::Statics,
+        ItemType::Constant => ItemSection::Constants,
+        ItemType::Trait => ItemSection::Traits,
+        ItemType::Impl => ItemSection::Implementations,
+        ItemType::TyMethod => ItemSection::TypeMethods,
+        ItemType::Method => ItemSection::Methods,
+        ItemType::StructField => ItemSection::StructFields,
+        ItemType::Variant => ItemSection::Variants,
+        ItemType::Macro => ItemSection::Macros,
+        ItemType::Primitive => ItemSection::PrimitiveTypes,
+        ItemType::AssocType => ItemSection::AssociatedTypes,
+        ItemType::AssocConst => ItemSection::AssociatedConstants,
+        ItemType::ForeignType => ItemSection::ForeignTypes,
+        ItemType::Keyword => ItemSection::Keywords,
+        ItemType::OpaqueTy => ItemSection::OpaqueTypes,
+        ItemType::ProcAttribute => ItemSection::AttributeMacros,
+        ItemType::ProcDerive => ItemSection::DeriveMacros,
+        ItemType::TraitAlias => ItemSection::TraitAliases,
         ItemType::Generic => unreachable!(),
     }
 }
@@ -2449,8 +2540,8 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
             && (it.type_() == ItemType::ExternCrate
                 || (it.type_() == ItemType::Import && !it.is_stripped()))
     }) {
-        let (id, name) = item_ty_to_strs(ItemType::Import);
-        sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
+        let sec = item_ty_to_section(ItemType::Import);
+        sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
     }
 
     // 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]) {
         ItemType::Keyword,
     ] {
         if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
-            let (id, name) = item_ty_to_strs(myty);
-            sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
+            let sec = item_ty_to_section(myty);
+            sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
         }
     }
 
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index f2c111495ed..d73bc658f9f 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -16,10 +16,10 @@ use rustc_span::symbol::{kw, sym, Symbol};
 use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
 
 use super::{
-    collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
-    render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre,
-    render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context,
-    ImplRenderingParameters,
+    collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
+    notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
+    render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
+    AssocItemLink, Context, ImplRenderingParameters,
 };
 use crate::clean;
 use crate::formats::item_type::ItemType;
@@ -288,15 +288,15 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
                 w.write_str(ITEM_TABLE_CLOSE);
             }
             curty = myty;
-            let (short, name) = item_ty_to_strs(myty.unwrap());
+            let sec = item_ty_to_section(myty.unwrap());
             write!(
                 w,
                 "<h2 id=\"{id}\" class=\"small-section-header\">\
                     <a href=\"#{id}\">{name}</a>\
                  </h2>\n{}",
                 ITEM_TABLE_OPEN,
-                id = cx.derive_id(short.to_owned()),
-                name = name
+                id = cx.derive_id(sec.id().to_owned()),
+                name = sec.name(),
             );
         }