cap_primitives/fs/manually/
canonical_path.rs1use std::ffi::OsStr;
2use std::path::{Component, PathBuf};
3
4pub(super) struct CanonicalPath<'path_buf> {
6 path: Option<&'path_buf mut PathBuf>,
9
10 #[cfg(racy_asserts)]
12 pub(super) debug: PathBuf,
13}
14
15impl<'path_buf> CanonicalPath<'path_buf> {
16 pub(super) fn new(path: Option<&'path_buf mut PathBuf>) -> Self {
17 Self {
18 #[cfg(racy_asserts)]
19 debug: PathBuf::new(),
20
21 path,
22 }
23 }
24
25 pub(super) fn push(&mut self, one: &OsStr) {
26 #[cfg(racy_asserts)]
27 self.debug.push(one);
28
29 if let Some(path) = &mut self.path {
30 path.push(one)
31 }
32 }
33
34 pub(super) fn pop(&mut self) -> bool {
35 #[cfg(racy_asserts)]
36 self.debug.pop();
37
38 if let Some(path) = &mut self.path {
39 path.pop()
40 } else {
41 true
42 }
43 }
44
45 pub(super) fn complete(&mut self) {
48 if let Some(path) = &mut self.path {
51 if path.as_os_str().is_empty() {
52 path.push(Component::CurDir);
53 }
54 self.path = None;
55 }
56 }
57}
58
59impl<'path_buf> Drop for CanonicalPath<'path_buf> {
60 fn drop(&mut self) {
61 if let Some(path) = &mut self.path {
66 path.clear();
67 self.path = None;
68 }
69 }
70}