Skip to content

Commit

Permalink
refcctor check_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
benluiwj committed Nov 25, 2024
1 parent e7ee88d commit b972394
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
43 changes: 27 additions & 16 deletions check_diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,24 +349,35 @@ pub fn compile_rustfmt(
});
}

pub fn check_diff(config: Option<Vec<String>>, runners: CheckDiffRunners) -> i32 {
fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
return WalkDir::new(repo)
.into_iter()
.filter_map(|e| e.ok())
.filter(|entry| {
let path = entry.path();
return path.is_file() && path.extension().map_or(false, |ext| ext == "rs");
})
.map(|entry| entry.into_path());
}

pub fn check_diff(config: Option<Vec<String>>, runners: CheckDiffRunners, repo: &Path) -> i32 {
let mut errors = 0;
for entry in WalkDir::new(".").into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "rs") {
match runners.create_diff(path, &config) {
Ok(diff) => {
if !diff.is_empty() {
eprint!("{diff}");
errors += 1;
}
}
Err(e) => {
eprintln!("Error creating diff for {:?}: {:?}", path.display(), e);
errors += 1;
}
search_for_rs_files(repo).for_each(|file| match runners.create_diff(file.as_path(), &config) {
Ok(diff) => {
if !diff.is_empty() {
eprint!("{diff}");
errors += 1;
}
}
}
Err(e) => {
eprintln!(
"Error creating diff for {:?}: {:?}",
file.as_path().display(),
e
);
errors += 1;
}
});

return errors;
}
3 changes: 2 additions & 1 deletion check_diff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ fn main() -> Result<(), CheckDiffError> {
args.commit_hash,
)?;

let _ = check_diff(args.rustfmt_config, check_diff_runners);
// TODO: currently using same tmp dir path for sake of compilation
let _ = check_diff(args.rustfmt_config, check_diff_runners, tmp_dir.path());

Ok(())
}

0 comments on commit b972394

Please sign in to comment.