1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::sync::Arc;

#[derive(Ord, PartialOrd, Eq, PartialEq, Clone)]
pub struct Path(Arc<String>);

impl Path {
    pub fn parent(&self) -> Path {
        let mut path_parts: Vec<&str> = self.split('.').collect();
        path_parts.pop();
        path_parts.join(".").into()
    }

    pub fn set(&self) -> Path {
        format!("{self}.set").into()
    }

    pub fn is_absolute(&self) -> bool {
        self.0.starts_with("top.")
    }

    pub fn join(&self, path: Path) -> Path {
        format!("{}.{}", self, path).into()
    }
}

impl std::ops::Deref for Path {
    type Target = str;

    fn deref(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for Path {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{}", &self.0)
    }
}

impl std::fmt::Debug for Path {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "Path(\"{}\")", &self.0)
    }
}

impl From<String> for Path {
    fn from(path: String) -> Path {
        Path(Arc::new(path))
    }
}

impl From<&str> for Path {
    fn from(path: &str) -> Path {
        Path(Arc::new(path.to_string()))
    }
}