ignore/walk: add `WalkBuilder::empty` and `WalkBuilder::from_iter`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #1761, Closes #3261
authored by Sam Altier · committed by Andrew Gallant · parent 8bca2eb · Browse files · 1 file·+79 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+79 added−1 removed
- other:+79 added−1 removed
Provenance
- Commit
b6849da- Tree
aed6186- Author
Sam Altier <sam@altier.me>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
8bca2eb
1 file changed+79 added−1 removed
modified:crates/ignore/src/walk.rs+79 added-1 removed
547 unmodified lines548 /// Note that if you want to traverse multiple different directories, it549 /// is better to call `add` on this builder than to create multiple550 /// `Walk` values.551 pub fn new<P: AsRef<Path>>(path: P) -> WalkBuilder {552+ WalkBuilder::from_iter([path])553+ }554+555+ /// Create an empty builder to which paths can be added.556+ ///557+ /// Note that if you call `build` on this instance before calling `add`558+ /// on it, it will return exactly zero items during iteration.559+ pub fn empty() -> WalkBuilder {560 WalkBuilder {553- paths: vec![path.as_ref().to_path_buf()],561+ paths: vec![],562 ig_builder: IgnoreBuilder::new(),563 max_depth: None,564 min_depth: None,565 max_filesize: None,6 unmodified lines572 global_gitignores_relative_to: OnceLock::new(),573 }574 }575 576+ /// Create a new builder for a recursive directory iterator from the577+ /// sequence of paths.578+ ///579+ /// Note that if the iterator is empty, this is the same as580+ /// `WalkBuilder::empty`.581+ pub fn from_iter<P: AsRef<Path>, I: IntoIterator<Item = P>>(582+ paths: I,583+ ) -> WalkBuilder {584+ let mut builder = WalkBuilder::empty();585+ for path in paths.into_iter() {586+ builder.add(path);587+ }588+ builder589+ }590+591 /// Build a new `Walk` iterator.592 pub fn build(&self) -> Walk {593 let follow_links = self.follow_links;594 let max_depth = self.max_depth;485 unmodified lines1080 pub fn new<P: AsRef<Path>>(path: P) -> Walk {1081 WalkBuilder::new(path).build()1082 }1083 1084+ /// Create a new recursive directory iterator from the sequence of paths1085+ /// given.1086+ ///1087+ /// Note that if the provided iterator is empty, then `Walk` is guaranteed1088+ /// to yield zero entries.1089+ pub fn from_iter<P: AsRef<Path>, I: IntoIterator<Item = P>>(1090+ paths: I,1091+ ) -> Walk {1092+ WalkBuilder::from_iter(paths).build()1093+ }1094+1095 fn skip_entry(&self, ent: &DirEntry) -> Result<bool, Error> {1096 if ent.depth() == 0 {1097 return Ok(false);1098 }1460 unmodified lines2559 .filter_entry(|entry| entry.file_name() != OsStr::new("a")),2560 &["x", "x/y", "x/y/foo"],2561 );2562 }2563+2564+ #[test]2565+ fn empty() {2566+ let td = tmpdir();2567+ assert_paths(td.path(), &WalkBuilder::empty(), &[]);2568+2569+ let empty_paths: Vec<&OsStr> = Vec::new();2570+ assert_paths(td.path(), &WalkBuilder::from_iter(empty_paths), &[]);2571+ }2572+2573+ #[test]2574+ fn from_iter() {2575+ let td = tmpdir();2576+ mkdirp(td.path().join("a/b/c"));2577+ mkdirp(td.path().join("d/e/f"));2578+ mkdirp(td.path().join("x/y"));2579+ wfile(td.path().join("a/b/foo"), "");2580+ wfile(td.path().join("d/e/f/foo"), "");2581+ wfile(td.path().join("x/y/foo"), "");2582+2583+ let paths = vec![2584+ td.path().join("a"),2585+ td.path().join("d"),2586+ td.path().join("x"),2587+ ];2588+2589+ assert_paths(2590+ td.path(),2591+ &WalkBuilder::from_iter(paths),2592+ &[2593+ "x",2594+ "x/y",2595+ "x/y/foo",2596+ "d",2597+ "d/e",2598+ "d/e/f",2599+ "d/e/f/foo",2600+ "a",2601+ "a/b",2602+ "a/b/foo",2603+ "a/b/c",2604+ ],2605+ );2606+ }2607 }