Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stop_descent method #176

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,45 @@ impl IntoIter {
}
}

/// Stops the walk from descending into subdirectories, but continues
/// to traverse the current directory.
///
/// Both `stop_descent` and `skip_current_dir` affect the behavior
/// of the directory traversal in a way that prevents the walk
/// from descending into subdirectories. However, they differ in how
/// they handle the current directory:
///
/// * `stop_descent` continues to traverse the current directory,
/// while preventing the walk from descending into subdirectories.
/// * `skip_current_dir` skips over the current directory entirely,
/// including its contents, and continues the walk in its parent directory.
///
/// Note that this method has the same ergonomics issues
/// as skip_current_dir` since it borrows the iterator mutably:
///
/// ```no_run
/// use std::sync::mpsc;
/// use walkdir::WalkDir;
///
/// fn calc_dir_size(rx: mpsc::Receiver<()>) -> std::io::Result<u64> {
/// let mut sum = 0;
/// let mut it = WalkDir::new("foo").into_iter();
/// while let Some(entry) = it.next() {
/// if let Ok(stop) = rx.try_recv() {
/// it.stop_descent()
/// }
/// let entry = entry?;
/// if entry.file_type().is_file() {
/// sum += entry.metadata()?.len();
/// }
/// }
/// Ok(sum)
/// }
/// ```
pub fn stop_descent(&mut self) {
self.opts.max_depth = self.depth;
}

/// Yields only entries which satisfy the given predicate and skips
/// descending into directories that do not satisfy the given predicate.
///
Expand Down
29 changes: 29 additions & 0 deletions src/tests/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,35 @@ fn skip_current_dir() {
assert_eq!(expected, paths);
}

#[test]
fn stop_descent() {
let dir = Dir::tmp();
dir.mkdirp("foo/a");
dir.mkdirp("foo/b");
dir.mkdirp("foo/sub");
dir.touch("foo/sub/bar");

let mut paths = vec![];
let mut it = WalkDir::new(dir.path()).into_iter();
while let Some(result) = it.next() {
let ent = result.unwrap();
paths.push(ent.path().to_path_buf());
if ent.file_name() == "sub" {
it.stop_descent();
}
}
paths.sort();

let expected = vec![
dir.path().to_path_buf(),
dir.join("foo"),
dir.join("foo").join("a"),
dir.join("foo").join("b"),
dir.join("foo").join("sub"),
];
assert_eq!(expected, paths);
}

#[test]
fn filter_entry() {
let dir = Dir::tmp();
Expand Down