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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::ops::{Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
use core::slice::Iter;

#[derive(Debug, Eq, Ord, Clone)]
/// Path struct
pub struct Path
{
    names: Vec<String>,
}

impl Path
{
    /// New a Path from a path string
    ///
    /// ```rust
    ///  use cnfs::Path;
    ///  let p1 = Path::new("/home//caozhanhao/cnss");
    ///  let p2 = Path::new("/home/caozhanhao/cnss/dev/../");
    ///  assert_eq!(p1, p2);
    /// ```
    ///
    pub fn new(path_str: &str) -> Self
    {
        // Currently we only support absolute path
        assert!(path_str.starts_with('/'));
        let mut path = Self { names: Vec::<String>::new() };
        for part in path_str.split('/') {
            match part {
                "" | "." => continue,
                ".." => {
                    match path.names.last() {
                        Some(last) if last == "/" => {}
                        None => {}
                        _ => {
                            path.names.pop();
                        }
                    }
                }
                _ => {
                    if path.names.is_empty() {
                        path.names.push("/".into());
                    }
                    path.names.push(part.into());
                }
            }
        }
        if path.names.is_empty() {
            path.names.push("/".into());
        }
        path
    }

    /// Returns the parent path
    pub fn parent(&self) -> Option<Path>
    {
        if self.names.len() < 2 { return None; }
        let mut ret = self.clone();
        ret.names.pop();
        Some(ret)
    }

    /// Check if the path is starts with given path
    pub fn starts_with(&self, item: &Self) -> bool
    {
        self.names.starts_with(&item.names)
    }

    /// Returns the length of the path.
    pub fn len(&self) -> usize
    {
        self.names.len()
    }

    /// Iterator
    pub fn iter(&self) -> Iter<'_, String>
    {
        self.names.iter()
    }

    /// Convert the path to string.
    pub fn to_string(&self) -> String
    {
        let mut ret = self.names.join("/");
        if ret.len() != 1
        {
            assert_eq!(ret.remove(0), '/');
        }
        ret
    }
}

impl Index<usize> for Path {
    type Output = String;

    fn index(&self, index: usize) -> &Self::Output {
        &self.names[index]
    }
}

impl Index<Range<usize>> for Path {
    type Output = [String];

    fn index(&self, index: Range<usize>) -> &Self::Output {
        &self.names[index.start..index.end]
    }
}

impl Index<RangeFull> for Path {
    type Output = [String];

    fn index(&self, _index: RangeFull) -> &Self::Output {
        &self.names
    }
}

impl Index<RangeFrom<usize>> for Path {
    type Output = [String];

    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
        &self.names[index.start..]
    }
}

impl Index<RangeTo<usize>> for Path {
    type Output = [String];

    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
        &self.names[..index.end]
    }
}

impl Index<RangeToInclusive<usize>> for Path {
    type Output = [String];

    fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
        &self.names[..index.end]
    }
}

impl Index<RangeInclusive<usize>> for Path {
    type Output = [String];

    fn index(&self, index: RangeInclusive<usize>) -> &Self::Output {
        &self.names[*index.start()..*index.end()]
    }
}

impl From<&[String]> for Path
{
    fn from(value: &[String]) -> Self {
        Self {
            names: value.to_vec()
        }
    }
}

impl PartialEq for Path {
    fn eq(&self, other: &Self) -> bool {
        self.names.eq(&other.names)
    }
}

impl PartialOrd for Path
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.names.partial_cmp(&other.names)
    }
}