From fefac9f2fc1ec40f5c5db1a55b1496f3abe8591f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 17 Aug 2020 11:45:46 +0200 Subject: [PATCH] Move doc::ItemType::group_id to the parser module Previously, the doc::ItemType::group_id method returned the ID of the section for the item type in the HTML documentation files. This patch refactors the method into the get_item_group_id function in the parser module as it is an implementation detail of the HTML parser. --- src/parser/mod.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 3472285..88c7424 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -268,7 +268,7 @@ fn get_fields( ) -> anyhow::Result<(doc::ItemType, Vec)> { let ty = doc::ItemType::StructField; let mut fields = MemberDocs::new(parent, ty); - let heading = select_first(document, &format!("#{}", ty.group_id()))?; + let heading = select_first(document, &format!("#{}", get_item_group_id(ty)))?; let mut next = heading.as_ref().and_then(NodeRefExt::next_sibling_element); let mut name: Option = None; @@ -469,7 +469,7 @@ fn get_variants( ) -> anyhow::Result<(doc::ItemType, Vec)> { let ty = doc::ItemType::Variant; let mut variants = MemberDocs::new(parent, ty); - let heading = select_first(document, &format!("#{}", ty.group_id()))?; + let heading = select_first(document, &format!("#{}", get_item_group_id(ty)))?; let mut next = heading.as_ref().and_then(NodeRefExt::next_sibling_element); let mut name: Option = None; @@ -557,7 +557,7 @@ fn get_members( ty: doc::ItemType, ) -> anyhow::Result> { let mut members: Vec = Vec::new(); - if let Some(table) = select_first(document, &format!("#{} + table", ty.group_id()))? { + if let Some(table) = select_first(document, &format!("#{} + table", get_item_group_id(ty)))? { let items = select(table.as_node(), "td:first-child > :first-child")?; for item in items { let item_name = item.as_node().text_contents(); @@ -640,6 +640,39 @@ impl<'a> From> for Vec { } } +fn get_item_group_id(ty: doc::ItemType) -> &'static str { + use doc::ItemType; + + match ty { + ItemType::Module => "modules", + ItemType::ExternCrate => "extern-crates", + ItemType::Import => "imports", + ItemType::Struct => "structs", + ItemType::Enum => "enums", + ItemType::Function => "functions", + ItemType::Typedef => "types", + ItemType::Static => "statics", + ItemType::Trait => "traits", + ItemType::Impl => "impls", + ItemType::TyMethod => "required-methods", + ItemType::Method => "methods", + ItemType::StructField => "fields", + ItemType::Variant => "variants", + ItemType::Macro => "macros", + ItemType::Primitive => "primitives", + ItemType::AssocType => "associated-types", + ItemType::Constant => "constants", + ItemType::AssocConst => "associated-consts", + ItemType::Union => "unions", + ItemType::ForeignType => "foreign-types", + ItemType::Keyword => "keywords", + ItemType::OpaqueTy => "opaque-types", + ItemType::ProcAttribute => "proc-attributes", + ItemType::ProcDerive => "proc-derives", + ItemType::TraitAlias => "trait-aliases", + } +} + #[cfg(test)] mod tests { use crate::doc;