Skip to content

Commit a2e2d76

Browse files
committed
tidy: move directory traversal utility functions into dedicated module
1 parent 4caedba commit a2e2d76

File tree

2 files changed

+74
-62
lines changed

2 files changed

+74
-62
lines changed

src/tools/tidy/src/lib.rs

+9-62
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
//! This library contains the tidy lints and exposes it
44
//! to be used by tools.
55
6-
use std::fs::File;
7-
use std::io::Read;
8-
use walkdir::{DirEntry, WalkDir};
9-
10-
use std::path::Path;
11-
6+
use walk::{filter_dirs, walk, walk_many, walk_no_read};
7+
8+
/// A helper macro to `unwrap` a result except also print out details like:
9+
///
10+
/// * The expression that failed
11+
/// * The error itself
12+
/// * (optionally) a path connected to the error (e.g. failure to open a file)
13+
#[macro_export]
1214
macro_rules! t {
1315
($e:expr, $p:expr) => {
1416
match $e {
@@ -53,59 +55,4 @@ pub mod target_specific_tests;
5355
pub mod ui_tests;
5456
pub mod unit_tests;
5557
pub mod unstable_book;
56-
57-
fn filter_dirs(path: &Path) -> bool {
58-
let skip = [
59-
"tidy-test-file",
60-
"compiler/rustc_codegen_cranelift",
61-
"compiler/rustc_codegen_gcc",
62-
"src/llvm-project",
63-
"library/backtrace",
64-
"library/portable-simd",
65-
"library/stdarch",
66-
"src/tools/cargo",
67-
"src/tools/clippy",
68-
"src/tools/miri",
69-
"src/tools/rls",
70-
"src/tools/rust-analyzer",
71-
"src/tools/rust-installer",
72-
"src/tools/rustfmt",
73-
"src/doc/book",
74-
// Filter RLS output directories
75-
"target/rls",
76-
];
77-
skip.iter().any(|p| path.ends_with(p))
78-
}
79-
80-
fn walk_many(
81-
paths: &[&Path],
82-
skip: &mut dyn FnMut(&Path) -> bool,
83-
f: &mut dyn FnMut(&DirEntry, &str),
84-
) {
85-
for path in paths {
86-
walk(path, skip, f);
87-
}
88-
}
89-
90-
fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
91-
let mut contents = String::new();
92-
walk_no_read(path, skip, &mut |entry| {
93-
contents.clear();
94-
if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
95-
contents.clear();
96-
}
97-
f(&entry, &contents);
98-
});
99-
}
100-
101-
fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
102-
let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
103-
for entry in walker {
104-
if let Ok(entry) = entry {
105-
if entry.file_type().is_dir() {
106-
continue;
107-
}
108-
f(&entry);
109-
}
110-
}
111-
}
58+
pub mod walk;

src/tools/tidy/src/walk.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// Filter RLS output directories
25+
"target/rls",
26+
];
27+
skip.iter().any(|p| path.ends_with(p))
28+
}
29+
30+
pub fn walk_many(
31+
paths: &[&Path],
32+
skip: &mut dyn FnMut(&Path) -> bool,
33+
f: &mut dyn FnMut(&DirEntry, &str),
34+
) {
35+
for path in paths {
36+
walk(path, skip, f);
37+
}
38+
}
39+
40+
pub fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
41+
let mut contents = String::new();
42+
walk_no_read(path, skip, &mut |entry| {
43+
contents.clear();
44+
if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
45+
contents.clear();
46+
}
47+
f(&entry, &contents);
48+
});
49+
}
50+
51+
pub(crate) fn walk_no_read(
52+
path: &Path,
53+
skip: &mut dyn FnMut(&Path) -> bool,
54+
f: &mut dyn FnMut(&DirEntry),
55+
) {
56+
let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
57+
for entry in walker {
58+
if let Ok(entry) = entry {
59+
if entry.file_type().is_dir() {
60+
continue;
61+
}
62+
f(&entry);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)