Skip to content
Myr

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

1 file+79 added−1 removed

modified:crates/ignore/src/walk.rs+79 added-1 removed
547 unmodified lines
548 /// 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 }554555 /// 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 lines
572 global_gitignores_relative_to: OnceLock::new(),573 }574 }575576 /// 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 }590591 /// 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 lines
1080 pub fn new<P: AsRef<Path>>(path: P) -> Walk {1081 WalkBuilder::new(path).build()1082 }10831084 /// 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 }10941095 fn skip_entry(&self, ent: &DirEntry) -> Result<bool, Error> {1096 if ent.depth() == 0 {1097 return Ok(false);1098 }
1460 unmodified lines
2559 .filter_entry(|entry| entry.file_name() != OsStr::new("a")),2560 &["x", "x/y", "x/y/foo"],2561 );2562 }25632564 #[test]2565 fn empty() {2566 let td = tmpdir();2567 assert_paths(td.path(), &WalkBuilder::empty(), &[]);25682569 let empty_paths: Vec<&OsStr> = Vec::new();2570 assert_paths(td.path(), &WalkBuilder::from_iter(empty_paths), &[]);2571 }25722573 #[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"), "");25822583 let paths = vec![2584 td.path().join("a"),2585 td.path().join("d"),2586 td.path().join("x"),2587 ];25882589 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}