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
use crate::{common::*, dir_stack::try_rebase_path};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct AbsPathBuf(PathBuf);
impl AbsPathBuf {
pub fn get(&self) -> &PathBuf {
&self.0
}
pub fn into_inner(self) -> PathBuf {
self.0
}
}
impl TryFrom<&'_ OsStr> for AbsPathBuf {
type Error = anyhow::Error;
fn try_from(from: &'_ OsStr) -> Result<Self, Self::Error> {
let path = Path::new(from);
let path = try_rebase_path(path);
Ok(Self(path.into_owned()))
}
}
impl TryFrom<&'_ Path> for AbsPathBuf {
type Error = anyhow::Error;
fn try_from(from: &'_ Path) -> Result<Self, Self::Error> {
Self::try_from(from.as_os_str())
}
}
impl TryFrom<&'_ PathBuf> for AbsPathBuf {
type Error = anyhow::Error;
fn try_from(from: &PathBuf) -> Result<Self, Self::Error> {
Self::try_from(from.as_os_str())
}
}
impl TryFrom<PathBuf> for AbsPathBuf {
type Error = anyhow::Error;
fn try_from(from: PathBuf) -> Result<Self, Self::Error> {
Self::try_from(from.as_os_str())
}
}
impl AsRef<Path> for AbsPathBuf {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
impl Deref for AbsPathBuf {
type Target = Path;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Serialize for AbsPathBuf {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let path = &self.0;
assert!(path.is_absolute(), "please report bug");
path.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for AbsPathBuf {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let path = PathBuf::deserialize(deserializer)?;
let path = Self::try_from(&path).map_err(|_| {
D::Error::custom(format!(
"unable to resolve the absolute path of '{}'",
path.display()
))
})?;
Ok(path)
}
}