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 display(&self) -> String {
47        let mut path_segments = self.path.clone();
48        path_segments.push(self.name.clone());
49
50        path_segments.join("::")
51    }
52
53    pub fn url_path(&self) -> String {
54        let name = &self.name;
55        let mut path_components = self.path.clone();
56
57        match self.kind {
58            RustdocItemKind::Mod => {
59                path_components.push(name.clone());
60                path_components.push("index.html".into());
61            }
62            RustdocItemKind::Macro
63            | RustdocItemKind::Struct
64            | RustdocItemKind::Enum
65            | RustdocItemKind::Constant
66            | RustdocItemKind::Trait
67            | RustdocItemKind::Function
68            | RustdocItemKind::TypeAlias
69            | RustdocItemKind::AttributeMacro
70            | RustdocItemKind::DeriveMacro => {
71                path_components
72                    .push(format!("{kind}.{name}.html", kind = self.kind.class()).into());
73            }
74        }
75
76        path_components.join("/")
77    }
78}