rustdoc/json/
import_finder.rs

1use rustc_hir::def_id::DefIdSet;
2
3use crate::clean::{self, Import, ImportSource, Item};
4use crate::fold::DocFolder;
5
6/// Get the id's of all items that are `pub use`d in the crate.
7///
8/// We need this to know if a stripped module is `pub use mod::*`, to decide
9/// if it needs to be kept in the index, despite being stripped.
10///
11/// See [#100973](https://github.com/rust-lang/rust/issues/100973) and
12/// [#101103](https://github.com/rust-lang/rust/issues/101103) for times when
13/// this information is needed.
14pub(crate) fn get_imports(krate: clean::Crate) -> (clean::Crate, DefIdSet) {
15    let mut finder = ImportFinder::default();
16    let krate = finder.fold_crate(krate);
17    (krate, finder.imported)
18}
19
20#[derive(Default)]
21struct ImportFinder {
22    imported: DefIdSet,
23}
24
25impl DocFolder for ImportFinder {
26    fn fold_item(&mut self, i: Item) -> Option<Item> {
27        match i.kind {
28            clean::ImportItem(Import { source: ImportSource { did: Some(did), .. }, .. }) => {
29                self.imported.insert(did);
30                Some(i)
31            }
32
33            _ => Some(self.fold_item_recur(i)),
34        }
35    }
36}