use crate::item_kind::ItemKind;
pub const METHOD_ANCHOR_PREFIXES: &[&str] = &["method.", "tymethod."];
const TYPE_ANCHOR_PREFIXES: &[&str] = &["associatedtype."];
const CONSTANT_ANCHOR_PREFIXES: &[&str] = &["associatedconstant."];
const VARIANT_ANCHOR_PREFIXES: &[&str] = &["variant."];
const STRUCT_FIELD_ANCHOR_PREFIXES: &[&str] = &["structfield."];
pub const ASSOC_ANCHOR_MISS_PREFIX: &str = "associated item anchor ";
const METHOD_PARENT_KINDS: &[ItemKind] = &[
ItemKind::Struct,
ItemKind::Enum,
ItemKind::Trait,
ItemKind::Type,
ItemKind::Union,
];
const TYPE_PARENT_KINDS: &[ItemKind] = &[
ItemKind::Trait,
ItemKind::Struct,
ItemKind::Enum,
ItemKind::Union,
ItemKind::Type,
];
const CONSTANT_PARENT_KINDS: &[ItemKind] = &[
ItemKind::Struct,
ItemKind::Trait,
ItemKind::Enum,
ItemKind::Union,
ItemKind::Type,
];
const VARIANT_PARENT_KINDS: &[ItemKind] = &[ItemKind::Enum];
const STRUCT_FIELD_PARENT_KINDS: &[ItemKind] = &[ItemKind::Struct, ItemKind::Union];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssocAnchorKind {
Method,
Type,
Constant,
Variant,
StructField,
}
impl AssocAnchorKind {
pub fn anchor_prefixes(self) -> &'static [&'static str] {
match self {
Self::Method => METHOD_ANCHOR_PREFIXES,
Self::Type => TYPE_ANCHOR_PREFIXES,
Self::Constant => CONSTANT_ANCHOR_PREFIXES,
Self::Variant => VARIANT_ANCHOR_PREFIXES,
Self::StructField => STRUCT_FIELD_ANCHOR_PREFIXES,
}
}
pub fn primary_prefix(self) -> &'static str {
self.anchor_prefixes()[0]
}
pub fn parent_kind_probe(self) -> &'static [ItemKind] {
match self {
Self::Method => METHOD_PARENT_KINDS,
Self::Type => TYPE_PARENT_KINDS,
Self::Constant => CONSTANT_PARENT_KINDS,
Self::Variant => VARIANT_PARENT_KINDS,
Self::StructField => STRUCT_FIELD_PARENT_KINDS,
}
}
pub fn parent_kind_probe_names(self) -> impl Iterator<Item = &'static str> + Clone {
self.parent_kind_probe().iter().map(|k| k.as_str())
}
pub fn anchor_ids(self, name: &str) -> Vec<String> {
self.anchor_prefixes()
.iter()
.map(|prefix| format!("{prefix}{name}"))
.collect()
}
}
pub fn associated_item_path(kind: ItemKind, segs: &[String]) -> Option<AssocAnchorKind> {
let [.., parent, leaf] = segs else {
return None;
};
if !parent
.chars()
.next()
.is_some_and(|c| c.is_ascii_uppercase())
{
return None;
}
if leaf.is_empty() {
return None;
}
match kind {
ItemKind::Fn => Some(AssocAnchorKind::Method),
ItemKind::Type => Some(AssocAnchorKind::Type),
ItemKind::Constant => Some(AssocAnchorKind::Constant),
ItemKind::Variant => Some(AssocAnchorKind::Variant),
ItemKind::StructField => Some(AssocAnchorKind::StructField),
_ => None,
}
}
pub fn member_only_family(kind: ItemKind) -> Option<AssocAnchorKind> {
match kind {
ItemKind::Variant => Some(AssocAnchorKind::Variant),
ItemKind::StructField => Some(AssocAnchorKind::StructField),
_ => None,
}
}
pub fn assoc_from_fragment(fragment: &str) -> Option<(AssocAnchorKind, &str)> {
const FAMILIES: &[AssocAnchorKind] = &[
AssocAnchorKind::Method,
AssocAnchorKind::Type,
AssocAnchorKind::Constant,
AssocAnchorKind::Variant,
AssocAnchorKind::StructField,
];
FAMILIES.iter().find_map(|family| {
family
.anchor_prefixes()
.iter()
.find_map(|prefix| fragment.strip_prefix(prefix))
.filter(|name| !name.is_empty())
.map(|name| (*family, name))
})
}
pub fn strip_assoc_anchor_prefix<'a>(id: &'a str, prefixes: &[&str]) -> Option<&'a str> {
prefixes
.iter()
.find_map(|prefix| id.strip_prefix(prefix))
.filter(|name| !name.is_empty())
}