use crate::error::{AppError, AppResult, ErrorKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ItemKind {
Module,
Struct,
Trait,
Enum,
Union,
Fn,
Type,
Constant,
Static,
Macro,
Attribute,
Derive,
}
impl ItemKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Module => "module",
Self::Struct => "struct",
Self::Trait => "trait",
Self::Enum => "enum",
Self::Union => "union",
Self::Fn => "fn",
Self::Type => "type",
Self::Constant => "constant",
Self::Static => "static",
Self::Macro => "macro",
Self::Attribute => "attribute",
Self::Derive => "derive",
}
}
pub fn file_prefix(self) -> &'static str {
match self {
Self::Module => "module",
Self::Struct => "struct",
Self::Trait => "trait",
Self::Enum => "enum",
Self::Union => "union",
Self::Fn => "fn",
Self::Type => "type",
Self::Constant => "constant",
Self::Static => "static",
Self::Macro => "macro",
Self::Attribute => "attr",
Self::Derive => "derive",
}
}
pub fn parse(input: &str) -> AppResult<Self> {
let s = input.trim().to_ascii_lowercase();
match s.as_str() {
"module" | "mod" => Ok(Self::Module),
"struct" => Ok(Self::Struct),
"trait" => Ok(Self::Trait),
"enum" => Ok(Self::Enum),
"union" => Ok(Self::Union),
"fn" | "function" | "method" => Ok(Self::Fn),
"type" => Ok(Self::Type),
"const" | "constant" => Ok(Self::Constant),
"static" => Ok(Self::Static),
"macro" => Ok(Self::Macro),
"attr" | "attribute" => Ok(Self::Attribute),
"derive" => Ok(Self::Derive),
other => Err(AppError::new(
ErrorKind::InvalidInput,
format!("unknown item type '{other}'"),
)),
}
}
pub fn from_href(href: &str) -> Option<Self> {
let file = href.rsplit('/').next().unwrap_or(href);
let file = file.split('#').next().unwrap_or(file);
let prefix = file.split('.').next().unwrap_or("");
match prefix {
"struct" => Some(Self::Struct),
"trait" => Some(Self::Trait),
"fn" => Some(Self::Fn),
"enum" => Some(Self::Enum),
"type" => Some(Self::Type),
"constant" | "const" => Some(Self::Constant),
"static" => Some(Self::Static),
"macro" => Some(Self::Macro),
"union" => Some(Self::Union),
"attr" => Some(Self::Attribute),
"derive" => Some(Self::Derive),
"index" => Some(Self::Module),
_ => {
if href.contains("#method.") {
Some(Self::Fn)
} else {
None
}
}
}
}
}
pub fn rustc_crate_name(pkg: &str) -> String {
pkg.replace('-', "_")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hyphen_to_underscore() {
assert_eq!(rustc_crate_name("async-trait"), "async_trait");
assert_eq!(rustc_crate_name("serde"), "serde");
}
#[test]
fn fn_aliases() {
assert_eq!(ItemKind::parse("fn").unwrap(), ItemKind::Fn);
assert_eq!(ItemKind::parse("function").unwrap(), ItemKind::Fn);
}
#[test]
fn href_classifier() {
assert_eq!(ItemKind::from_href("fn.foo.html"), Some(ItemKind::Fn));
assert_eq!(ItemKind::from_href("union.U.html"), Some(ItemKind::Union));
assert_eq!(
ItemKind::from_href("attr.async_trait.html"),
Some(ItemKind::Attribute)
);
assert_eq!(
ItemKind::from_href("derive.Serialize.html"),
Some(ItemKind::Derive)
);
assert_eq!(
ItemKind::from_href("de/trait.Deserialize.html"),
Some(ItemKind::Trait)
);
assert_eq!(ItemKind::from_href("unknown.html"), None);
}
#[test]
fn constant_prefix_modern_and_legacy() {
assert_eq!(ItemKind::file_prefix(ItemKind::Constant), "constant");
assert_eq!(
ItemKind::from_href("constant.MAX.html"),
Some(ItemKind::Constant)
);
assert_eq!(
ItemKind::from_href("constant._SC_OPEN_MAX.html"),
Some(ItemKind::Constant)
);
assert_eq!(
ItemKind::from_href("const.MAX.html"),
Some(ItemKind::Constant)
);
assert_eq!(ItemKind::parse("const").unwrap(), ItemKind::Constant);
assert_eq!(ItemKind::parse("constant").unwrap(), ItemKind::Constant);
assert_eq!(ItemKind::Constant.as_str(), "constant");
}
}