use super::*;
use crate::common::TraversalType;
impl DocTree {
pub fn walk_to_root(mut self) -> Self {
self.tree = self.tree.walk_to_root();
self
}
pub fn walk(mut self, traversal_type: TraversalType) -> Self {
self.tree = self.tree.walk_to_root();
match traversal_type {
TraversalType::ID(id) => self.walk_to_node_with_id(id),
}
}
fn walk_to_node_with_id(mut self, id: NodeId) -> Self {
if id > self.node_count() {
panic!("No node with given ID. Computer says no...")
}
self.tree = match self.tree.walk_to_node_with_id(id) {
Ok(zipper) => zipper,
Err(zipper) => zipper,
};
self
}
fn walk_to_and_fro(self, to_id: NodeId, current_id: NodeId) -> Self {
if to_id > self.node_count() {
panic!("No node with given ID. Computer says no...")
}
todo!()
}
}
impl TreeZipper {
pub fn walk_to_node_with_id(mut self, id: NodeId) -> Result<Self, Self> {
if self.node_id() == id {
return Ok(self);
}
let n_of_children = if let Some(children) = self.shared_node().shared_children() {
self.n_of_children()
} else {
match self.focus_on_parent() {
Ok(zipper) | Err(zipper) => return Err(zipper),
}
};
for ind in 0..n_of_children {
self = if let Ok(child) = self.focus_on_child(ind) {
child
} else {
unreachable!("This should not happen with enumerated children...")
};
match self.walk_to_node_with_id(id) {
Ok(zipper) => return Ok(zipper),
Err(zipper) => {
self = zipper;
continue;
}
}
}
self = match self.focus_on_parent() {
Ok(zipper) | Err(zipper) => zipper,
};
Err(self)
}
}