@@ -247,23 +247,16 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
247
247
success. unwrap ( ) ;
248
248
}
249
249
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 `.
252
252
#[ 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 > {
258
254
let mut matching_files = Vec :: new ( ) ;
259
255
for entry in fs_wrapper:: read_dir ( path) {
260
256
let entry = entry. expect ( "failed to read directory entry." ) ;
261
257
let path = entry. path ( ) ;
262
258
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) {
267
260
matching_files. push ( path) ;
268
261
}
269
262
}
@@ -275,6 +268,16 @@ pub fn find_files_by_prefix_and_extension<P: AsRef<Path>>(
275
268
}
276
269
}
277
270
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
+
278
281
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
279
282
/// available on the platform!
280
283
#[ track_caller]
0 commit comments