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.
This commit is contained in:
Robin Krahl 2020-08-17 11:45:46 +02:00
parent ebc9c58838
commit fefac9f2fc
No known key found for this signature in database
GPG Key ID: 8E9B0870524F69D8

View File

@ -268,7 +268,7 @@ fn get_fields(
) -> anyhow::Result<(doc::ItemType, Vec<doc::MemberGroup>)> { ) -> anyhow::Result<(doc::ItemType, Vec<doc::MemberGroup>)> {
let ty = doc::ItemType::StructField; let ty = doc::ItemType::StructField;
let mut fields = MemberDocs::new(parent, ty); 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 next = heading.as_ref().and_then(NodeRefExt::next_sibling_element);
let mut name: Option<String> = None; let mut name: Option<String> = None;
@ -469,7 +469,7 @@ fn get_variants(
) -> anyhow::Result<(doc::ItemType, Vec<doc::MemberGroup>)> { ) -> anyhow::Result<(doc::ItemType, Vec<doc::MemberGroup>)> {
let ty = doc::ItemType::Variant; let ty = doc::ItemType::Variant;
let mut variants = MemberDocs::new(parent, ty); 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 next = heading.as_ref().and_then(NodeRefExt::next_sibling_element);
let mut name: Option<String> = None; let mut name: Option<String> = None;
@ -557,7 +557,7 @@ fn get_members(
ty: doc::ItemType, ty: doc::ItemType,
) -> anyhow::Result<Vec<doc::Doc>> { ) -> anyhow::Result<Vec<doc::Doc>> {
let mut members: Vec<doc::Doc> = Vec::new(); let mut members: Vec<doc::Doc> = 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")?; let items = select(table.as_node(), "td:first-child > :first-child")?;
for item in items { for item in items {
let item_name = item.as_node().text_contents(); let item_name = item.as_node().text_contents();
@ -640,6 +640,39 @@ impl<'a> From<MemberDocs<'a>> for Vec<doc::Doc> {
} }
} }
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)] #[cfg(test)]
mod tests { mod tests {
use crate::doc; use crate::doc;