From d6ac8a48c000ec270bdaae093af33ff457d1e935 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Wed, 25 Feb 2026 18:38:32 +0100 Subject: [PATCH] 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 --- crates/xtask/src/format.rs | 31 +++---------------------------- crates/xtask/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/crates/xtask/src/format.rs b/crates/xtask/src/format.rs index d21f61701..88fd397c2 100644 --- a/crates/xtask/src/format.rs +++ b/crates/xtask/src/format.rs @@ -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, I: IntoIterator, M: Fn(&Path) -> bool>( - all_paths: I, - matcher: M, -) -> Vec { - 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, I: IntoIterator>( - all_paths: I, - extension: &str, -) -> Vec { - 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() { diff --git a/crates/xtask/src/lib.rs b/crates/xtask/src/lib.rs index c56ff4557..389b90eda 100644 --- a/crates/xtask/src/lib.rs +++ b/crates/xtask/src/lib.rs @@ -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(cargo_args: I) { Command::new(env!("CARGO")).args(cargo_args).run_or_fail(); } + +fn get_matching_files, I: IntoIterator, M: Fn(&Path) -> bool>( + all_paths: I, + matcher: M, +) -> Vec { + 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, I: IntoIterator>( + all_paths: I, + extension: &str, +) -> Vec { + let matcher = |p: &Path| p.extension().is_some_and(|e| e == extension); + get_matching_files(all_paths, matcher) +}