fast_walker/dir_walker/rev_iter/
mod.rs1use super::*;
2
3impl WalkPlan {
4 pub fn ancestors(&self) -> AncestorWalker {
5 AncestorWalker {
6 config: self,
7 tasks: self.check_list.iter().map(|s| WalkItem::from(s.as_path())).collect(),
8 results: self.check_list.iter().map(|s| WalkItem::from(s.as_path())).collect(),
9 }
10 }
11}
12
13pub struct AncestorWalker<'i> {
14 pub config: &'i WalkPlan,
15 pub tasks: VecDeque<WalkItem>,
16 pub results: Vec<WalkItem>,
17}
18
19impl<'i> AncestorWalker<'i> {
20 fn pop(&mut self) -> Option<WalkItem> {
21 if self.config.depth_first { self.tasks.pop_back() } else { self.tasks.pop_front() }
22 }
23 fn read_item(&mut self, entry: WalkItem) {
24 if (self.config.finish_when)(&entry) {
25 self.tasks.clear();
26 self.results.push(entry);
27 return;
28 }
29 self.read_directory(entry);
30 }
31 fn read_directory(&mut self, entry: WalkItem) {
32 if (self.config.ignore_when)(&entry) {
33 return;
34 }
35 match entry.path.parent() {
36 Some(dir) => {
37 let parent = WalkItem::from(dir).with_depth(entry.depth - 1);
38 self.results.push(parent.clone());
39 self.tasks.push_back(parent);
40 }
41 None => {}
42 }
43 }
44}
45
46impl<'i> Iterator for AncestorWalker<'i> {
47 type Item = WalkItem;
48
49 fn next(&mut self) -> Option<Self::Item> {
50 match self.results.pop() {
51 Some(s) => return Some(s),
52 None => match self.pop() {
53 Some(s) => {
54 self.read_item(s);
55 self.next()
56 }
57 None => None,
58 },
59 }
60 }
61}