Skip to main content

typr_core/utils/
path.rs

1use serde::Serialize;
2use std::ops::Add;
3
4// names separated by a "/"
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Default, Hash)]
6pub struct Path(String);
7
8impl Path {
9    pub fn new(s: &str) -> Path {
10        Path(s.to_string())
11    }
12
13    pub fn to_r(self) -> String {
14        match &self.0[..] {
15            "" => "".to_string(),
16            _ => self.0.replace("::", "$") + "$",
17        }
18    }
19
20    pub fn add_path(self, p: Path) -> Path {
21        Path(self.0 + "::" + &p.0)
22    }
23
24    pub fn is_empty(&self) -> bool {
25        self.0 == ""
26    }
27
28    pub fn get_value(&self) -> String {
29        self.0.clone()
30    }
31}
32
33impl Add<Path> for Path {
34    type Output = Path;
35
36    fn add(self, rhs: Path) -> Path {
37        Path(self.0 + "::" + &rhs.0)
38    }
39}
40
41use std::fmt;
42impl fmt::Display for Path {
43    fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        if self.0 == "" {
45            write!(f, "{}", self.0)
46        } else {
47            write!(f, "{}::", self.0)
48        }
49    }
50}
51
52impl From<String> for Path {
53    fn from(val: String) -> Self {
54        Path(val)
55    }
56}
57
58impl From<&str> for Path {
59    fn from(val: &str) -> Self {
60        Path(val.to_string())
61    }
62}
63
64impl From<Path> for String {
65    fn from(val: Path) -> Self {
66        val.0
67    }
68}