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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::{Component as StdComponent, Path as StdPath, PathBuf as StdPathBuf};

use crate::native::{Utf8NativePath, Utf8NativePathBuf};
use crate::unix::UnixComponent;
use crate::windows::{WindowsComponent, WindowsPrefixComponent};
use crate::{Encoding, Path, PathBuf};

/// Interface to try to perform a cheap reference-to-reference conversion.
pub trait TryAsRef<T: ?Sized> {
    fn try_as_ref(&self) -> Option<&T>;
}

impl<T> TryAsRef<StdPath> for Path<T>
where
    T: for<'enc> Encoding<'enc>,
{
    /// Attempts to convert a [`Path`] into a [`std::path::Path`], succeeding if the path is
    /// comprised only of valid UTF-8 bytes
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::Path;
    /// use typed_path::{TryAsRef, UnixPath};
    ///
    /// let unix_path = UnixPath::new("/path/to/file.txt");
    /// let std_path: &Path = unix_path.try_as_ref().unwrap();
    /// ```
    fn try_as_ref(&self) -> Option<&StdPath> {
        std::str::from_utf8(self.as_bytes()).map(StdPath::new).ok()
    }
}

impl<T> TryAsRef<Path<T>> for StdPath
where
    T: for<'enc> Encoding<'enc>,
{
    /// Attempts to convert a [`std::path::Path`] into a [`Path`], returning a result containing
    /// the new path when successful
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::Path;
    /// use typed_path::{TryAsRef, UnixPath};
    ///
    /// let std_path = Path::new("/path/to/file.txt");
    /// let unix_path: &UnixPath = std_path.try_as_ref().unwrap();
    /// ```
    fn try_as_ref(&self) -> Option<&Path<T>> {
        self.to_str().map(Path::new)
    }
}

impl<T> TryFrom<PathBuf<T>> for StdPathBuf
where
    T: for<'enc> Encoding<'enc>,
{
    type Error = PathBuf<T>;

    /// Attempts to convert a [`PathBuf`] into a [`std::path::PathBuf`], returning a result
    /// containing the new path when successful or the original path when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::path::PathBuf;
    /// use typed_path::UnixPathBuf;
    ///
    /// let unix_path_buf = UnixPathBuf::from("/path/to/file.txt");
    /// let std_path_buf: PathBuf = TryFrom::try_from(unix_path_buf).unwrap();
    /// ```
    fn try_from(path: PathBuf<T>) -> Result<Self, Self::Error> {
        match std::str::from_utf8(path.as_bytes()) {
            Ok(s) => Ok(StdPathBuf::from(s)),
            Err(_) => Err(path),
        }
    }
}

impl<T> TryFrom<StdPathBuf> for PathBuf<T>
where
    T: for<'enc> Encoding<'enc>,
{
    type Error = StdPathBuf;

    /// Attempts to convert a [`std::path::PathBuf`] into a [`PathBuf`], returning a result
    /// containing the new path when successful or the original path when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::path::PathBuf;
    /// use typed_path::UnixPathBuf;
    ///
    /// let std_path_buf = PathBuf::from("/path/to/file.txt");
    /// let unix_path_buf: UnixPathBuf = TryFrom::try_from(std_path_buf).unwrap();
    /// ```
    fn try_from(path: StdPathBuf) -> Result<Self, Self::Error> {
        match path.to_str() {
            Some(s) => Ok(PathBuf::from(s)),
            None => Err(path),
        }
    }
}

impl<'a> TryFrom<UnixComponent<'a>> for StdComponent<'a> {
    type Error = UnixComponent<'a>;

    /// Attempts to convert a [`UnixComponent`] into a [`std::path::Component`], returning a result
    /// containing the new path when successful or the original path when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::ffi::OsStr;
    /// use std::path::Component;
    /// use typed_path::unix::UnixComponent;
    ///
    /// let component = Component::try_from(UnixComponent::RootDir).unwrap();
    /// assert_eq!(component, Component::RootDir);
    ///
    /// let component = Component::try_from(UnixComponent::CurDir).unwrap();
    /// assert_eq!(component, Component::CurDir);
    ///
    /// let component = Component::try_from(UnixComponent::ParentDir).unwrap();
    /// assert_eq!(component, Component::ParentDir);
    ///
    /// let component = Component::try_from(UnixComponent::Normal(b"file.txt")).unwrap();
    /// assert_eq!(component, Component::Normal(OsStr::new("file.txt")));
    /// ```
    fn try_from(component: UnixComponent<'a>) -> Result<Self, Self::Error> {
        match &component {
            UnixComponent::RootDir => Ok(Self::RootDir),
            UnixComponent::CurDir => Ok(Self::CurDir),
            UnixComponent::ParentDir => Ok(Self::ParentDir),
            UnixComponent::Normal(x) => Ok(Self::Normal(OsStr::new(
                std::str::from_utf8(x).map_err(|_| component)?,
            ))),
        }
    }
}

impl<'a> TryFrom<StdComponent<'a>> for UnixComponent<'a> {
    type Error = StdComponent<'a>;

    /// Attempts to convert a [`std::path::Component`] into a [`UnixComponent`], returning a result
    /// containing the new component when successful or the original component when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::ffi::OsStr;
    /// use std::path::Component;
    /// use typed_path::unix::UnixComponent;
    ///
    /// let component = UnixComponent::try_from(Component::RootDir).unwrap();
    /// assert_eq!(component, UnixComponent::RootDir);
    ///
    /// let component = UnixComponent::try_from(Component::CurDir).unwrap();
    /// assert_eq!(component, UnixComponent::CurDir);
    ///
    /// let component = UnixComponent::try_from(Component::ParentDir).unwrap();
    /// assert_eq!(component, UnixComponent::ParentDir);
    ///
    /// let component = UnixComponent::try_from(Component::Normal(OsStr::new("file.txt"))).unwrap();
    /// assert_eq!(component, UnixComponent::Normal(b"file.txt"));
    /// ```
    fn try_from(component: StdComponent<'a>) -> Result<Self, Self::Error> {
        match &component {
            StdComponent::Prefix(_) => Err(component),
            StdComponent::RootDir => Ok(Self::RootDir),
            StdComponent::CurDir => Ok(Self::CurDir),
            StdComponent::ParentDir => Ok(Self::ParentDir),
            StdComponent::Normal(x) => Ok(Self::Normal(x.to_str().ok_or(component)?.as_bytes())),
        }
    }
}

impl<'a> TryFrom<WindowsComponent<'a>> for StdComponent<'a> {
    type Error = WindowsComponent<'a>;

    /// Attempts to convert a [`WindowsComponent`] into a [`std::path::Component`], returning a
    /// result containing the new path when successful or the original path when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::ffi::OsStr;
    /// use std::path::Component;
    /// use typed_path::windows::WindowsComponent;
    ///
    /// let component = Component::try_from(WindowsComponent::RootDir).unwrap();
    /// assert_eq!(component, Component::RootDir);
    ///
    /// let component = Component::try_from(WindowsComponent::CurDir).unwrap();
    /// assert_eq!(component, Component::CurDir);
    ///
    /// let component = Component::try_from(WindowsComponent::ParentDir).unwrap();
    /// assert_eq!(component, Component::ParentDir);
    ///
    /// let component = Component::try_from(WindowsComponent::Normal(b"file.txt")).unwrap();
    /// assert_eq!(component, Component::Normal(OsStr::new("file.txt")));
    /// ```
    ///
    /// Alongside the traditional path components, the [`Component::Prefix`] variant is also
    /// supported, but only when compiling on Windows. When on a non-Windows platform, the
    /// conversion will always fail.
    ///
    /// [`Component::Prefix`]: std::path::Component::Prefix
    ///
    fn try_from(component: WindowsComponent<'a>) -> Result<Self, Self::Error> {
        match &component {
            // NOTE: Standard library provides no way to construct a PrefixComponent, so, we have
            //       to build a new path with just the prefix and then get the component
            //
            //       Because the prefix is not empty when being supplied to the path, we should get
            //       back at least one component and can therefore return the unwrapped result
            WindowsComponent::Prefix(x) => {
                if cfg!(windows) {
                    Ok(
                        StdPath::new(std::str::from_utf8(x.as_bytes()).map_err(|_| component)?)
                            .components()
                            .next()
                            .expect("Impossible: non-empty std path had no components"),
                    )
                } else {
                    Err(component)
                }
            }
            WindowsComponent::RootDir => Ok(Self::RootDir),
            WindowsComponent::CurDir => Ok(Self::CurDir),
            WindowsComponent::ParentDir => Ok(Self::ParentDir),
            WindowsComponent::Normal(x) => Ok(Self::Normal(OsStr::new(
                std::str::from_utf8(x).map_err(|_| component)?,
            ))),
        }
    }
}

impl<'a> TryFrom<StdComponent<'a>> for WindowsComponent<'a> {
    type Error = StdComponent<'a>;

    /// Attempts to convert a [`std::path::Component`] into a [`WindowsComponent`], returning a
    /// result containing the new component when successful or the original component when failed
    ///
    /// # Examples
    ///
    /// ```
    /// use std::convert::TryFrom;
    /// use std::ffi::OsStr;
    /// use std::path::Component;
    /// use typed_path::windows::WindowsComponent;
    ///
    /// let component = WindowsComponent::try_from(Component::RootDir).unwrap();
    /// assert_eq!(component, WindowsComponent::RootDir);
    ///
    /// let component = WindowsComponent::try_from(Component::CurDir).unwrap();
    /// assert_eq!(component, WindowsComponent::CurDir);
    ///
    /// let component = WindowsComponent::try_from(Component::ParentDir).unwrap();
    /// assert_eq!(component, WindowsComponent::ParentDir);
    ///
    /// let component = WindowsComponent::try_from(Component::Normal(OsStr::new("file.txt"))).unwrap();
    /// assert_eq!(component, WindowsComponent::Normal(b"file.txt"));
    /// ```
    ///
    /// Alongside the traditional path components, the [`Component::Prefix`] variant is also
    /// supported, but only when compiling on Windows. When on a non-Windows platform, the
    /// conversion will always fail.
    ///
    /// [`Component::Prefix`]: std::path::Component::Prefix
    ///
    fn try_from(component: StdComponent<'a>) -> Result<Self, Self::Error> {
        match &component {
            StdComponent::Prefix(x) => Ok(WindowsComponent::Prefix(
                WindowsPrefixComponent::try_from(x.as_os_str().to_str().ok_or(component)?)
                    .map_err(|_| component)?,
            )),
            StdComponent::RootDir => Ok(Self::RootDir),
            StdComponent::CurDir => Ok(Self::CurDir),
            StdComponent::ParentDir => Ok(Self::ParentDir),
            StdComponent::Normal(x) => Ok(Self::Normal(x.to_str().ok_or(component)?.as_bytes())),
        }
    }
}

impl AsRef<StdPath> for Utf8NativePath {
    /// Converts a native utf8 path (based on compilation family) into [`std::path::Path`].
    ///
    /// ```
    /// use typed_path::Utf8NativePath;
    /// use std::path::Path;
    ///
    /// let native_path = Utf8NativePath::new("some_file.txt");
    /// let std_path: &Path = native_path.as_ref();
    ///
    /// assert_eq!(std_path, Path::new("some_file.txt"));
    /// ```
    fn as_ref(&self) -> &StdPath {
        StdPath::new(self.as_str())
    }
}

impl AsRef<StdPath> for Utf8NativePathBuf {
    /// Converts a native utf8 pathbuf (based on compilation family) into [`std::path::Path`].
    ///
    /// ```
    /// use typed_path::Utf8NativePathBuf;
    /// use std::path::Path;
    ///
    /// let native_path_buf = Utf8NativePathBuf::from("some_file.txt");
    /// let std_path: &Path = native_path_buf.as_ref();
    ///
    /// assert_eq!(std_path, Path::new("some_file.txt"));
    /// ```
    fn as_ref(&self) -> &StdPath {
        StdPath::new(self.as_str())
    }
}

impl<'a> From<&'a Utf8NativePath> for StdPathBuf {
    /// Converts a native utf8 path (based on compilation family) into [`std::path::PathBuf`].
    ///
    /// ```
    /// use typed_path::Utf8NativePath;
    /// use std::path::PathBuf;
    ///
    /// let native_path = Utf8NativePath::new("some_file.txt");
    /// let std_path_buf = PathBuf::from(native_path);
    ///
    /// assert_eq!(std_path_buf, PathBuf::from("some_file.txt"));
    /// ```
    fn from(utf8_native_path: &'a Utf8NativePath) -> StdPathBuf {
        StdPathBuf::from(utf8_native_path.to_string())
    }
}

impl From<Utf8NativePathBuf> for StdPathBuf {
    /// Converts a native utf8 pathbuf (based on compilation family) into [`std::path::PathBuf`].
    ///
    /// ```
    /// use typed_path::Utf8NativePathBuf;
    /// use std::path::PathBuf;
    ///
    /// let native_path_buf = Utf8NativePathBuf::from("some_file.txt");
    /// let std_path_buf = PathBuf::from(native_path_buf);
    ///
    /// assert_eq!(std_path_buf, PathBuf::from("some_file.txt"));
    /// ```
    fn from(utf8_native_path_buf: Utf8NativePathBuf) -> StdPathBuf {
        StdPathBuf::from(utf8_native_path_buf.into_string())
    }
}

#[cfg(any(
    unix,
    all(target_vendor = "fortanix", target_env = "sgx"),
    target_os = "solid_asp3",
    target_os = "hermit",
    target_os = "wasi"
))]
mod common {
    use std::ffi::{OsStr, OsString};
    #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
    use std::os::fortanix_sgx as os;
    #[cfg(target_os = "solid_asp3")]
    use std::os::solid as os;
    #[cfg(any(target_os = "hermit", unix))]
    use std::os::unix as os;
    #[cfg(target_os = "wasi")]
    use std::os::wasi as os;

    use os::ffi::{OsStrExt, OsStringExt};

    use super::*;

    impl<T> From<PathBuf<T>> for OsString
    where
        T: for<'enc> Encoding<'enc>,
    {
        #[inline]
        fn from(path_buf: PathBuf<T>) -> Self {
            OsStringExt::from_vec(path_buf.into_vec())
        }
    }

    impl<T> AsRef<Path<T>> for OsStr
    where
        T: for<'enc> Encoding<'enc>,
    {
        #[inline]
        fn as_ref(&self) -> &Path<T> {
            Path::new(self.as_bytes())
        }
    }

    impl<T> AsRef<Path<T>> for OsString
    where
        T: for<'enc> Encoding<'enc>,
    {
        #[inline]
        fn as_ref(&self) -> &Path<T> {
            Path::new(self.as_bytes())
        }
    }

    impl<T> AsRef<OsStr> for Path<T>
    where
        T: for<'enc> Encoding<'enc>,
    {
        #[inline]
        fn as_ref(&self) -> &OsStr {
            OsStrExt::from_bytes(self.as_bytes())
        }
    }

    impl<T> AsRef<OsStr> for PathBuf<T>
    where
        T: for<'enc> Encoding<'enc>,
    {
        #[inline]
        fn as_ref(&self) -> &OsStr {
            OsStrExt::from_bytes(self.as_bytes())
        }
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryFrom;
    use std::path::Component;

    use super::*;

    fn make_windows_prefix_component(s: &str) -> WindowsComponent {
        let component = WindowsComponent::try_from(s).unwrap();
        assert!(component.is_prefix());
        component
    }

    #[test]
    #[cfg(windows)]
    fn try_from_windows_component_to_std_component_should_keep_prefix_on_windows() {
        use std::ffi::OsStr;
        use std::path::Prefix;
        fn get_prefix(component: Component) -> Prefix {
            match component {
                Component::Prefix(prefix) => prefix.kind(),
                x => panic!("Wrong component: {x:?}"),
            }
        }

        let component = Component::try_from(make_windows_prefix_component("C:")).unwrap();
        assert_eq!(get_prefix(component), Prefix::Disk(b'C'));

        let component =
            Component::try_from(make_windows_prefix_component(r"\\server\share")).unwrap();
        assert_eq!(
            get_prefix(component),
            Prefix::UNC(OsStr::new("server"), OsStr::new("share"))
        );

        let component = Component::try_from(make_windows_prefix_component(r"\\.\COM42")).unwrap();
        assert_eq!(get_prefix(component), Prefix::DeviceNS(OsStr::new("COM42")));

        let component = Component::try_from(make_windows_prefix_component(r"\\?\C:")).unwrap();
        assert_eq!(get_prefix(component), Prefix::VerbatimDisk(b'C'));

        let component =
            Component::try_from(make_windows_prefix_component(r"\\?\UNC\server\share")).unwrap();
        assert_eq!(
            get_prefix(component),
            Prefix::VerbatimUNC(OsStr::new("server"), OsStr::new("share"))
        );

        let component =
            Component::try_from(make_windows_prefix_component(r"\\?\pictures")).unwrap();
        assert_eq!(
            get_prefix(component),
            Prefix::Verbatim(OsStr::new("pictures"))
        );
    }

    #[test]
    #[cfg(not(windows))]
    fn try_from_windows_component_to_std_component_should_fail_for_prefix_on_non_windows() {
        Component::try_from(make_windows_prefix_component("C:")).unwrap_err();
        Component::try_from(make_windows_prefix_component(r"\\server\share")).unwrap_err();
        Component::try_from(make_windows_prefix_component(r"\\.\COM42")).unwrap_err();
        Component::try_from(make_windows_prefix_component(r"\\?\C:")).unwrap_err();
        Component::try_from(make_windows_prefix_component(r"\\?\UNC\server\share")).unwrap_err();
        Component::try_from(make_windows_prefix_component(r"\\?\pictures")).unwrap_err();
    }

    #[test]
    #[cfg(windows)]
    fn try_from_std_component_to_windows_component_should_keep_prefix_on_windows() {
        use std::path::Path;

        use crate::windows::WindowsPrefix;

        fn make_component(s: &str) -> Component {
            let component = Path::new(s).components().next();
            assert!(
                matches!(component, Some(Component::Prefix(_))),
                "std component not a prefix"
            );
            component.unwrap()
        }

        fn get_prefix(component: WindowsComponent) -> WindowsPrefix {
            match component {
                WindowsComponent::Prefix(prefix) => prefix.kind(),
                x => panic!("Wrong component: {x:?}"),
            }
        }

        let component = WindowsComponent::try_from(make_component("C:")).unwrap();
        assert_eq!(get_prefix(component), WindowsPrefix::Disk(b'C'));

        let component = WindowsComponent::try_from(make_component(r"\\server\share")).unwrap();
        assert_eq!(
            get_prefix(component),
            WindowsPrefix::UNC(b"server", b"share")
        );

        let component = WindowsComponent::try_from(make_component(r"\\.\COM42")).unwrap();
        assert_eq!(get_prefix(component), WindowsPrefix::DeviceNS(b"COM42"));

        let component = WindowsComponent::try_from(make_component(r"\\?\C:")).unwrap();
        assert_eq!(get_prefix(component), WindowsPrefix::VerbatimDisk(b'C'));

        let component =
            WindowsComponent::try_from(make_component(r"\\?\UNC\server\share")).unwrap();
        assert_eq!(
            get_prefix(component),
            WindowsPrefix::VerbatimUNC(b"server", b"share")
        );

        let component = WindowsComponent::try_from(make_component(r"\\?\pictures")).unwrap();
        assert_eq!(get_prefix(component), WindowsPrefix::Verbatim(b"pictures"));
    }
}