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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
pub mod segment;
pub use segment::*;


use std::fmt::Display;

#[derive(Clone, Default, PartialEq, Debug)]
pub struct Path
{
    pub segments: Vec<String>
}

impl Path {
    pub fn join(&self, segment: impl Into<String>) -> Path
    {
        let mut clone = self.clone();
        clone.segments.push(segment.into());
        clone
    }
}

impl<'a> From<&'a str> for Path {
    fn from(value: &'a str) -> Self {
        let segments = value.split("::").map(String::from).collect();
        Self { segments }
    }
}

impl From<Vec<&str>> for Path
{
    fn from(value: Vec<&str>) -> Path {
        let segments = value.iter().map(|s| s.to_string()).collect();
        Path { segments }
    }
}

impl From<Vec<String>> for Path
{
    fn from(value: Vec<String>) -> Path {
        let segments = value;
        Path { segments }
    }
}

impl From<&[&str]> for Path
{
    fn from(value: &[&str]) -> Path {
        let segments = value.iter().map(|s| s.to_string()).collect();
        Path { segments }
    }
}

impl From<&[String]> for Path
{
    fn from(value: &[String]) -> Path {
        let segments = value.to_vec();
        Path { segments }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_string() {
        let path = Path::from("A");
        assert_eq!(path.segments, ["A"]);
        let path = Path::from("A::B");
        assert_eq!(path.segments, ["A", "B"]);
    }

    #[test]
    fn from_array() {
        let array = ["A", "B", "C"];
        let slice = array.as_slice();
        let path = Path::from(slice);
        assert_eq!(path.segments, ["A", "B", "C"]);
    }

    #[test]
    fn from_vector() {
        let vector = vec!["A", "B", "C"];
        let path = Path::from(vector);
        assert_eq!(path.segments, ["A", "B", "C"]);
    }
}