use crate::prelude::Path;
use super::{depth::Traversal, TraversalNode};
pub struct Absolute<I, B> {
iter: I,
path: Path<B>,
}
impl<I, B> Absolute<I, B>
where
I: Iterator,
{
pub fn new(iter: I) -> Absolute<I, B> {
Absolute {
iter,
path: Path::new(),
}
}
}
impl<N, I, B> Iterator for Absolute<I, B>
where
I: Iterator<Item = Traversal<N, B>>,
B: Clone,
{
type Item = TraversalNode<N, B>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.iter.next() {
Some(match item {
Traversal::Start(data) => TraversalNode {
path: Path::new(),
data,
},
Traversal::Step { up, branch, data } => {
(0..up).for_each(|_| {
self.path.pop_last();
});
self.path.push_last(branch.clone());
TraversalNode {
path: self.path.clone(),
data,
}
}
})
} else {
None
}
}
}