|
3 | 3 | //! This library contains the tidy lints and exposes it |
4 | 4 | //! to be used by tools. |
5 | 5 |
|
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] |
12 | 14 | macro_rules! t { |
13 | 15 | ($e:expr, $p:expr) => { |
14 | 16 | match $e { |
@@ -53,59 +55,4 @@ pub mod target_specific_tests; |
53 | 55 | pub mod ui_tests; |
54 | 56 | pub mod unit_tests; |
55 | 57 | 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; |
0 commit comments