use std::path::Path;
#[derive(Clone)]
pub struct RestorablePath<'a> {
path: &'a Path,
current_component_idx: u8,
is_using_relative_path: bool,
}
impl<'a> RestorablePath<'a> {
pub fn new(path: &'a Path) -> Self {
RestorablePath {
path,
current_component_idx: 0,
is_using_relative_path: !path.starts_with("/"),
}
}
pub(super) fn get_current_component(&self) -> Option<&Path> {
self.path
.iter()
.nth(self.current_component_idx as usize)
.map(Path::new)
}
pub(super) fn restore(&self) -> Self {
RestorablePath {
path: self.path,
current_component_idx: 0,
is_using_relative_path: self.is_using_relative_path,
}
}
pub(super) fn advance(&self) -> Self {
RestorablePath {
path: self.path,
current_component_idx: self.current_component_idx + 1,
is_using_relative_path: self.is_using_relative_path,
}
}
pub(super) fn strip_prefix(&mut self) {
self.path = self.path.strip_prefix("/").ok().unwrap_or(self.path);
}
pub(super) fn is_using_relative_path(&self) -> bool {
self.is_using_relative_path
}
}