1 //! Thin wrappers around `std::path`, distinguishing between absolute and
4 convert::{TryFrom, TryInto},
6 path::{Component, Path, PathBuf},
9 /// Wrapper around an absolute [`PathBuf`].
10 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
11 pub struct AbsPathBuf(PathBuf);
13 impl From<AbsPathBuf> for PathBuf {
14 fn from(AbsPathBuf(path_buf): AbsPathBuf) -> PathBuf {
19 impl ops::Deref for AbsPathBuf {
20 type Target = AbsPath;
21 fn deref(&self) -> &AbsPath {
26 impl AsRef<Path> for AbsPathBuf {
27 fn as_ref(&self) -> &Path {
32 impl AsRef<AbsPath> for AbsPathBuf {
33 fn as_ref(&self) -> &AbsPath {
38 impl TryFrom<PathBuf> for AbsPathBuf {
40 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
41 if !path_buf.is_absolute() {
44 Ok(AbsPathBuf(path_buf))
48 impl TryFrom<&str> for AbsPathBuf {
50 fn try_from(path: &str) -> Result<AbsPathBuf, PathBuf> {
51 AbsPathBuf::try_from(PathBuf::from(path))
55 impl PartialEq<AbsPath> for AbsPathBuf {
56 fn eq(&self, other: &AbsPath) -> bool {
57 self.as_path() == other
62 /// Wrap the given absolute path in `AbsPathBuf`
66 /// Panics if `path` is not absolute.
67 pub fn assert(path: PathBuf) -> AbsPathBuf {
68 AbsPathBuf::try_from(path)
69 .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display()))
72 /// Coerces to a `AbsPath` slice.
74 /// Equivalent of [`PathBuf::as_path`] for `AbsPathBuf`.
75 pub fn as_path(&self) -> &AbsPath {
76 AbsPath::assert(self.0.as_path())
79 /// Equivalent of [`PathBuf::pop`] for `AbsPathBuf`.
81 /// Note that this won't remove the root component, so `self` will still be
83 pub fn pop(&mut self) -> bool {
88 /// Wrapper around an absolute [`Path`].
89 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
91 pub struct AbsPath(Path);
93 impl ops::Deref for AbsPath {
95 fn deref(&self) -> &Path {
100 impl AsRef<Path> for AbsPath {
101 fn as_ref(&self) -> &Path {
106 impl<'a> TryFrom<&'a Path> for &'a AbsPath {
107 type Error = &'a Path;
108 fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {
109 if !path.is_absolute() {
112 Ok(AbsPath::assert(path))
117 /// Wrap the given absolute path in `AbsPath`
121 /// Panics if `path` is not absolute.
122 pub fn assert(path: &Path) -> &AbsPath {
123 assert!(path.is_absolute());
124 unsafe { &*(path as *const Path as *const AbsPath) }
127 /// Equivalent of [`Path::parent`] for `AbsPath`.
128 pub fn parent(&self) -> Option<&AbsPath> {
129 self.0.parent().map(AbsPath::assert)
132 /// Equivalent of [`Path::join`] for `AbsPath`.
133 pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
134 self.as_ref().join(path).try_into().unwrap()
137 /// Normalize the given path:
138 /// - Removes repeated separators: `/a//b` becomes `/a/b`
139 /// - Removes occurrences of `.` and resolves `..`.
140 /// - Removes trailing slashes: `/a/b/` becomes `/a/b`.
144 /// # use paths::AbsPathBuf;
145 /// let abs_path_buf = AbsPathBuf::assert("/a/../../b/.//c//".into());
146 /// let normalized = abs_path_buf.normalize();
147 /// assert_eq!(normalized, AbsPathBuf::assert("/b/c".into()));
149 pub fn normalize(&self) -> AbsPathBuf {
150 AbsPathBuf(normalize_path(&self.0))
153 /// Equivalent of [`Path::to_path_buf`] for `AbsPath`.
154 pub fn to_path_buf(&self) -> AbsPathBuf {
155 AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
158 /// Equivalent of [`Path::strip_prefix`] for `AbsPath`.
160 /// Returns a relative path.
161 pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> {
162 self.0.strip_prefix(base).ok().map(RelPath::new_unchecked)
166 /// Wrapper around a relative [`PathBuf`].
167 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
168 pub struct RelPathBuf(PathBuf);
170 impl From<RelPathBuf> for PathBuf {
171 fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf {
176 impl ops::Deref for RelPathBuf {
177 type Target = RelPath;
178 fn deref(&self) -> &RelPath {
183 impl AsRef<Path> for RelPathBuf {
184 fn as_ref(&self) -> &Path {
189 impl TryFrom<PathBuf> for RelPathBuf {
190 type Error = PathBuf;
191 fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> {
192 if !path_buf.is_relative() {
193 return Err(path_buf);
195 Ok(RelPathBuf(path_buf))
199 impl TryFrom<&str> for RelPathBuf {
200 type Error = PathBuf;
201 fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> {
202 RelPathBuf::try_from(PathBuf::from(path))
207 /// Coerces to a `RelPath` slice.
209 /// Equivalent of [`PathBuf::as_path`] for `RelPathBuf`.
210 pub fn as_path(&self) -> &RelPath {
211 RelPath::new_unchecked(self.0.as_path())
215 /// Wrapper around a relative [`Path`].
216 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
218 pub struct RelPath(Path);
220 impl ops::Deref for RelPath {
222 fn deref(&self) -> &Path {
227 impl AsRef<Path> for RelPath {
228 fn as_ref(&self) -> &Path {
234 /// Creates a new `RelPath` from `path`, without checking if it is relative.
235 pub fn new_unchecked(path: &Path) -> &RelPath {
236 unsafe { &*(path as *const Path as *const RelPath) }
240 /// Taken from https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85
241 fn normalize_path(path: &Path) -> PathBuf {
242 let mut components = path.components().peekable();
243 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
245 PathBuf::from(c.as_os_str())
250 for component in components {
252 Component::Prefix(..) => unreachable!(),
253 Component::RootDir => {
254 ret.push(component.as_os_str());
256 Component::CurDir => {}
257 Component::ParentDir => {
260 Component::Normal(c) => {