1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Augment an iterator over nodes with something that tracks the full
//! path of the files involved.
//!
//! Unfortunately, Rust's Iter does not tie any lifetimes between the
//! iterator and the result of iteration (which is usually good). This
//! makes it difficult to avoid computing these paths, however.
//!
//! If this becomes a performance bottleneck, we can come up with something
//! more complicated that avoids computing (and allocating) the result
//! paths for each node encountered.
use crate::;
use ;
/*
pub trait PathTrack: Sized {
fn into_tracker(self, root: &str) -> PathTracker<Self>;
}
impl<I: Iterator<Item = Result<SureNode>>> PathTrack for I {
fn into_tracker(self, root: &str) -> PathTracker<I> {
PathTracker {
iter: self,
root: Some(root.to_owned()),
dirs: vec![],
}
}
}
pub struct PathTracker<I> {
iter: I,
root: Option<String>,
dirs: Vec<String>,
}
#[derive(Debug)]
pub struct PathedNode {
pub node: SureNode,
pub path: Option<String>,
}
impl<I> Iterator for PathTracker<I>
where I: Iterator<Item = Result<SureNode>>,
{
type Item = Result<PathedNode>;
fn next(&mut self) -> Option<Result<PathedNode>> {
match self.iter.next() {
None => None,
Some(Err(e)) => Some(Err(e)),
Some(Ok(node)) => {
let path = match &node {
SureNode::Enter { name, .. } => {
// Don't add the pseudo "__root__ flag.
if self.dirs.is_empty() && name == "__root__" {
let root = self.root.take().unwrap();
self.dirs.push(root);
} else {
self.dirs.push(name.clone());
}
Some(self.dirs.join("/"))
}
SureNode::File { name, .. } => {
self.dirs.push(name.clone());
Some(self.dirs.join("/"))
}
_ => None,
};
let do_pop = node.is_file() || node.is_leave();
let result = Some(Ok(PathedNode {
node: node,
path: path,
}));
if do_pop {
self.dirs.pop();
}
result
}
}
}
}
*/