@@ -247,23 +247,16 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
247247 success. unwrap ( ) ;
248248}
249249
250- /// Browse the directory `path` non-recursively and return the first file path which starts with `prefix` and has the
251- /// file extension `extension `.
250+ /// Browse the directory `path` non-recursively and return all files which respect the parameters
251+ /// outlined by `closure `.
252252#[ track_caller]
253- pub fn find_files_by_prefix_and_extension < P : AsRef < Path > > (
254- path : P ,
255- prefix : & str ,
256- extension : & str ,
257- ) -> Vec < PathBuf > {
253+ pub fn find_files < P : AsRef < Path > , F : Fn ( & PathBuf ) -> bool > ( path : P , closure : F ) -> Vec < PathBuf > {
258254 let mut matching_files = Vec :: new ( ) ;
259255 for entry in fs_wrapper:: read_dir ( path) {
260256 let entry = entry. expect ( "failed to read directory entry." ) ;
261257 let path = entry. path ( ) ;
262258
263- if path. is_file ( )
264- && path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . starts_with ( prefix)
265- && path. extension ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) == extension
266- {
259+ if path. is_file ( ) && closure ( & path) {
267260 matching_files. push ( path) ;
268261 }
269262 }
@@ -275,6 +268,16 @@ pub fn find_files_by_prefix_and_extension<P: AsRef<Path>>(
275268 }
276269}
277270
271+ /// Returns true if the filename at `path` starts with `prefix`.
272+ pub fn has_prefix < P : AsRef < Path > > ( path : P , prefix : & str ) -> bool {
273+ path. as_ref ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . starts_with ( prefix)
274+ }
275+
276+ /// Returns true if the filename at `path` has the extension `extension`.
277+ pub fn has_extension < P : AsRef < Path > > ( path : P , extension : & str ) -> bool {
278+ path. as_ref ( ) . extension ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) == extension
279+ }
280+
278281/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
279282/// available on the platform!
280283#[ track_caller]
0 commit comments