1 //! Thin wrappers around `std::path`, distinguishing between absolute and
4 convert::{TryFrom, TryInto},
6 path::{Component, Path, PathBuf},
9 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
10 pub struct AbsPathBuf(PathBuf);
12 impl From<AbsPathBuf> for PathBuf {
13 fn from(AbsPathBuf(path_buf): AbsPathBuf) -> PathBuf {
18 impl ops::Deref for AbsPathBuf {
19 type Target = AbsPath;
20 fn deref(&self) -> &AbsPath {
25 impl AsRef<Path> for AbsPathBuf {
26 fn as_ref(&self) -> &Path {
31 impl AsRef<AbsPath> for AbsPathBuf {
32 fn as_ref(&self) -> &AbsPath {
37 impl TryFrom<PathBuf> for AbsPathBuf {
39 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
40 if !path_buf.is_absolute() {
43 Ok(AbsPathBuf(path_buf))
47 impl TryFrom<&str> for AbsPathBuf {
49 fn try_from(path: &str) -> Result<AbsPathBuf, PathBuf> {
50 AbsPathBuf::try_from(PathBuf::from(path))
54 impl PartialEq<AbsPath> for AbsPathBuf {
55 fn eq(&self, other: &AbsPath) -> bool {
56 self.as_path() == other
61 pub fn assert(path: PathBuf) -> AbsPathBuf {
62 AbsPathBuf::try_from(path)
63 .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display()))
65 pub fn as_path(&self) -> &AbsPath {
66 AbsPath::assert(self.0.as_path())
68 pub fn pop(&mut self) -> bool {
73 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
75 pub struct AbsPath(Path);
77 impl ops::Deref for AbsPath {
79 fn deref(&self) -> &Path {
84 impl AsRef<Path> for AbsPath {
85 fn as_ref(&self) -> &Path {
90 impl<'a> TryFrom<&'a Path> for &'a AbsPath {
91 type Error = &'a Path;
92 fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {
93 if !path.is_absolute() {
96 Ok(AbsPath::assert(path))
101 pub fn assert(path: &Path) -> &AbsPath {
102 assert!(path.is_absolute());
103 unsafe { &*(path as *const Path as *const AbsPath) }
106 pub fn parent(&self) -> Option<&AbsPath> {
107 self.0.parent().map(AbsPath::assert)
109 pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
110 self.as_ref().join(path).try_into().unwrap()
112 pub fn normalize(&self) -> AbsPathBuf {
113 AbsPathBuf(normalize_path(&self.0))
115 pub fn to_path_buf(&self) -> AbsPathBuf {
116 AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
118 pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> {
119 self.0.strip_prefix(base).ok().map(RelPath::new_unchecked)
123 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
124 pub struct RelPathBuf(PathBuf);
126 impl From<RelPathBuf> for PathBuf {
127 fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf {
132 impl ops::Deref for RelPathBuf {
133 type Target = RelPath;
134 fn deref(&self) -> &RelPath {
139 impl AsRef<Path> for RelPathBuf {
140 fn as_ref(&self) -> &Path {
145 impl TryFrom<PathBuf> for RelPathBuf {
146 type Error = PathBuf;
147 fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> {
148 if !path_buf.is_relative() {
149 return Err(path_buf);
151 Ok(RelPathBuf(path_buf))
155 impl TryFrom<&str> for RelPathBuf {
156 type Error = PathBuf;
157 fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> {
158 RelPathBuf::try_from(PathBuf::from(path))
163 pub fn as_path(&self) -> &RelPath {
164 RelPath::new_unchecked(self.0.as_path())
168 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
170 pub struct RelPath(Path);
172 impl ops::Deref for RelPath {
174 fn deref(&self) -> &Path {
179 impl AsRef<Path> for RelPath {
180 fn as_ref(&self) -> &Path {
186 pub fn new_unchecked(path: &Path) -> &RelPath {
187 unsafe { &*(path as *const Path as *const RelPath) }
191 // https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85
192 fn normalize_path(path: &Path) -> PathBuf {
193 let mut components = path.components().peekable();
194 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
196 PathBuf::from(c.as_os_str())
201 for component in components {
203 Component::Prefix(..) => unreachable!(),
204 Component::RootDir => {
205 ret.push(component.as_os_str());
207 Component::CurDir => {}
208 Component::ParentDir => {
211 Component::Normal(c) => {