Skip to content

Commit 461f4a8

Browse files
KimundiBurntSushi
authored andcommitted
api: add convenience sort routines
This commit adds sort_by_key and sort_by_file_name convenience routines for common sorting needs. The signature for sort_by_key in particular mirrors the eponymous method on slices in the standard library. Closes #147
1 parent 3d1f2f2 commit 461f4a8

3 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎src/error.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Error {
9898
///
9999
/// # Example
100100
///
101-
/// ```rust,no-run
101+
/// ```rust,no_run
102102
/// use std::io;
103103
/// use std::path::Path;
104104
///

‎src/lib.rs‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,14 @@ impl WalkDir {
377377
self
378378
}
379379

380-
/// Set a function for sorting directory entries.
380+
/// Set a function for sorting directory entries with a comparator
381+
/// function.
381382
///
382383
/// If a compare function is set, the resulting iterator will return all
383384
/// paths in sorted order. The compare function will be called to compare
384385
/// entries from the same directory.
385386
///
386-
/// ```rust,no-run
387+
/// ```rust,no_run
387388
/// use std::cmp;
388389
/// use std::ffi::OsString;
389390
/// use walkdir::WalkDir;
@@ -398,6 +399,41 @@ impl WalkDir {
398399
self
399400
}
400401

402+
/// Set a function for sorting directory entries with a key extraction
403+
/// function.
404+
///
405+
/// If a compare function is set, the resulting iterator will return all
406+
/// paths in sorted order. The compare function will be called to compare
407+
/// entries from the same directory.
408+
///
409+
/// ```rust,no_run
410+
/// use std::cmp;
411+
/// use std::ffi::OsString;
412+
/// use walkdir::WalkDir;
413+
///
414+
/// WalkDir::new("foo").sort_by_key(|a| a.file_name().to_owned());
415+
/// ```
416+
pub fn sort_by_key<K, F>(self, mut cmp: F) -> Self
417+
where
418+
F: FnMut(&DirEntry) -> K + Send + Sync + 'static,
419+
K: Ord,
420+
{
421+
self.sort_by(move |a, b| cmp(a).cmp(&cmp(b)))
422+
}
423+
424+
/// Sort directory entries by file name, to ensure a deterministic order.
425+
///
426+
/// This is a convenience function for calling `Self::sort_by()`.
427+
///
428+
/// ```rust,no_run
429+
/// use walkdir::WalkDir;
430+
///
431+
/// WalkDir::new("foo").sort_by_file_name();
432+
/// ```
433+
pub fn sort_by_file_name(self) -> Self {
434+
self.sort_by(|a, b| a.file_name().cmp(b.file_name()))
435+
}
436+
401437
/// Yield a directory's contents before the directory itself. By default,
402438
/// this is disabled.
403439
///

‎src/tests/recursive.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ fn filter_entry() {
879879
}
880880

881881
#[test]
882-
fn sort() {
882+
fn sort_by() {
883883
let dir = Dir::tmp();
884884
dir.mkdirp("foo/bar/baz/abc");
885885
dir.mkdirp("quux");
@@ -900,6 +900,49 @@ fn sort() {
900900
assert_eq!(expected, r.paths());
901901
}
902902

903+
#[test]
904+
fn sort_by_key() {
905+
let dir = Dir::tmp();
906+
dir.mkdirp("foo/bar/baz/abc");
907+
dir.mkdirp("quux");
908+
909+
let wd =
910+
WalkDir::new(dir.path()).sort_by_key(|a| a.file_name().to_owned());
911+
let r = dir.run_recursive(wd);
912+
r.assert_no_errors();
913+
914+
let expected = vec![
915+
dir.path().to_path_buf(),
916+
dir.join("foo"),
917+
dir.join("foo").join("bar"),
918+
dir.join("foo").join("bar").join("baz"),
919+
dir.join("foo").join("bar").join("baz").join("abc"),
920+
dir.join("quux"),
921+
];
922+
assert_eq!(expected, r.paths());
923+
}
924+
925+
#[test]
926+
fn sort_by_file_name() {
927+
let dir = Dir::tmp();
928+
dir.mkdirp("foo/bar/baz/abc");
929+
dir.mkdirp("quux");
930+
931+
let wd = WalkDir::new(dir.path()).sort_by_file_name();
932+
let r = dir.run_recursive(wd);
933+
r.assert_no_errors();
934+
935+
let expected = vec![
936+
dir.path().to_path_buf(),
937+
dir.join("foo"),
938+
dir.join("foo").join("bar"),
939+
dir.join("foo").join("bar").join("baz"),
940+
dir.join("foo").join("bar").join("baz").join("abc"),
941+
dir.join("quux"),
942+
];
943+
assert_eq!(expected, r.paths());
944+
}
945+
903946
#[test]
904947
fn sort_max_open() {
905948
let dir = Dir::tmp();

0 commit comments

Comments
 (0)