• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use ignore::DirEntry;
2 
3 use std::{ffi::OsStr, fs::File, io::Read, path::Path};
4 
5 /// The default directory filter.
filter_dirs(path: &Path) -> bool6 pub fn filter_dirs(path: &Path) -> bool {
7     // FIXME: sync submodule exclusion list with rustfmt.toml
8     // bootstrap/etc
9     let skip = [
10         "tidy-test-file",
11         "compiler/rustc_codegen_cranelift",
12         "compiler/rustc_codegen_gcc",
13         "src/llvm-project",
14         "library/backtrace",
15         "library/portable-simd",
16         "library/stdarch",
17         "src/tools/cargo",
18         "src/tools/clippy",
19         "src/tools/miri",
20         "src/tools/rust-analyzer",
21         "src/tools/rustfmt",
22         "src/doc/book",
23         "src/doc/edition-guide",
24         "src/doc/embedded-book",
25         "src/doc/nomicon",
26         "src/doc/rust-by-example",
27         "src/doc/rustc-dev-guide",
28         "src/doc/reference",
29         // Filter RLS output directories
30         "target/rls",
31         "src/bootstrap/target",
32         "vendor",
33     ];
34     skip.iter().any(|p| path.ends_with(p))
35 }
36 
37 /// Filter for only files that end in `.rs`.
filter_not_rust(path: &Path) -> bool38 pub fn filter_not_rust(path: &Path) -> bool {
39     path.extension() != Some(OsStr::new("rs")) && !path.is_dir()
40 }
41 
walk( path: &Path, skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool, f: &mut dyn FnMut(&DirEntry, &str), )42 pub fn walk(
43     path: &Path,
44     skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
45     f: &mut dyn FnMut(&DirEntry, &str),
46 ) {
47     walk_many(&[path], skip, f);
48 }
49 
walk_many( paths: &[&Path], skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool, f: &mut dyn FnMut(&DirEntry, &str), )50 pub fn walk_many(
51     paths: &[&Path],
52     skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
53     f: &mut dyn FnMut(&DirEntry, &str),
54 ) {
55     let mut contents = Vec::new();
56     walk_no_read(paths, skip, &mut |entry| {
57         contents.clear();
58         let mut file = t!(File::open(entry.path()), entry.path());
59         t!(file.read_to_end(&mut contents), entry.path());
60         let contents_str = match std::str::from_utf8(&contents) {
61             Ok(s) => s,
62             Err(_) => return, // skip this file
63         };
64         f(&entry, &contents_str);
65     });
66 }
67 
walk_no_read( paths: &[&Path], skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool, f: &mut dyn FnMut(&DirEntry), )68 pub(crate) fn walk_no_read(
69     paths: &[&Path],
70     skip: impl Send + Sync + 'static + Fn(&Path, bool) -> bool,
71     f: &mut dyn FnMut(&DirEntry),
72 ) {
73     let mut walker = ignore::WalkBuilder::new(paths[0]);
74     for path in &paths[1..] {
75         walker.add(path);
76     }
77     let walker = walker.filter_entry(move |e| {
78         !skip(e.path(), e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
79     });
80     for entry in walker.build() {
81         if let Ok(entry) = entry {
82             if entry.file_type().map_or(true, |kind| kind.is_dir() || kind.is_symlink()) {
83                 continue;
84             }
85             f(&entry);
86         }
87     }
88 }
89