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
pub mod segment;
pub use segment::*;


use std::fmt::Display;

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

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

impl<Segment> Default for Path<Segment> {
    fn default() -> Self {
        let segments = Vec::new();
        Self { segments }
    }
}

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

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

impl<'a, Segment> From<&'a [Segment]> for Path<Segment>
where Segment: Copy
{
    fn from(value: &'a [Segment]) -> Path<Segment> {
        let segments = value.to_vec();
        Path { segments }
    }
}

impl Display for Path<String> {
    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<_> = 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"]);
    }
}