xtasks: make files_with_extension more accessible

This function is useful beyond the formatting module, so put it into the
top level of the library.

Closes #12483
This commit is contained in:
Daniel Rainer
2026-02-25 18:38:32 +01:00
committed by Johannes Altmanninger
parent 2ba031677f
commit d6ac8a48c0
2 changed files with 37 additions and 29 deletions

View File

@@ -2,10 +2,11 @@
use clap::Args;
use std::{
io::{ErrorKind, Write},
path::{Path, PathBuf},
path::PathBuf,
process::{Command, Stdio},
};
use walkdir::WalkDir;
use crate::files_with_extension;
const GREEN: Style = AnsiColor::Green.on_default();
const YELLOW: Style = AnsiColor::Yellow.on_default();
@@ -67,32 +68,6 @@ pub fn format(args: FormatArgs) {
format_rust(&args);
}
fn get_matching_files<P: AsRef<Path>, I: IntoIterator<Item = P>, M: Fn(&Path) -> bool>(
all_paths: I,
matcher: M,
) -> Vec<PathBuf> {
all_paths
.into_iter()
.flat_map(WalkDir::new)
.filter_map(|res| {
let entry = res.unwrap();
let path = entry.path();
if entry.file_type().is_file() && matcher(path) {
Some(path.to_owned())
} else {
None
}
})
.collect()
}
fn files_with_extension<P: AsRef<Path>, I: IntoIterator<Item = P>>(
all_paths: I,
extension: &str,
) -> Vec<PathBuf> {
let matcher = |p: &Path| p.extension().is_some_and(|e| e == extension);
get_matching_files(all_paths, matcher)
}
fn run_formatter(formatter: &mut Command, name: &str) {
println!("=== Running {GREEN}{name}{GREEN:#}");
match formatter.status() {

View File

@@ -1,4 +1,10 @@
use std::{ffi::OsStr, process::Command};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
process::Command,
};
use walkdir::WalkDir;
macro_rules! fail {
($($arg:tt)+) => {{
@@ -35,3 +41,30 @@ pub fn cargo<I, S>(cargo_args: I)
{
Command::new(env!("CARGO")).args(cargo_args).run_or_fail();
}
fn get_matching_files<P: AsRef<Path>, I: IntoIterator<Item = P>, M: Fn(&Path) -> bool>(
all_paths: I,
matcher: M,
) -> Vec<PathBuf> {
all_paths
.into_iter()
.flat_map(WalkDir::new)
.filter_map(|res| {
let entry = res.unwrap();
let path = entry.path();
if entry.file_type().is_file() && matcher(path) {
Some(path.to_owned())
} else {
None
}
})
.collect()
}
fn files_with_extension<P: AsRef<Path>, I: IntoIterator<Item = P>>(
all_paths: I,
extension: &str,
) -> Vec<PathBuf> {
let matcher = |p: &Path| p.extension().is_some_and(|e| e == extension);
get_matching_files(all_paths, matcher)
}