]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! See [`ManifestPath`]. |
2 | use std::{ops, path::Path}; | |
3 | ||
4 | use paths::{AbsPath, AbsPathBuf}; | |
5 | ||
6 | /// More or less [`AbsPathBuf`] with non-None parent. | |
7 | /// | |
8 | /// We use it to store path to Cargo.toml, as we frequently use the parent dir | |
9 | /// as a working directory to spawn various commands, and its nice to not have | |
10 | /// to `.unwrap()` everywhere. | |
11 | /// | |
12 | /// This could have been named `AbsNonRootPathBuf`, as we don't enforce that | |
13 | /// this stores manifest files in particular, but we only use this for manifests | |
14 | /// at the moment in practice. | |
15 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | |
16 | pub struct ManifestPath { | |
17 | file: AbsPathBuf, | |
18 | } | |
19 | ||
20 | impl TryFrom<AbsPathBuf> for ManifestPath { | |
21 | type Error = AbsPathBuf; | |
22 | ||
23 | fn try_from(file: AbsPathBuf) -> Result<Self, Self::Error> { | |
24 | if file.parent().is_none() { | |
25 | Err(file) | |
26 | } else { | |
27 | Ok(ManifestPath { file }) | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
32 | impl ManifestPath { | |
33 | // Shadow `parent` from `Deref`. | |
34 | pub fn parent(&self) -> &AbsPath { | |
35 | self.file.parent().unwrap() | |
36 | } | |
37 | } | |
38 | ||
39 | impl ops::Deref for ManifestPath { | |
40 | type Target = AbsPath; | |
41 | ||
42 | fn deref(&self) -> &Self::Target { | |
9c376795 | 43 | &self.file |
064997fb FG |
44 | } |
45 | } | |
46 | ||
47 | impl AsRef<Path> for ManifestPath { | |
48 | fn as_ref(&self) -> &Path { | |
49 | self.file.as_ref() | |
50 | } | |
51 | } |