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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// allow "path.rs" in "path"
#![allow(clippy::module_inception)]

use crate::fs::path::{PathBuf, SEPARATOR};
use crate::{CStr16, CString16};
use core::fmt::{Display, Formatter};

/// A path similar to the `Path` of the standard library, but based on
/// [`CStr16`] strings and [`SEPARATOR`] as separator.
///
/// [`SEPARATOR`]: super::SEPARATOR
#[derive(Debug, Eq, PartialOrd, Ord)]
pub struct Path(CStr16);

impl Path {
    /// Constructor.
    #[must_use]
    pub fn new<S: AsRef<CStr16> + ?Sized>(s: &S) -> &Self {
        unsafe { &*(s.as_ref() as *const CStr16 as *const Self) }
    }

    /// Returns the underlying string.
    #[must_use]
    pub fn to_cstr16(&self) -> &CStr16 {
        &self.0
    }

    /// Returns a path buf from that type.
    #[must_use]
    pub fn to_path_buf(&self) -> PathBuf {
        let cstring = CString16::from(&self.0);
        PathBuf::from(cstring)
    }

    /// Iterator over the components of a path.
    #[must_use]
    pub fn components(&self) -> Components {
        Components {
            path: self.as_ref(),
            i: 0,
        }
    }

    /// Returns the parent directory as [`PathBuf`].
    ///
    /// If the path is a top-level component, this returns None.
    #[must_use]
    pub fn parent(&self) -> Option<PathBuf> {
        let components_count = self.components().count();
        if components_count == 0 {
            return None;
        }

        // Return None, as we do not treat "\\" as dedicated component.
        let sep_count = self
            .0
            .as_slice()
            .iter()
            .filter(|char| **char == SEPARATOR)
            .count();
        if sep_count == 0 {
            return None;
        }

        let path =
            self.components()
                .take(components_count - 1)
                .fold(CString16::new(), |mut acc, next| {
                    // Add separator, as needed.
                    if !acc.is_empty() && *acc.as_slice().last().unwrap() != SEPARATOR {
                        acc.push(SEPARATOR);
                    }
                    acc.push_str(next.as_ref());
                    acc
                });
        let path = PathBuf::from(path);
        Some(path)
    }

    /// Returns of the path is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.to_cstr16().is_empty()
    }
}

impl Display for Path {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        Display::fmt(self.to_cstr16(), f)
    }
}

impl PartialEq for Path {
    fn eq(&self, other: &Self) -> bool {
        self.components().count() == other.components().count()
            && !self
                .components()
                .zip(other.components())
                .any(|(c1, c2)| c1 != c2)
    }
}

/// Iterator over the components of a path. For example, the path `\\a\\b\\c`
/// has the components `[a, b, c]`. This is a more basic approach than the
/// components type of the standard library.
#[derive(Debug)]
pub struct Components<'a> {
    path: &'a CStr16,
    i: usize,
}

impl<'a> Iterator for Components<'a> {
    // Attention. We can't iterate over &'Ctr16, as we would break any guarantee
    // made for the terminating null character.
    type Item = CString16;

    fn next(&mut self) -> Option<Self::Item> {
        if self.path.is_empty() {
            return None;
        }
        if self.path.num_chars() == 1 && self.path.as_slice()[0] == SEPARATOR {
            // The current implementation does not handle the root dir as
            // dedicated component so far. We just return nothing.
            return None;
        }

        // If the path is not empty and starts with a separator, skip it.
        if self.i == 0 && *self.path.as_slice().first().unwrap() == SEPARATOR {
            self.i = 1;
        }

        // Count how many characters are there until the next separator is
        // found.
        let len = self
            .path
            .iter()
            .skip(self.i)
            .take_while(|c| **c != SEPARATOR)
            .count();

        let progress = self.i + len;
        if progress > self.path.num_chars() {
            None
        } else {
            // select the next component and build an owned string
            let part = &self.path.as_slice()[self.i..self.i + len];
            let mut string = CString16::new();
            part.iter().for_each(|c| string.push(*c));

            // +1: skip the separator
            self.i = progress + 1;
            Some(string)
        }
    }
}

mod convenience_impls {
    use super::*;
    use core::borrow::Borrow;

    impl AsRef<Path> for &Path {
        fn as_ref(&self) -> &Path {
            self
        }
    }

    impl<'a> From<&'a CStr16> for &'a Path {
        fn from(value: &'a CStr16) -> Self {
            Path::new(value)
        }
    }

    impl AsRef<CStr16> for Path {
        fn as_ref(&self) -> &CStr16 {
            &self.0
        }
    }

    impl Borrow<CStr16> for Path {
        fn borrow(&self) -> &CStr16 {
            &self.0
        }
    }

    impl AsRef<Path> for CStr16 {
        fn as_ref(&self) -> &Path {
            Path::new(self)
        }
    }

    impl Borrow<Path> for CStr16 {
        fn borrow(&self) -> &Path {
            Path::new(self)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec::Vec;
    use uefi_macros::cstr16;

    #[test]
    fn from_cstr16() {
        let source: &CStr16 = cstr16!("\\hello\\foo\\bar");
        let _path: &Path = source.into();
        let _path: &Path = Path::new(source);
    }

    #[test]
    fn from_cstring16() {
        let source = CString16::try_from("\\hello\\foo\\bar").unwrap();
        let _path: &Path = source.as_ref().into();
        let _path: &Path = Path::new(source.as_ref());
    }

    #[test]
    fn components_iter() {
        let path = Path::new(cstr16!("foo\\bar\\hello"));
        let components = path.components().collect::<Vec<_>>();
        let components: Vec<&CStr16> = components.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
        let expected: &[&CStr16] = &[cstr16!("foo"), cstr16!("bar"), cstr16!("hello")];
        assert_eq!(components.as_slice(), expected);

        // In case there is a leading slash, it should be ignored.
        let path = Path::new(cstr16!("\\foo\\bar\\hello"));
        let components = path.components().collect::<Vec<_>>();
        let components: Vec<&CStr16> = components.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
        let expected: &[&CStr16] = &[cstr16!("foo"), cstr16!("bar"), cstr16!("hello")];
        assert_eq!(components.as_slice(), expected);

        // empty path iteration should be just fine
        let empty_cstring16 = CString16::try_from("").unwrap();
        let path = Path::new(empty_cstring16.as_ref());
        let components = path.components().collect::<Vec<_>>();
        let expected: &[CString16] = &[];
        assert_eq!(components.as_slice(), expected);

        // test empty path
        let _path = Path::new(cstr16!());
        let path = Path::new(cstr16!(""));
        let components = path.components().collect::<Vec<_>>();
        let components: Vec<&CStr16> = components.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
        let expected: &[&CStr16] = &[];
        assert_eq!(components.as_slice(), expected);

        // test path that has only root component. Treated as empty path by
        // the components iterator.
        let path = Path::new(cstr16!("\\"));
        let components = path.components().collect::<Vec<_>>();
        let components: Vec<&CStr16> = components.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
        let expected: &[&CStr16] = &[];
        assert_eq!(components.as_slice(), expected);
    }

    #[test]
    fn test_parent() {
        assert_eq!(None, Path::new(cstr16!("")).parent());
        assert_eq!(None, Path::new(cstr16!("\\")).parent());
        assert_eq!(
            Path::new(cstr16!("a\\b")).parent(),
            Some(PathBuf::from(cstr16!("a"))),
        );
        assert_eq!(
            Path::new(cstr16!("\\a\\b")).parent(),
            Some(PathBuf::from(cstr16!("a"))),
        );
        assert_eq!(
            Path::new(cstr16!("a\\b\\c\\d")).parent(),
            Some(PathBuf::from(cstr16!("a\\b\\c"))),
        );
        assert_eq!(Path::new(cstr16!("abc")).parent(), None,);
    }

    #[test]
    fn partial_eq() {
        let path1 = Path::new(cstr16!(r"a\b"));
        let path2 = Path::new(cstr16!(r"\a\b"));
        let path3 = Path::new(cstr16!(r"a\b\c"));

        assert_eq!(path1, path1);
        assert_eq!(path2, path2);
        assert_eq!(path3, path3);

        // Equal as currently, we only support absolute paths, so the leading
        // separator is obligatory.
        assert_eq!(path1, path2);
        assert_eq!(path2, path1);

        assert_ne!(path1, path3);
        assert_ne!(path3, path1);
    }
}