1 //! Thin wrappers around `std::path`, distinguishing between absolute and
5 convert::{TryFrom, TryInto},
7 path::{Component, Path, PathBuf},
10 /// Wrapper around an absolute [`PathBuf`].
11 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
12 pub struct AbsPathBuf(PathBuf);
14 impl From<AbsPathBuf> for PathBuf {
15 fn from(AbsPathBuf(path_buf): AbsPathBuf) -> PathBuf {
20 impl ops::Deref for AbsPathBuf {
21 type Target = AbsPath;
22 fn deref(&self) -> &AbsPath {
27 impl AsRef<Path> for AbsPathBuf {
28 fn as_ref(&self) -> &Path {
33 impl AsRef<AbsPath> for AbsPathBuf {
34 fn as_ref(&self) -> &AbsPath {
39 impl Borrow<AbsPath> for AbsPathBuf {
40 fn borrow(&self) -> &AbsPath {
45 impl TryFrom<PathBuf> for AbsPathBuf {
47 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
48 if !path_buf.is_absolute() {
51 Ok(AbsPathBuf(path_buf))
55 impl TryFrom<&str> for AbsPathBuf {
57 fn try_from(path: &str) -> Result<AbsPathBuf, PathBuf> {
58 AbsPathBuf::try_from(PathBuf::from(path))
62 impl PartialEq<AbsPath> for AbsPathBuf {
63 fn eq(&self, other: &AbsPath) -> bool {
64 self.as_path() == other
69 /// Wrap the given absolute path in `AbsPathBuf`
73 /// Panics if `path` is not absolute.
74 pub fn assert(path: PathBuf) -> AbsPathBuf {
75 AbsPathBuf::try_from(path)
76 .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display()))
79 /// Coerces to a `AbsPath` slice.
81 /// Equivalent of [`PathBuf::as_path`] for `AbsPathBuf`.
82 pub fn as_path(&self) -> &AbsPath {
83 AbsPath::assert(self.0.as_path())
86 /// Equivalent of [`PathBuf::pop`] for `AbsPathBuf`.
88 /// Note that this won't remove the root component, so `self` will still be
90 pub fn pop(&mut self) -> bool {
95 /// Wrapper around an absolute [`Path`].
96 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
98 pub struct AbsPath(Path);
100 impl ops::Deref for AbsPath {
102 fn deref(&self) -> &Path {
107 impl AsRef<Path> for AbsPath {
108 fn as_ref(&self) -> &Path {
113 impl<'a> TryFrom<&'a Path> for &'a AbsPath {
114 type Error = &'a Path;
115 fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {
116 if !path.is_absolute() {
119 Ok(AbsPath::assert(path))
124 /// Wrap the given absolute path in `AbsPath`
128 /// Panics if `path` is not absolute.
129 pub fn assert(path: &Path) -> &AbsPath {
130 assert!(path.is_absolute());
131 unsafe { &*(path as *const Path as *const AbsPath) }
134 /// Equivalent of [`Path::parent`] for `AbsPath`.
135 pub fn parent(&self) -> Option<&AbsPath> {
136 self.0.parent().map(AbsPath::assert)
139 /// Equivalent of [`Path::join`] for `AbsPath`.
140 pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
141 self.as_ref().join(path).try_into().unwrap()
144 /// Normalize the given path:
145 /// - Removes repeated separators: `/a//b` becomes `/a/b`
146 /// - Removes occurrences of `.` and resolves `..`.
147 /// - Removes trailing slashes: `/a/b/` becomes `/a/b`.
151 /// # use paths::AbsPathBuf;
152 /// let abs_path_buf = AbsPathBuf::assert("/a/../../b/.//c//".into());
153 /// let normalized = abs_path_buf.normalize();
154 /// assert_eq!(normalized, AbsPathBuf::assert("/b/c".into()));
156 pub fn normalize(&self) -> AbsPathBuf {
157 AbsPathBuf(normalize_path(&self.0))
160 /// Equivalent of [`Path::to_path_buf`] for `AbsPath`.
161 pub fn to_path_buf(&self) -> AbsPathBuf {
162 AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
165 /// Equivalent of [`Path::strip_prefix`] for `AbsPath`.
167 /// Returns a relative path.
168 pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> {
169 self.0.strip_prefix(base).ok().map(RelPath::new_unchecked)
173 /// Wrapper around a relative [`PathBuf`].
174 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
175 pub struct RelPathBuf(PathBuf);
177 impl From<RelPathBuf> for PathBuf {
178 fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf {
183 impl ops::Deref for RelPathBuf {
184 type Target = RelPath;
185 fn deref(&self) -> &RelPath {
190 impl AsRef<Path> for RelPathBuf {
191 fn as_ref(&self) -> &Path {
196 impl TryFrom<PathBuf> for RelPathBuf {
197 type Error = PathBuf;
198 fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> {
199 if !path_buf.is_relative() {
200 return Err(path_buf);
202 Ok(RelPathBuf(path_buf))
206 impl TryFrom<&str> for RelPathBuf {
207 type Error = PathBuf;
208 fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> {
209 RelPathBuf::try_from(PathBuf::from(path))
214 /// Coerces to a `RelPath` slice.
216 /// Equivalent of [`PathBuf::as_path`] for `RelPathBuf`.
217 pub fn as_path(&self) -> &RelPath {
218 RelPath::new_unchecked(self.0.as_path())
222 /// Wrapper around a relative [`Path`].
223 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
225 pub struct RelPath(Path);
227 impl ops::Deref for RelPath {
229 fn deref(&self) -> &Path {
234 impl AsRef<Path> for RelPath {
235 fn as_ref(&self) -> &Path {
241 /// Creates a new `RelPath` from `path`, without checking if it is relative.
242 pub fn new_unchecked(path: &Path) -> &RelPath {
243 unsafe { &*(path as *const Path as *const RelPath) }
247 /// Taken from https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85
248 fn normalize_path(path: &Path) -> PathBuf {
249 let mut components = path.components().peekable();
250 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
252 PathBuf::from(c.as_os_str())
257 for component in components {
259 Component::Prefix(..) => unreachable!(),
260 Component::RootDir => {
261 ret.push(component.as_os_str());
263 Component::CurDir => {}
264 Component::ParentDir => {
267 Component::Normal(c) => {