bitsy_lang/
path.rs

1use std::sync::Arc;
2
3#[derive(Ord, PartialOrd, Eq, PartialEq, Clone)]
4pub struct Path(Arc<String>);
5
6impl Path {
7    pub fn parent(&self) -> Path {
8        let mut path_parts: Vec<&str> = self.split('.').collect();
9        path_parts.pop();
10        path_parts.join(".").into()
11    }
12
13    pub fn set(&self) -> Path {
14        format!("{self}.set").into()
15    }
16
17    pub fn is_absolute(&self) -> bool {
18        self.0.starts_with("top.")
19    }
20
21    pub fn join(&self, path: Path) -> Path {
22        format!("{}.{}", self, path).into()
23    }
24}
25
26impl std::ops::Deref for Path {
27    type Target = str;
28
29    fn deref(&self) -> &str {
30        &self.0
31    }
32}
33
34impl std::fmt::Display for Path {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
36        write!(f, "{}", &self.0)
37    }
38}
39
40impl std::fmt::Debug for Path {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
42        write!(f, "Path(\"{}\")", &self.0)
43    }
44}
45
46impl From<String> for Path {
47    fn from(path: String) -> Path {
48        Path(Arc::new(path))
49    }
50}
51
52impl From<&str> for Path {
53    fn from(path: &str) -> Path {
54        Path(Arc::new(path.to_string()))
55    }
56}