is_tree/path/
mod.rs

1pub mod segment;
2pub use segment::*;
3
4
5use std::fmt::Display;
6
7#[derive(Clone, Default, PartialEq, Debug)]
8pub struct Path
9{
10    pub segments: Vec<String>
11}
12
13impl Path {
14    pub fn join(&self, segment: impl Into<String>) -> Path
15    {
16        let mut clone = self.clone();
17        clone.segments.push(segment.into());
18        clone
19    }
20}
21
22impl<'a> From<&'a str> for Path {
23    fn from(value: &'a str) -> Self {
24        let segments = value.split("::").map(String::from).collect();
25        Self { segments }
26    }
27}
28
29impl From<Vec<&str>> for Path
30{
31    fn from(value: Vec<&str>) -> Path {
32        let segments = value.iter().map(|s| s.to_string()).collect();
33        Path { segments }
34    }
35}
36
37impl From<Vec<String>> for Path
38{
39    fn from(value: Vec<String>) -> Path {
40        let segments = value;
41        Path { segments }
42    }
43}
44
45impl From<&[&str]> for Path
46{
47    fn from(value: &[&str]) -> Path {
48        let segments = value.iter().map(|s| s.to_string()).collect();
49        Path { segments }
50    }
51}
52
53impl From<&[String]> for Path
54{
55    fn from(value: &[String]) -> Path {
56        let segments = value.to_vec();
57        Path { segments }
58    }
59}
60
61impl Display for Path {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "{}", self.segments.join("::"))
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn from_string() {
73        let path = Path::from("A");
74        assert_eq!(path.segments, ["A"]);
75        let path = Path::from("A::B");
76        assert_eq!(path.segments, ["A", "B"]);
77    }
78
79    #[test]
80    fn from_array() {
81        let array = ["A", "B", "C"];
82        let slice = array.as_slice();
83        let path = Path::from(slice);
84        assert_eq!(path.segments, ["A", "B", "C"]);
85    }
86
87    #[test]
88    fn from_vector() {
89        let vector = vec!["A", "B", "C"];
90        let path = Path::from(vector);
91        assert_eq!(path.segments, ["A", "B", "C"]);
92    }
93}