use std::ffi::OsStr;
use std::path::{Component, PathBuf};
pub(super) struct CanonicalPath<'path_buf> {
path: Option<&'path_buf mut PathBuf>,
#[cfg(racy_asserts)]
pub(super) debug: PathBuf,
}
impl<'path_buf> CanonicalPath<'path_buf> {
pub(super) fn new(path: Option<&'path_buf mut PathBuf>) -> Self {
Self {
#[cfg(racy_asserts)]
debug: PathBuf::new(),
path,
}
}
pub(super) fn push(&mut self, one: &OsStr) {
#[cfg(racy_asserts)]
self.debug.push(one);
if let Some(path) = &mut self.path {
path.push(one)
}
}
pub(super) fn pop(&mut self) -> bool {
#[cfg(racy_asserts)]
self.debug.pop();
if let Some(path) = &mut self.path {
path.pop()
} else {
true
}
}
pub(super) fn complete(&mut self) {
if let Some(path) = &mut self.path {
if path.as_os_str().is_empty() {
path.push(Component::CurDir);
}
self.path = None;
}
}
}
impl<'path_buf> Drop for CanonicalPath<'path_buf> {
fn drop(&mut self) {
if let Some(path) = &mut self.path {
path.clear();
self.path = None;
}
}
}