]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/walk.rs
Rollup merge of #99880 - compiler-errors:escape-ascii-is-not-exact-size-iterator...
[rust.git] / src / tools / tidy / src / walk.rs
1 use std::fs::File;
2 use std::io::Read;
3 use walkdir::{DirEntry, WalkDir};
4
5 use std::path::Path;
6
7 pub fn filter_dirs(path: &Path) -> bool {
8     let skip = [
9         "tidy-test-file",
10         "compiler/rustc_codegen_cranelift",
11         "compiler/rustc_codegen_gcc",
12         "src/llvm-project",
13         "library/backtrace",
14         "library/portable-simd",
15         "library/stdarch",
16         "src/tools/cargo",
17         "src/tools/clippy",
18         "src/tools/miri",
19         "src/tools/rls",
20         "src/tools/rust-analyzer",
21         "src/tools/rust-installer",
22         "src/tools/rustfmt",
23         "src/doc/book",
24         "src/doc/edition-guide",
25         "src/doc/embedded-book",
26         "src/doc/nomicon",
27         "src/doc/rust-by-example",
28         "src/doc/rustc-dev-guide",
29         "src/doc/reference",
30         // Filter RLS output directories
31         "target/rls",
32         "src/bootstrap/target",
33     ];
34     skip.iter().any(|p| path.ends_with(p))
35 }
36
37 pub fn walk_many(
38     paths: &[&Path],
39     skip: &mut dyn FnMut(&Path) -> bool,
40     f: &mut dyn FnMut(&DirEntry, &str),
41 ) {
42     for path in paths {
43         walk(path, skip, f);
44     }
45 }
46
47 pub fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
48     let mut contents = String::new();
49     walk_no_read(path, skip, &mut |entry| {
50         contents.clear();
51         if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
52             contents.clear();
53         }
54         f(&entry, &contents);
55     });
56 }
57
58 pub(crate) fn walk_no_read(
59     path: &Path,
60     skip: &mut dyn FnMut(&Path) -> bool,
61     f: &mut dyn FnMut(&DirEntry),
62 ) {
63     let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
64     for entry in walker {
65         if let Ok(entry) = entry {
66             if entry.file_type().is_dir() {
67                 continue;
68             }
69             f(&entry);
70         }
71     }
72 }