cobalt_core/
fs.rs

1use relative_path::RelativePathBuf;
2
3#[derive(
4    Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
5)]
6#[cfg_attr(not(feature = "unstable"), non_exhaustive)]
7pub struct SourcePath {
8    pub abs_path: std::path::PathBuf,
9    pub rel_path: RelativePathBuf,
10}
11
12impl SourcePath {
13    pub fn from_root(root: &std::path::Path, path: &std::path::Path) -> Option<Self> {
14        let abs_path = path.to_owned();
15        let rel_path = path.strip_prefix(root).ok()?;
16        let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
17        Some(Self { abs_path, rel_path })
18    }
19
20    pub fn pop(&mut self) -> bool {
21        let abs = self.abs_path.pop();
22        let rel = self.rel_path.pop();
23        assert_eq!(abs, rel);
24        abs
25    }
26
27    pub fn push(&mut self, name: &str) {
28        self.abs_path.push(name);
29        self.rel_path.push(name);
30    }
31}