Skip to main content

soothfast_docs/surface/
counted.rs

1//! The items coverage counts: public items and inherent methods, once each
2//! however many `pub use` paths spell them.
3
4use std::collections::{BTreeMap, HashMap};
5
6use super::{ItemInfo, Surface};
7
8/// One distinct public item or inherent method.
9pub struct Counted<'a> {
10    /// The shortest spelling, for display.
11    pub path: String,
12    /// Every spelling, for lookups: a `covers=` tag or a bind may name any.
13    pub aliases: Vec<&'a String>,
14    /// What the surface records about the item.
15    pub info: &'a ItemInfo,
16}
17
18impl Surface {
19    /// Every item and every pub inherent method, deduplicated across `pub
20    /// use` aliases and in path order. Trait-impl methods are never indexed
21    /// and rustdoc omits `#[doc(hidden)]` items, so neither is counted.
22    /// Every top-level item kind counts, plus inherent methods; associated
23    /// consts do not.
24    pub fn counted(&self) -> Vec<Counted<'_>> {
25        let groups = self.grouped();
26        let mut owner: HashMap<&str, usize> = HashMap::new();
27        for (i, group) in groups.iter().enumerate() {
28            owner.extend(group.aliases.iter().map(|a| (a.as_str(), i)));
29        }
30        let mut methods: BTreeMap<(usize, &str), Vec<&String>> = BTreeMap::new();
31        for (path, info) in &self.members {
32            if info.kind != "function" {
33                continue;
34            }
35            let Some((ty, name)) = path.rsplit_once("::") else {
36                continue;
37            };
38            if let Some(&i) = owner.get(ty) {
39                methods.entry((i, name)).or_default().push(path);
40            }
41        }
42        let items = groups.iter().map(|g| Counted {
43            path: g.representative.clone(),
44            aliases: g.aliases.clone(),
45            info: &self.items[g.representative],
46        });
47        let methods = methods.into_iter().map(|((i, name), aliases)| Counted {
48            path: format!("{}::{name}", groups[i].representative),
49            info: &self.members[aliases[0]],
50            aliases,
51        });
52        let mut out: Vec<_> = items.chain(methods).collect();
53        out.sort_by(|a, b| a.path.cmp(&b.path));
54        out
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    fn info(kind: &str, fingerprint: u64) -> ItemInfo {
63        ItemInfo {
64            kind: kind.into(),
65            fingerprint,
66            signature: String::new(),
67            has_docs: false,
68            example: false,
69            spans: Vec::new(),
70        }
71    }
72
73    fn surface() -> Surface {
74        let mut s = Surface::default();
75        for path in ["a::Thing", "a::inner::Thing"] {
76            s.items.insert(path.into(), info("struct", 1));
77        }
78        s.items.insert("a::free".into(), info("function", 2));
79        for ty in ["a::Thing", "a::inner::Thing"] {
80            s.members.insert(format!("{ty}::new"), info("function", 3));
81            s.members.insert(format!("{ty}::MAX"), info("constant", 4));
82        }
83        s
84    }
85
86    fn paths(s: &Surface) -> Vec<String> {
87        s.counted().into_iter().map(|c| c.path).collect()
88    }
89
90    #[test]
91    fn an_inherent_method_counts_once_under_its_types_shortest_path() {
92        let surface = surface();
93        assert_eq!(paths(&surface), ["a::Thing", "a::Thing::new", "a::free"]);
94        let counted = surface.counted();
95        assert_eq!(counted[1].aliases.len(), 2);
96        assert_eq!(counted[1].info.kind, "function");
97    }
98
99    #[test]
100    fn same_text_methods_of_different_types_stay_apart() {
101        let mut s = Surface::default();
102        for (ty, fingerprint) in [("a::X", 10), ("a::Y", 11)] {
103            s.items.insert(ty.into(), info("struct", fingerprint));
104            s.members.insert(format!("{ty}::len"), info("function", 7));
105        }
106        assert_eq!(paths(&s), ["a::X", "a::X::len", "a::Y", "a::Y::len"]);
107    }
108
109    #[test]
110    fn a_member_of_an_unknown_type_is_not_counted() {
111        let mut s = Surface::default();
112        s.members.insert("a::Gone::new".into(), info("function", 3));
113        assert!(s.counted().is_empty());
114    }
115}