xvc_walker/
abspath.rs

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
//! An absolute path type to ensure that we don't use relative paths.
use std::ffi::OsString;
use std::fmt::Display;
use std::ops::Deref;
use std::path::{Path, PathBuf};

/// A specialized path type for absolute paths.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AbsolutePath(PathBuf);

impl From<PathBuf> for AbsolutePath {
    fn from(p: PathBuf) -> Self {
        if p.is_absolute() {
            Self(p)
        } else {
            let current_dir = std::env::current_dir().expect("Cannot determine current dir");
            let joined = current_dir.join(p);
            Self(
                joined
                    .canonicalize()
                    .unwrap_or_else(|_| panic!("Cannot canonicalize {:?}", joined)),
            )
        }
    }
}

impl Display for AbsolutePath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.to_string_lossy())
    }
}

impl AbsolutePath {
    /// Convert to OsString by cloning the inner value
    pub fn into_os_string(&self) -> OsString {
        self.0.clone().as_os_str().to_os_string()
    }

    /// Get reference to inner value
    pub fn as_path(&self) -> &Path {
        &self.0
    }

    /// Appends a relative path to this path to get another absolute path
    pub fn join<T>(&self, p: T) -> AbsolutePath
    where
        T: AsRef<Path>,
    {
        assert!(!p.as_ref().is_absolute());
        Self(self.0.join(p))
    }
}

impl From<&PathBuf> for AbsolutePath {
    fn from(p: &PathBuf) -> Self {
        Self::from(p.to_owned())
    }
}

impl From<&Path> for AbsolutePath {
    fn from(p: &Path) -> Self {
        Self::from(p.to_path_buf())
    }
}

impl From<AbsolutePath> for PathBuf {
    fn from(a: AbsolutePath) -> Self {
        a.0
    }
}

impl From<&str> for AbsolutePath {
    fn from(s: &str) -> Self {
        Self::from(PathBuf::from(s.to_string()))
    }
}

impl From<String> for AbsolutePath {
    fn from(s: String) -> Self {
        Self::from(PathBuf::from(s))
    }
}

impl Deref for AbsolutePath {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

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