item.rs

 1use std::sync::Arc;
 2
 3use strum::EnumIter;
 4
 5#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, EnumIter)]
 6pub enum RustdocItemKind {
 7    Mod,
 8    Macro,
 9    Struct,
10    Enum,
11    Constant,
12    Trait,
13    Function,
14    TypeAlias,
15    AttributeMacro,
16    DeriveMacro,
17}
18
19impl RustdocItemKind {
20    pub(crate) const fn class(&self) -> &'static str {
21        match self {
22            Self::Mod => "mod",
23            Self::Macro => "macro",
24            Self::Struct => "struct",
25            Self::Enum => "enum",
26            Self::Constant => "constant",
27            Self::Trait => "trait",
28            Self::Function => "fn",
29            Self::TypeAlias => "type",
30            Self::AttributeMacro => "attr",
31            Self::DeriveMacro => "derive",
32        }
33    }
34}
35
36#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
37pub struct RustdocItem {
38    pub kind: RustdocItemKind,
39    /// The item path, up until the name of the item.
40    pub path: Vec<Arc<str>>,
41    /// The name of the item.
42    pub name: Arc<str>,
43}
44
45impl RustdocItem {
46    pub fn url_path(&self) -> String {
47        let name = &self.name;
48        let mut path_components = self.path.clone();
49
50        match self.kind {
51            RustdocItemKind::Mod => {
52                path_components.push(name.clone());
53                path_components.push("index.html".into());
54            }
55            RustdocItemKind::Macro
56            | RustdocItemKind::Struct
57            | RustdocItemKind::Enum
58            | RustdocItemKind::Constant
59            | RustdocItemKind::Trait
60            | RustdocItemKind::Function
61            | RustdocItemKind::TypeAlias
62            | RustdocItemKind::AttributeMacro
63            | RustdocItemKind::DeriveMacro => {
64                path_components
65                    .push(format!("{kind}.{name}.html", kind = self.kind.class()).into());
66            }
67        }
68
69        path_components.join("/")
70    }
71}