item.rs

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