#[derive(Debug, Default)]
pub struct Path(Vec<&'static str>);
impl Path {
pub fn new() -> Self {
Self(vec![])
}
pub fn as_slice(&self) -> &[&'static str] {
&self.0
}
pub fn with(&mut self, name: &'static str) -> PathGuard<'_> {
PathGuard::new(self, name)
}
}
impl From<Vec<&'static str>> for Path {
fn from(segments: Vec<&'static str>) -> Self {
Self(segments)
}
}
pub struct PathGuard<'a> {
path: &'a mut Path,
}
impl<'a> PathGuard<'a> {
fn new(path: &'a mut Path, name: &'static str) -> Self {
path.0.push(name);
Self { path }
}
pub fn with(&mut self, name: &'static str) -> PathGuard<'_> {
PathGuard::new(self.path, name)
}
}
impl Drop for PathGuard<'_> {
fn drop(&mut self) {
self.path.0.pop();
}
}
impl std::ops::Deref for PathGuard<'_> {
type Target = Path;
fn deref(&self) -> &Self::Target {
self.path
}
}
impl AsRef<Path> for PathGuard<'_> {
fn as_ref(&self) -> &Path {
self.path
}
}
impl AsMut<Path> for PathGuard<'_> {
fn as_mut(&mut self) -> &mut Path {
self.path
}
}