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
use std::ffi::OsString;
use std::fmt::Display;
use std::ops::Deref;
use std::path::{Path, PathBuf};
#[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 {
pub fn into_os_string(&self) -> OsString {
self.0.clone().as_os_str().to_os_string()
}
pub fn as_path(&self) -> &Path {
&self.0
}
}
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()
}
}