camino-anchored 0.1.1

UTF-8 paths with explicit resolution and base-relative display
Documentation
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright (c) The camino-anchored Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use camino::{FromPathBufError, Utf8Path, Utf8PathBuf};
use std::{error::Error, fmt, io};

/// An error indicating that a path was not a well-formed absolute path.
///
/// Returned by [`AbsUtf8PathBuf::new`](crate::AbsUtf8PathBuf::new).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AbsUtf8PathError {
    /// The path that was rejected.
    path: Utf8PathBuf,

    /// The reason the path was rejected.
    kind: AbsUtf8PathErrorKind,
}

impl AbsUtf8PathError {
    pub(crate) fn new(path: Utf8PathBuf, kind: AbsUtf8PathErrorKind) -> Self {
        Self { path, kind }
    }

    /// Returns the path that was rejected.
    #[must_use]
    pub fn path(&self) -> &Utf8Path {
        &self.path
    }

    /// Consumes the error, returning the path that was rejected.
    #[must_use]
    pub fn into_path(self) -> Utf8PathBuf {
        self.path
    }

    /// Returns the reason the path was rejected.
    #[must_use]
    pub fn kind(&self) -> AbsUtf8PathErrorKind {
        self.kind
    }
}

impl fmt::Display for AbsUtf8PathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let path = &self.path;
        match self.kind {
            AbsUtf8PathErrorKind::Empty => {
                write!(f, "expected an absolute path, but the path is empty")
            }
            AbsUtf8PathErrorKind::ContainsNul => {
                write!(
                    f,
                    "expected an absolute path, but {path:?} contains a NUL byte"
                )
            }
            AbsUtf8PathErrorKind::NotAbsolute => {
                write!(f, "expected an absolute path, got `{path}`")
            }
            AbsUtf8PathErrorKind::RootRelative => write!(
                f,
                "expected an absolute path, but `{path}` is relative to the root \
                 of the current drive"
            ),
            AbsUtf8PathErrorKind::DriveRelative => write!(
                f,
                "expected an absolute path, but `{path}` is relative to the \
                 current directory of its drive"
            ),
        }
    }
}

impl Error for AbsUtf8PathError {}

/// The reason a path is not a well-formed absolute path.
///
/// Part of [`AbsUtf8PathError`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AbsUtf8PathErrorKind {
    /// The path is empty.
    Empty,

    /// The path contains a NUL byte.
    ///
    /// No supported platform allows NUL bytes in paths.
    ContainsNul,

    /// The path is not absolute, e.g. `foo/bar`.
    NotAbsolute,

    /// The path is root-relative, e.g. `\foo` on Windows.
    RootRelative,

    /// The path is drive-relative, e.g. `C:foo` or `C:` on Windows.
    DriveRelative,
}

/// An error indicating that a path was not a well-formed relative path.
///
/// Returned by [`RelUtf8PathBuf::new`](crate::RelUtf8PathBuf::new).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RelUtf8PathError {
    /// The non-well-formed relative path.
    path: Utf8PathBuf,

    /// The reason the path was rejected.
    kind: RelUtf8PathErrorKind,
}

impl RelUtf8PathError {
    pub(crate) fn new(path: Utf8PathBuf, kind: RelUtf8PathErrorKind) -> Self {
        Self { path, kind }
    }

    /// Returns the path that was rejected.
    #[must_use]
    pub fn path(&self) -> &Utf8Path {
        &self.path
    }

    /// Consumes the error, returning the path that was rejected.
    #[must_use]
    pub fn into_path(self) -> Utf8PathBuf {
        self.path
    }

    /// Returns the reason the path was rejected.
    #[must_use]
    pub fn kind(&self) -> RelUtf8PathErrorKind {
        self.kind
    }
}

impl fmt::Display for RelUtf8PathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let path = &self.path;
        match self.kind {
            RelUtf8PathErrorKind::Empty => {
                write!(f, "expected a relative path, but the path is empty")
            }
            RelUtf8PathErrorKind::ContainsNul => {
                write!(
                    f,
                    "expected a relative path, but {path:?} contains a NUL byte"
                )
            }
            RelUtf8PathErrorKind::Absolute => {
                write!(f, "expected a relative path, but `{path}` is absolute")
            }
            RelUtf8PathErrorKind::RootRelative => write!(
                f,
                "expected a relative path, but `{path}` is relative to the root \
                 of the current drive (remove the leading separator to make it \
                 relative)"
            ),
            RelUtf8PathErrorKind::DriveRelative => write!(
                f,
                "expected a relative path, but `{path}` is relative to the \
                 current directory of its drive (remove the drive prefix to \
                 make it relative)"
            ),
        }
    }
}

impl Error for RelUtf8PathError {}

/// The reason a path is not a well-formed relative path.
///
/// Part of [`RelUtf8PathError`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RelUtf8PathErrorKind {
    /// The path is empty.
    Empty,

    /// The path contains a NUL byte.
    ///
    /// No supported platform allows NUL bytes in paths.
    ContainsNul,

    /// The path is absolute, e.g. `/foo` on Unix or `C:\foo` on Windows.
    Absolute,

    /// The path is root-relative, e.g. `\foo` on Windows.
    RootRelative,

    /// The path is drive-relative, e.g. `C:foo` or `C:` on Windows.
    DriveRelative,
}

/// An error that occurs if an input path could not be resolved.
///
/// Returned by
/// [`AbsUtf8PathBuf::resolve_against_current_dir`](crate::AbsUtf8PathBuf::resolve_against_current_dir)
/// and [`PathAnchor::resolve_input`](crate::PathAnchor::resolve_input).
#[derive(Debug)]
pub struct ResolvePathError {
    input: Utf8PathBuf,
    kind: ResolvePathErrorKind,
}

impl ResolvePathError {
    pub(crate) fn new(input: Utf8PathBuf, kind: ResolvePathErrorKind) -> Self {
        Self { input, kind }
    }

    /// The input path that could not be resolved.
    #[must_use]
    pub fn input(&self) -> &Utf8Path {
        &self.input
    }

    /// Consumes the error, returning the input path that could not be resolved.
    #[must_use]
    pub fn into_input(self) -> Utf8PathBuf {
        self.input
    }

    /// Returns the reason resolution failed.
    #[must_use]
    pub fn kind(&self) -> &ResolvePathErrorKind {
        &self.kind
    }
}

impl fmt::Display for ResolvePathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let input = &self.input;
        match &self.kind {
            ResolvePathErrorKind::Empty => write!(f, "cannot resolve an empty path"),
            ResolvePathErrorKind::ContainsNul => {
                write!(f, "cannot resolve {input:?}: path contains a NUL byte")
            }
            ResolvePathErrorKind::Native(NativePathErrorKind::Io(_)) => {
                write!(f, "failed to resolve `{input}` to an absolute path")
            }
            ResolvePathErrorKind::Native(NativePathErrorKind::NonUtf8(error)) => write!(
                f,
                "resolved `{input}` to `{}`, which is not valid UTF-8",
                error.as_path().display()
            ),
            ResolvePathErrorKind::Native(NativePathErrorKind::Invalid(error)) => {
                match error.kind() {
                    AbsUtf8PathErrorKind::Empty | AbsUtf8PathErrorKind::ContainsNul => {
                        write!(f, "resolved `{input}` to an invalid path")
                    }
                    AbsUtf8PathErrorKind::NotAbsolute
                    | AbsUtf8PathErrorKind::RootRelative
                    | AbsUtf8PathErrorKind::DriveRelative => write!(
                        f,
                        "resolved `{input}` to an invalid path \
                         (the current directory may be unreachable)"
                    ),
                }
            }
        }
    }
}

impl Error for ResolvePathError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            ResolvePathErrorKind::Empty | ResolvePathErrorKind::ContainsNul => None,
            ResolvePathErrorKind::Native(kind) => kind.source(),
        }
    }
}

/// The reason an input path could not be resolved.
///
/// Part of [`ResolvePathError`].
#[derive(Debug)]
#[non_exhaustive]
pub enum ResolvePathErrorKind {
    /// The input path is empty.
    Empty,

    /// The input path contains a NUL byte.
    ContainsNul,

    /// Native resolution failed, or its result could not be used.
    Native(NativePathErrorKind),
}

/// An error that occurred while obtaining the current directory.
///
/// Returned by
/// [`AbsUtf8PathBuf::current_dir`](crate::AbsUtf8PathBuf::current_dir) and
/// [`PathAnchor::current_dir`](crate::PathAnchor::current_dir).
#[derive(Debug)]
pub struct CurrentDirError {
    kind: NativePathErrorKind,
}

impl CurrentDirError {
    pub(crate) fn new(kind: NativePathErrorKind) -> Self {
        Self { kind }
    }

    /// Returns information about the kind of error that occurred.
    #[must_use]
    pub fn kind(&self) -> &NativePathErrorKind {
        &self.kind
    }
}

impl fmt::Display for CurrentDirError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            NativePathErrorKind::Io(_) => write!(f, "failed to read the current directory"),
            NativePathErrorKind::NonUtf8(error) => write!(
                f,
                "current directory `{}` is not valid UTF-8",
                error.as_path().display()
            ),
            NativePathErrorKind::Invalid(error) => match error.kind() {
                AbsUtf8PathErrorKind::Empty | AbsUtf8PathErrorKind::ContainsNul => {
                    write!(f, "current directory is invalid")
                }
                AbsUtf8PathErrorKind::NotAbsolute
                | AbsUtf8PathErrorKind::RootRelative
                | AbsUtf8PathErrorKind::DriveRelative => {
                    write!(f, "current directory is invalid (it may be unreachable)")
                }
            },
        }
    }
}

impl Error for CurrentDirError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.kind.source()
    }
}

/// A platform-level error that occurred while resolving a path.
///
/// Part of [`CurrentDirError`] and [`ResolvePathErrorKind::Native`].
#[derive(Debug)]
#[non_exhaustive]
pub enum NativePathErrorKind {
    /// A platform-level resolution failure occurred (e.g. the current directory
    /// is not readable).
    Io(io::Error),

    /// The resolved path is not valid UTF-8.
    NonUtf8(FromPathBufError),

    /// The resolved path is not a well-formed absolute path.
    ///
    /// This should not occur in typical use. On Linux systems with glibc older
    /// than 2.27, this can happen if the current directory is outside the
    /// process's root directory (for example, after a `chroot` operation):
    /// `getcwd` returns a path starting with `(unreachable)`. See
    /// [glibc bug 22679](https://sourceware.org/bugzilla/show_bug.cgi?id=22679).
    Invalid(AbsUtf8PathError),
}

impl NativePathErrorKind {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            NativePathErrorKind::Io(error) => Some(error),
            NativePathErrorKind::Invalid(error) => Some(error),
            NativePathErrorKind::NonUtf8(error) => Some(error),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) enum MalformedPathKind {
    Empty,
    ContainsNul,
}

impl From<MalformedPathKind> for AbsUtf8PathErrorKind {
    fn from(kind: MalformedPathKind) -> Self {
        match kind {
            MalformedPathKind::Empty => Self::Empty,
            MalformedPathKind::ContainsNul => Self::ContainsNul,
        }
    }
}

impl From<MalformedPathKind> for RelUtf8PathErrorKind {
    fn from(kind: MalformedPathKind) -> Self {
        match kind {
            MalformedPathKind::Empty => Self::Empty,
            MalformedPathKind::ContainsNul => Self::ContainsNul,
        }
    }
}

impl From<MalformedPathKind> for ResolvePathErrorKind {
    fn from(kind: MalformedPathKind) -> Self {
        match kind {
            MalformedPathKind::Empty => Self::Empty,
            MalformedPathKind::ContainsNul => Self::ContainsNul,
        }
    }
}

/// An error converting a [`PathBuf`](std::path::PathBuf) to an
/// [`AbsUtf8PathBuf`](crate::AbsUtf8PathBuf).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TryFromPathBufError {
    /// The path was not valid UTF-8.
    NonUtf8(FromPathBufError),

    /// The path was UTF-8, but was rejected by
    /// [`AbsUtf8PathBuf::new`](crate::AbsUtf8PathBuf::new).
    Invalid(AbsUtf8PathError),
}

impl fmt::Display for TryFromPathBufError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TryFromPathBufError::NonUtf8(error) => fmt::Display::fmt(error, f),
            TryFromPathBufError::Invalid(error) => fmt::Display::fmt(error, f),
        }
    }
}

impl Error for TryFromPathBufError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            TryFromPathBufError::NonUtf8(error) => error.source(),
            TryFromPathBufError::Invalid(error) => error.source(),
        }
    }
}

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

    #[cfg(unix)]
    fn non_utf8_error(prefix: &str) -> FromPathBufError {
        use std::{ffi::OsStr, os::unix::ffi::OsStrExt, path::PathBuf};

        let mut bytes = prefix.as_bytes().to_vec();
        bytes.push(0xff);
        Utf8PathBuf::try_from(PathBuf::from(OsStr::from_bytes(&bytes)))
            .expect_err("path with an invalid byte is not UTF-8")
    }

    #[cfg(windows)]
    fn non_utf8_error(prefix: &str) -> FromPathBufError {
        use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf};

        const LONE_SURROGATE: u16 = 0xD800;
        let mut wide: Vec<u16> = prefix.encode_utf16().collect();
        wide.push(LONE_SURROGATE);
        Utf8PathBuf::try_from(PathBuf::from(OsString::from_wide(&wide)))
            .expect_err("path with a lone surrogate is not UTF-8")
    }

    #[test]
    fn absolute_path_error_display() {
        for (path, kind, expected) in [
            (
                "",
                AbsUtf8PathErrorKind::Empty,
                "expected an absolute path, but the path is empty",
            ),
            (
                "/a\0b",
                AbsUtf8PathErrorKind::ContainsNul,
                r#"expected an absolute path, but "/a\0b" contains a NUL byte"#,
            ),
            (
                "foo/bar",
                AbsUtf8PathErrorKind::NotAbsolute,
                "expected an absolute path, got `foo/bar`",
            ),
            (
                r"\foo",
                AbsUtf8PathErrorKind::RootRelative,
                r"expected an absolute path, but `\foo` is relative to the root of the current drive",
            ),
            (
                "C:foo",
                AbsUtf8PathErrorKind::DriveRelative,
                "expected an absolute path, but `C:foo` is relative to the current directory of its drive",
            ),
        ] {
            let error = AbsUtf8PathError::new(Utf8PathBuf::from(path), kind);
            assert_eq!(error.to_string(), expected, "{kind:?}");
            assert!(error.source().is_none(), "{kind:?}");
        }
    }

    #[test]
    fn relative_path_error_display() {
        for (path, kind, expected) in [
            (
                "",
                RelUtf8PathErrorKind::Empty,
                "expected a relative path, but the path is empty",
            ),
            (
                "a\0b",
                RelUtf8PathErrorKind::ContainsNul,
                r#"expected a relative path, but "a\0b" contains a NUL byte"#,
            ),
            (
                "/foo",
                RelUtf8PathErrorKind::Absolute,
                "expected a relative path, but `/foo` is absolute",
            ),
            (
                r"\foo",
                RelUtf8PathErrorKind::RootRelative,
                r"expected a relative path, but `\foo` is relative to the root of the current drive (remove the leading separator to make it relative)",
            ),
            (
                "C:foo",
                RelUtf8PathErrorKind::DriveRelative,
                "expected a relative path, but `C:foo` is relative to the current directory of its drive (remove the drive prefix to make it relative)",
            ),
        ] {
            let error = RelUtf8PathError::new(Utf8PathBuf::from(path), kind);
            assert_eq!(error.to_string(), expected, "{kind:?}");
            assert!(error.source().is_none(), "{kind:?}");
        }
    }

    #[cfg(any(unix, windows))]
    #[test]
    fn resolve_path_error_display() {
        let not_found = io::Error::from(io::ErrorKind::NotFound).to_string();
        for (input, kind, expected, expected_source) in [
            (
                "",
                ResolvePathErrorKind::Empty,
                "cannot resolve an empty path",
                None,
            ),
            (
                "a\0b",
                ResolvePathErrorKind::ContainsNul,
                r#"cannot resolve "a\0b": path contains a NUL byte"#,
                None,
            ),
            (
                "config.toml",
                ResolvePathErrorKind::Native(NativePathErrorKind::Io(io::Error::from(
                    io::ErrorKind::NotFound,
                ))),
                "failed to resolve `config.toml` to an absolute path",
                Some(not_found.as_str()),
            ),
            (
                "config.toml",
                ResolvePathErrorKind::Native(NativePathErrorKind::NonUtf8(non_utf8_error(
                    "/repo/config.toml",
                ))),
                "resolved `config.toml` to `/repo/config.toml\u{fffd}`, which is not valid UTF-8",
                Some("PathBuf contains invalid UTF-8: /repo/config.toml\u{fffd}"),
            ),
            (
                "config.toml",
                native_invalid(
                    "(unreachable)/repo/config.toml",
                    AbsUtf8PathErrorKind::NotAbsolute,
                ),
                "resolved `config.toml` to an invalid path (the current directory may be unreachable)",
                Some("expected an absolute path, got `(unreachable)/repo/config.toml`"),
            ),
            (
                "config.toml",
                native_invalid("", AbsUtf8PathErrorKind::Empty),
                "resolved `config.toml` to an invalid path",
                Some("expected an absolute path, but the path is empty"),
            ),
            (
                "config.toml",
                native_invalid("/repo/a\0b", AbsUtf8PathErrorKind::ContainsNul),
                "resolved `config.toml` to an invalid path",
                Some(r#"expected an absolute path, but "/repo/a\0b" contains a NUL byte"#),
            ),
        ] {
            let error = ResolvePathError::new(Utf8PathBuf::from(input), kind);
            assert_eq!(error.to_string(), expected, "{error:?}");
            assert_eq!(
                error.source().map(|source| source.to_string()).as_deref(),
                expected_source,
                "{error:?}"
            );
        }
    }

    fn native_invalid(path: &str, kind: AbsUtf8PathErrorKind) -> ResolvePathErrorKind {
        ResolvePathErrorKind::Native(NativePathErrorKind::Invalid(AbsUtf8PathError::new(
            Utf8PathBuf::from(path),
            kind,
        )))
    }

    #[cfg(any(unix, windows))]
    #[test]
    fn current_dir_error_display() {
        let invalid = |path: &str, kind| {
            CurrentDirError::new(NativePathErrorKind::Invalid(AbsUtf8PathError::new(
                Utf8PathBuf::from(path),
                kind,
            )))
        };
        let not_found = io::Error::from(io::ErrorKind::NotFound).to_string();
        for (error, expected, expected_source) in [
            (
                CurrentDirError::new(NativePathErrorKind::Io(io::Error::from(
                    io::ErrorKind::NotFound,
                ))),
                "failed to read the current directory",
                Some(not_found.as_str()),
            ),
            (
                CurrentDirError::new(NativePathErrorKind::NonUtf8(non_utf8_error("/repo/"))),
                "current directory `/repo/\u{fffd}` is not valid UTF-8",
                Some("PathBuf contains invalid UTF-8: /repo/\u{fffd}"),
            ),
            (
                invalid("(unreachable)/repo", AbsUtf8PathErrorKind::NotAbsolute),
                "current directory is invalid (it may be unreachable)",
                Some("expected an absolute path, got `(unreachable)/repo`"),
            ),
            (
                invalid(r"\repo", AbsUtf8PathErrorKind::RootRelative),
                "current directory is invalid (it may be unreachable)",
                Some(
                    r"expected an absolute path, but `\repo` is relative to the root of the current drive",
                ),
            ),
            (
                invalid("", AbsUtf8PathErrorKind::Empty),
                "current directory is invalid",
                Some("expected an absolute path, but the path is empty"),
            ),
            (
                invalid("/a\0b", AbsUtf8PathErrorKind::ContainsNul),
                "current directory is invalid",
                Some(r#"expected an absolute path, but "/a\0b" contains a NUL byte"#),
            ),
        ] {
            assert_eq!(error.to_string(), expected, "{error:?}");
            assert_eq!(
                error.source().map(|source| source.to_string()).as_deref(),
                expected_source,
                "{error:?}"
            );
        }
    }
}