1use crate::error::{AppError, AppResult, ErrorKind};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum ItemKind {
9 Module,
11 Struct,
13 Trait,
15 Enum,
17 Union,
19 Fn,
21 Type,
23 Constant,
25 Static,
27 Macro,
29 Attribute,
31 Derive,
33}
34
35impl ItemKind {
36 pub fn as_str(self) -> &'static str {
38 match self {
39 Self::Module => "module",
40 Self::Struct => "struct",
41 Self::Trait => "trait",
42 Self::Enum => "enum",
43 Self::Union => "union",
44 Self::Fn => "fn",
45 Self::Type => "type",
46 Self::Constant => "constant",
47 Self::Static => "static",
48 Self::Macro => "macro",
49 Self::Attribute => "attribute",
50 Self::Derive => "derive",
51 }
52 }
53
54 pub fn file_prefix(self) -> &'static str {
58 match self {
59 Self::Module => "module",
60 Self::Struct => "struct",
61 Self::Trait => "trait",
62 Self::Enum => "enum",
63 Self::Union => "union",
64 Self::Fn => "fn",
65 Self::Type => "type",
66 Self::Constant => "constant",
67 Self::Static => "static",
68 Self::Macro => "macro",
69 Self::Attribute => "attr",
70 Self::Derive => "derive",
71 }
72 }
73
74 pub fn parse(input: &str) -> AppResult<Self> {
80 Ok(Self::parse_with_echo(input)?.0)
81 }
82
83 pub fn parse_with_echo(input: &str) -> AppResult<(Self, &'static str)> {
91 let s = input.trim().to_ascii_lowercase();
92 match s.as_str() {
93 "module" | "mod" => Ok((Self::Module, "module")),
94 "struct" => Ok((Self::Struct, "struct")),
95 "trait" => Ok((Self::Trait, "trait")),
96 "enum" => Ok((Self::Enum, "enum")),
97 "union" => Ok((Self::Union, "union")),
98 "fn" | "function" => Ok((Self::Fn, "fn")),
99 "method" => Ok((Self::Fn, "method")),
100 "type" => Ok((Self::Type, "type")),
101 "const" | "constant" => Ok((Self::Constant, "constant")),
102 "static" => Ok((Self::Static, "static")),
103 "macro" => Ok((Self::Macro, "macro")),
104 "attr" | "attribute" => Ok((Self::Attribute, "attribute")),
105 "derive" => Ok((Self::Derive, "derive")),
106 other => Err(AppError::new(
107 ErrorKind::InvalidInput,
108 format!("unknown item type '{other}'"),
109 )),
110 }
111 }
112
113 pub fn from_href(href: &str) -> Option<Self> {
117 let file = href.rsplit('/').next().unwrap_or(href);
118 let file = file.split('#').next().unwrap_or(file);
119 let prefix = file.split('.').next().unwrap_or("");
120 match prefix {
121 "struct" => Some(Self::Struct),
122 "trait" => Some(Self::Trait),
123 "fn" => Some(Self::Fn),
124 "enum" => Some(Self::Enum),
125 "type" => Some(Self::Type),
126 "constant" | "const" => Some(Self::Constant),
128 "static" => Some(Self::Static),
129 "macro" => Some(Self::Macro),
130 "union" => Some(Self::Union),
131 "attr" => Some(Self::Attribute),
132 "derive" => Some(Self::Derive),
133 "index" => Some(Self::Module),
135 _ => {
136 if href.contains("#method.") {
138 Some(Self::Fn)
139 } else {
140 None
141 }
142 }
143 }
144 }
145}
146
147pub fn rustc_crate_name(pkg: &str) -> String {
149 pkg.replace('-', "_")
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn hyphen_to_underscore() {
158 assert_eq!(rustc_crate_name("async-trait"), "async_trait");
159 assert_eq!(rustc_crate_name("serde"), "serde");
160 }
161
162 #[test]
163 fn fn_aliases() {
164 assert_eq!(ItemKind::parse("fn").unwrap(), ItemKind::Fn);
165 assert_eq!(ItemKind::parse("function").unwrap(), ItemKind::Fn);
166 assert_eq!(ItemKind::parse("method").unwrap(), ItemKind::Fn);
167 let (k, echo) = ItemKind::parse_with_echo("method").unwrap();
168 assert_eq!(k, ItemKind::Fn);
169 assert_eq!(echo, "method");
170 let (_, echo) = ItemKind::parse_with_echo("fn").unwrap();
171 assert_eq!(echo, "fn");
172 }
173
174 #[test]
175 fn href_classifier() {
176 assert_eq!(ItemKind::from_href("fn.foo.html"), Some(ItemKind::Fn));
177 assert_eq!(ItemKind::from_href("union.U.html"), Some(ItemKind::Union));
178 assert_eq!(
179 ItemKind::from_href("attr.async_trait.html"),
180 Some(ItemKind::Attribute)
181 );
182 assert_eq!(
183 ItemKind::from_href("derive.Serialize.html"),
184 Some(ItemKind::Derive)
185 );
186 assert_eq!(
187 ItemKind::from_href("de/trait.Deserialize.html"),
188 Some(ItemKind::Trait)
189 );
190 assert_eq!(ItemKind::from_href("unknown.html"), None);
191 }
192
193 #[test]
194 fn constant_prefix_modern_and_legacy() {
195 assert_eq!(ItemKind::file_prefix(ItemKind::Constant), "constant");
196 assert_eq!(
197 ItemKind::from_href("constant.MAX.html"),
198 Some(ItemKind::Constant)
199 );
200 assert_eq!(
201 ItemKind::from_href("constant._SC_OPEN_MAX.html"),
202 Some(ItemKind::Constant)
203 );
204 assert_eq!(
206 ItemKind::from_href("const.MAX.html"),
207 Some(ItemKind::Constant)
208 );
209 assert_eq!(ItemKind::parse("const").unwrap(), ItemKind::Constant);
210 assert_eq!(ItemKind::parse("constant").unwrap(), ItemKind::Constant);
211 assert_eq!(ItemKind::Constant.as_str(), "constant");
212 }
213}