1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use syntax::ast;
use syntax::codemap;
use syntax::parse::parser;
use utils::contains_skip;
pub fn list_files<'a>(
krate: &'a ast::Crate,
codemap: &codemap::CodeMap,
) -> BTreeMap<PathBuf, &'a ast::Mod> {
let mut result = BTreeMap::new();
let root_filename: PathBuf = codemap.span_to_filename(krate.span).into();
list_submodules(
&krate.module,
root_filename.parent().unwrap(),
codemap,
&mut result,
);
result.insert(root_filename, &krate.module);
result
}
fn list_submodules<'a>(
module: &'a ast::Mod,
search_dir: &Path,
codemap: &codemap::CodeMap,
result: &mut BTreeMap<PathBuf, &'a ast::Mod>,
) {
debug!("list_submodules: search_dir: {:?}", search_dir);
for item in &module.items {
if let ast::ItemKind::Mod(ref sub_mod) = item.node {
if !contains_skip(&item.attrs) {
let is_internal =
codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
let dir_path = if is_internal {
search_dir.join(&item.ident.to_string())
} else {
let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap);
let dir_path = mod_path.parent().unwrap().to_owned();
result.insert(mod_path, sub_mod);
dir_path
};
list_submodules(sub_mod, &dir_path, codemap, result);
}
}
}
}
fn module_file(
id: ast::Ident,
attrs: &[ast::Attribute],
dir_path: &Path,
codemap: &codemap::CodeMap,
) -> PathBuf {
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
return path;
}
match parser::Parser::default_submod_path(id, dir_path, codemap).result {
Ok(parser::ModulePathSuccess { path, .. }) => path,
Err(_) => panic!("Couldn't find module {}", id),
}
}