1use std::ffi::OsStr;
2use std::fs::File;
3use std::io::Read;
4use std::path::Path;
5
6use ignore::DirEntry;
7
8pub fn filter_dirs(path: &Path) -> bool {
10 let skip = [
12 "tidy-test-file",
13 "compiler/rustc_codegen_cranelift",
14 "compiler/rustc_codegen_gcc",
15 "src/llvm-project",
16 "library/backtrace",
17 "library/compiler-builtins",
18 "library/hashbrown",
19 "library/portable-simd",
20 "library/stdarch",
21 "src/tools/cargo",
22 "src/tools/clippy",
23 "src/tools/libcxx-version",
24 "src/tools/miri",
25 "src/tools/rust-analyzer",
26 "src/tools/rustc-perf",
27 "src/tools/rustfmt",
28 "src/tools/enzyme",
29 "src/tpde",
30 "src/doc/book",
31 "src/doc/edition-guide",
32 "src/doc/embedded-book",
33 "src/doc/nomicon",
34 "src/doc/rust-by-example",
35 "src/doc/rustc-dev-guide",
36 "src/doc/reference",
37 "src/gcc",
38 "src/bootstrap/target",
39 "vendor",
40 ];
41 skip.iter().any(|p| path.ends_with(p))
42}
43
44pub fn filter_not_rust(path: &Path) -> bool {
46 path.extension() != Some(OsStr::new("rs")) && !path.is_dir()
47}
48
49pub fn walk(
50 path: &Path,
51 skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
52 f: &mut dyn FnMut(&DirEntry, &str),
53) {
54 walk_many(&[path], skip, f);
55}
56
57pub fn walk_many(
58 paths: &[&Path],
59 skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
60 f: &mut dyn FnMut(&DirEntry, &str),
61) {
62 let mut contents = Vec::new();
63 walk_no_read(paths, skip, &mut |entry| {
64 contents.clear();
65 let mut file = t!(File::open(entry.path()), entry.path());
66 t!(file.read_to_end(&mut contents), entry.path());
67 let contents_str = match std::str::from_utf8(&contents) {
68 Ok(s) => s,
69 Err(_) => return, };
71 f(entry, contents_str);
72 });
73}
74
75pub(crate) fn walk_no_read(
76 paths: &[&Path],
77 skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
78 f: &mut dyn FnMut(&DirEntry),
79) {
80 let mut walker = ignore::WalkBuilder::new(paths[0]);
81 for path in &paths[1..] {
82 walker.add(path);
83 }
84 let walker = walker.filter_entry(move |e| {
85 !skip(e.path(), e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
86 });
87 for entry in walker.build().flatten() {
88 if entry.file_type().is_none_or(|kind| kind.is_dir() || kind.is_symlink()) {
89 continue;
90 }
91 f(&entry);
92 }
93}
94
95pub(crate) fn walk_dir(
97 path: &Path,
98 skip: impl Send + Sync + 'static + Fn(&Path) -> bool,
99 f: &mut dyn FnMut(&DirEntry),
100) {
101 let mut walker = ignore::WalkBuilder::new(path);
102 let walker = walker.filter_entry(move |e| !skip(e.path()));
103 for entry in walker.build().flatten() {
104 if entry.path().is_dir() {
105 f(&entry);
106 }
107 }
108}