use crate::{Node};
use alloc::vec::Vec;
#[derive(PartialEq, Eq, Debug)]
pub struct NavPath {
path: Vec<usize>,
}
impl NavPath {
pub fn new(path: Vec<usize>) -> Self { Self { path } }
pub fn to_navigator(&mut self) -> NavPathNavigator {
NavPathNavigator {
path: self,
index: 0
}
}
pub fn root(&mut self) -> bool {
self.path.len() == 1
}
pub fn pop(&mut self, n: usize) {
for _ in 0..n {
self.path.pop();
}
}
pub fn push(&mut self, index: usize) {
self.path.push(index);
}
pub fn offset(&mut self, n: isize) {
*self.path.last_mut().unwrap() = (*self.path.last().unwrap() as isize + n) as usize;
}
}
impl core::ops::Index<usize> for NavPath {
type Output = usize;
fn index(&self, index: usize) -> &Self::Output {
&self.path[index]
}
}
pub struct NavPathNavigator<'a> {
path: &'a mut NavPath,
index: usize,
}
impl<'a> NavPathNavigator<'a> {
pub fn next(&mut self) -> usize {
self.path.path[self.index]
}
pub fn here(&mut self) -> bool {
self.index == self.path.path.len() - 1
}
pub fn step(&mut self) -> NavPathNavigator {
NavPathNavigator { index: self.index + 1, path: self.path }
}
pub fn step_if_next(&mut self, required_next: usize) -> Option<NavPathNavigator> {
if self.next() == required_next {
Some(self.step())
} else {
None
}
}
}