rust/crates/paths/src/lib.rs

295 lines
7.7 KiB
Rust
Raw Normal View History

//! Thin wrappers around `std::path`, distinguishing between absolute and
//! relative paths.
use std::{
2021-04-06 08:22:26 -05:00
borrow::Borrow,
convert::{TryFrom, TryInto},
2021-07-17 08:43:33 -05:00
ffi::OsStr,
2020-06-11 04:04:09 -05:00
ops,
path::{Component, Path, PathBuf},
};
2021-01-22 08:24:32 -06:00
/// Wrapper around an absolute [`PathBuf`].
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct AbsPathBuf(PathBuf);
impl From<AbsPathBuf> for PathBuf {
fn from(AbsPathBuf(path_buf): AbsPathBuf) -> PathBuf {
path_buf
}
}
impl ops::Deref for AbsPathBuf {
type Target = AbsPath;
fn deref(&self) -> &AbsPath {
self.as_path()
}
}
impl AsRef<Path> for AbsPathBuf {
fn as_ref(&self) -> &Path {
self.0.as_path()
}
}
impl AsRef<AbsPath> for AbsPathBuf {
fn as_ref(&self) -> &AbsPath {
2021-04-06 08:22:26 -05:00
self.as_path()
}
}
impl Borrow<AbsPath> for AbsPathBuf {
fn borrow(&self) -> &AbsPath {
self.as_path()
}
}
impl TryFrom<PathBuf> for AbsPathBuf {
type Error = PathBuf;
fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
if !path_buf.is_absolute() {
return Err(path_buf);
}
Ok(AbsPathBuf(path_buf))
}
}
impl TryFrom<&str> for AbsPathBuf {
type Error = PathBuf;
fn try_from(path: &str) -> Result<AbsPathBuf, PathBuf> {
AbsPathBuf::try_from(PathBuf::from(path))
}
}
impl PartialEq<AbsPath> for AbsPathBuf {
fn eq(&self, other: &AbsPath) -> bool {
self.as_path() == other
}
}
impl AbsPathBuf {
2021-01-22 08:24:32 -06:00
/// Wrap the given absolute path in `AbsPathBuf`
///
/// # Panics
///
/// Panics if `path` is not absolute.
pub fn assert(path: PathBuf) -> AbsPathBuf {
AbsPathBuf::try_from(path)
.unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display()))
}
2021-01-22 08:24:32 -06:00
/// Coerces to a `AbsPath` slice.
///
/// Equivalent of [`PathBuf::as_path`] for `AbsPathBuf`.
pub fn as_path(&self) -> &AbsPath {
AbsPath::assert(self.0.as_path())
}
2021-01-22 08:24:32 -06:00
/// Equivalent of [`PathBuf::pop`] for `AbsPathBuf`.
///
/// Note that this won't remove the root component, so `self` will still be
/// absolute.
pub fn pop(&mut self) -> bool {
self.0.pop()
}
}
2021-01-22 08:24:32 -06:00
/// Wrapper around an absolute [`Path`].
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct AbsPath(Path);
impl AsRef<Path> for AbsPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl<'a> TryFrom<&'a Path> for &'a AbsPath {
type Error = &'a Path;
fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {
if !path.is_absolute() {
return Err(path);
}
Ok(AbsPath::assert(path))
}
}
impl AbsPath {
2021-01-22 08:24:32 -06:00
/// Wrap the given absolute path in `AbsPath`
///
/// # Panics
///
/// Panics if `path` is not absolute.
pub fn assert(path: &Path) -> &AbsPath {
assert!(path.is_absolute());
unsafe { &*(path as *const Path as *const AbsPath) }
}
2021-01-22 08:24:32 -06:00
/// Equivalent of [`Path::parent`] for `AbsPath`.
pub fn parent(&self) -> Option<&AbsPath> {
self.0.parent().map(AbsPath::assert)
}
2021-01-22 08:24:32 -06:00
/// Equivalent of [`Path::join`] for `AbsPath`.
pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
self.as_ref().join(path).try_into().unwrap()
}
2021-01-22 08:24:32 -06:00
/// Normalize the given path:
/// - Removes repeated separators: `/a//b` becomes `/a/b`
/// - Removes occurrences of `.` and resolves `..`.
/// - Removes trailing slashes: `/a/b/` becomes `/a/b`.
///
/// # Example
/// ```
/// # use paths::AbsPathBuf;
/// let abs_path_buf = AbsPathBuf::assert("/a/../../b/.//c//".into());
/// let normalized = abs_path_buf.normalize();
/// assert_eq!(normalized, AbsPathBuf::assert("/b/c".into()));
/// ```
pub fn normalize(&self) -> AbsPathBuf {
AbsPathBuf(normalize_path(&self.0))
}
2021-01-22 08:24:32 -06:00
/// Equivalent of [`Path::to_path_buf`] for `AbsPath`.
2020-06-22 08:03:37 -05:00
pub fn to_path_buf(&self) -> AbsPathBuf {
AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
}
2021-01-22 08:24:32 -06:00
/// Equivalent of [`Path::strip_prefix`] for `AbsPath`.
///
/// Returns a relative path.
2020-06-22 08:03:37 -05:00
pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> {
self.0.strip_prefix(base).ok().map(RelPath::new_unchecked)
}
2021-07-17 08:43:33 -05:00
pub fn starts_with(&self, base: &AbsPath) -> bool {
self.0.starts_with(&base.0)
}
// region:delegate-methods
// Note that we deliberately don't implement `Deref<Target = Path>` here.
//
// The problem with `Path` is that it directly exposes convenience IO-ing
// methods. For example, `Path::exists` delegates to `fs::metadata`.
//
// For `AbsPath`, we want to make sure that this is a POD type, and that all
// IO goes via `fs`. That way, it becomes easier to mock IO when we need it.
pub fn file_name(&self) -> Option<&OsStr> {
self.0.file_name()
}
pub fn extension(&self) -> Option<&OsStr> {
self.0.extension()
}
pub fn file_stem(&self) -> Option<&OsStr> {
self.0.file_stem()
}
pub fn as_os_str(&self) -> &OsStr {
self.0.as_os_str()
}
pub fn display(&self) -> std::path::Display<'_> {
self.0.display()
}
#[deprecated(note = "use std::fs::metadata().is_ok() instead")]
pub fn exists(&self) -> bool {
self.0.exists()
}
// endregion:delegate-methods
2020-06-22 08:03:37 -05:00
}
2021-01-22 08:24:32 -06:00
/// Wrapper around a relative [`PathBuf`].
2020-06-22 08:03:37 -05:00
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct RelPathBuf(PathBuf);
impl From<RelPathBuf> for PathBuf {
fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf {
path_buf
}
}
impl ops::Deref for RelPathBuf {
type Target = RelPath;
fn deref(&self) -> &RelPath {
self.as_path()
}
}
impl AsRef<Path> for RelPathBuf {
fn as_ref(&self) -> &Path {
self.0.as_path()
}
}
impl TryFrom<PathBuf> for RelPathBuf {
type Error = PathBuf;
fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> {
if !path_buf.is_relative() {
return Err(path_buf);
}
Ok(RelPathBuf(path_buf))
}
}
impl TryFrom<&str> for RelPathBuf {
type Error = PathBuf;
fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> {
RelPathBuf::try_from(PathBuf::from(path))
}
}
impl RelPathBuf {
2021-01-22 08:24:32 -06:00
/// Coerces to a `RelPath` slice.
///
/// Equivalent of [`PathBuf::as_path`] for `RelPathBuf`.
2020-06-22 08:03:37 -05:00
pub fn as_path(&self) -> &RelPath {
RelPath::new_unchecked(self.0.as_path())
}
}
2021-01-22 08:24:32 -06:00
/// Wrapper around a relative [`Path`].
2020-06-22 08:03:37 -05:00
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct RelPath(Path);
impl AsRef<Path> for RelPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl RelPath {
2021-01-22 08:24:32 -06:00
/// Creates a new `RelPath` from `path`, without checking if it is relative.
2020-06-22 08:03:37 -05:00
pub fn new_unchecked(path: &Path) -> &RelPath {
unsafe { &*(path as *const Path as *const RelPath) }
}
}
/// Taken from <https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85>
fn normalize_path(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}