• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Helper tools for intra doc links.
2 
3 const TYPES: ([&str; 9], [&str; 0]) =
4     (["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
5 const VALUES: ([&str; 8], [&str; 1]) =
6     (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
7 const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
8 
9 /// Extract the specified namespace from an intra-doc-link if one exists.
10 ///
11 /// # Examples
12 ///
13 /// * `struct MyStruct` -> ("MyStruct", `Namespace::Types`)
14 /// * `panic!` -> ("panic", `Namespace::Macros`)
15 /// * `fn@from_intra_spec` -> ("from_intra_spec", `Namespace::Values`)
parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>)16 pub(super) fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
17     let s = s.trim_matches('`');
18 
19     [
20         (hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
21         (hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
22         (hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
23     ]
24     .into_iter()
25     .find_map(|(ns, (mut prefixes, mut suffixes))| {
26         if let Some(prefix) = prefixes.find(|&&prefix| {
27             s.starts_with(prefix)
28                 && s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
29         }) {
30             Some((&s[prefix.len() + 1..], ns))
31         } else {
32             suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
33         }
34     })
35     .map_or((s, None), |(s, ns)| (s, Some(ns)))
36 }
37 
strip_prefixes_suffixes(s: &str) -> &str38 pub(super) fn strip_prefixes_suffixes(s: &str) -> &str {
39     [
40         (TYPES.0.iter(), TYPES.1.iter()),
41         (VALUES.0.iter(), VALUES.1.iter()),
42         (MACROS.0.iter(), MACROS.1.iter()),
43     ]
44     .into_iter()
45     .find_map(|(mut prefixes, mut suffixes)| {
46         if let Some(prefix) = prefixes.find(|&&prefix| {
47             s.starts_with(prefix)
48                 && s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
49         }) {
50             Some(&s[prefix.len() + 1..])
51         } else {
52             suffixes.find_map(|&suffix| s.strip_suffix(suffix))
53         }
54     })
55     .unwrap_or(s)
56 }
57 
58 #[cfg(test)]
59 mod tests {
60     use expect_test::{expect, Expect};
61 
62     use super::*;
63 
check(link: &str, expected: Expect)64     fn check(link: &str, expected: Expect) {
65         let (l, a) = parse_intra_doc_link(link);
66         let a = a.map_or_else(String::new, |a| format!(" ({a:?})"));
67         expected.assert_eq(&format!("{l}{a}"));
68     }
69 
70     #[test]
test_name()71     fn test_name() {
72         check("foo", expect![[r#"foo"#]]);
73         check("struct Struct", expect![[r#"Struct (Types)"#]]);
74         check("makro!", expect![[r#"makro (Macros)"#]]);
75         check("fn@function", expect![[r#"function (Values)"#]]);
76     }
77 }
78