typed_path/common/
errors.rs

1use core::fmt;
2
3/// An error returned if the prefix was not found.
4///
5/// This `struct` is created by the [`strip_prefix`] method on [`Path`].
6/// See its documentation for more.
7///
8/// [`Path`]: crate::Path
9/// [`strip_prefix`]: crate::Path::strip_prefix
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct StripPrefixError(pub(crate) ());
12
13impl fmt::Display for StripPrefixError {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        write!(f, "prefix not found")
16    }
17}
18
19#[cfg(feature = "std")]
20impl std::error::Error for StripPrefixError {}
21
22/// An error returned when a path violates checked criteria.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub enum CheckedPathError {
25    /// When a normal component contains invalid characters for the current encoding.
26    InvalidFilename,
27
28    /// When a path component that represents a parent directory is provided such that the original
29    /// path would be escaped to access arbitrary files.
30    PathTraversalAttack,
31
32    /// When a path component that represents a prefix is provided after the start of the path.
33    UnexpectedPrefix,
34
35    /// When a path component that represents a root is provided after the start of the path.
36    UnexpectedRoot,
37}
38
39impl fmt::Display for CheckedPathError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::InvalidFilename => write!(f, "path contains invalid filename"),
43            Self::PathTraversalAttack => write!(f, "path attempts to escape original path"),
44            Self::UnexpectedPrefix => write!(f, "path contains unexpected prefix"),
45            Self::UnexpectedRoot => write!(f, "path contains unexpected root"),
46        }
47    }
48}
49
50#[cfg(feature = "std")]
51impl std::error::Error for CheckedPathError {}