Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Aug 1, 2024
1 parent 95871a8 commit efcaca9
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 299 deletions.
14 changes: 7 additions & 7 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::PathBuf;
use super::error::MissingPrefixBufError;
use super::error::MissingPrefixError;
use super::error::ParentError;
use super::normalize;
use super::imp;
use super::PathExt;

/// A borrowed path that has a [prefix] on Windows.
Expand Down Expand Up @@ -81,7 +81,7 @@ impl BasePath {
match path {
Cow::Borrowed(path) => Self::try_new(path)
.map(Cow::Borrowed)
.or_else(|_| normalize::to_base(path).map(Cow::Owned)),
.or_else(|_| imp::to_base(path).map(Cow::Owned)),
Cow::Owned(path) => BasePathBuf::new(path).map(Cow::Owned),
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ impl BasePath {
P: AsRef<Path> + ?Sized,
{
let path = path.as_ref();
if normalize::is_base(path) {
if imp::is_base(path) {
Ok(Self::from_inner(path.as_os_str()))
} else {
Err(MissingPrefixError(()))
Expand All @@ -142,7 +142,7 @@ impl BasePath {
#[inline]
pub fn canonicalize(&self) -> io::Result<BasePathBuf> {
self.as_path().canonicalize().map(|base| {
debug_assert!(normalize::is_base(&base));
debug_assert!(imp::is_base(&base));
BasePathBuf(base)
})
}
Expand Down Expand Up @@ -511,7 +511,7 @@ impl BasePathBuf {
where
P: Into<PathBuf>,
{
Self::try_new(path).or_else(|x| normalize::to_base(&x.0))
Self::try_new(path).or_else(|x| imp::to_base(&x.0))
}

/// Equivalent to [`BasePath::try_new`] but returns an owned path.
Expand Down Expand Up @@ -539,7 +539,7 @@ impl BasePathBuf {
P: Into<PathBuf>,
{
let path = path.into();
if normalize::is_base(&path) {
if imp::is_base(&path) {
Ok(Self(path))
} else {
Err(MissingPrefixBufError(path))
Expand Down Expand Up @@ -629,7 +629,7 @@ impl BasePathBuf {
where
P: AsRef<Path>,
{
normalize::push(self, path.as_ref());
imp::push(self, path.as_ref());
}
}

Expand Down
27 changes: 26 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
use std::io;
use std::path::Path;

use crate::BasePathBuf;

#[cfg(feature = "localization")]
pub(super) mod localize;

pub(super) mod normalize;
#[inline(always)]
pub(crate) fn is_base(_: &Path) -> bool {
true
}

#[inline(always)]
pub(crate) fn to_base(_: &Path) -> io::Result<BasePathBuf> {
unreachable!();
}

pub(crate) fn normalize(path: &Path) -> io::Result<BasePathBuf> {
// This method rejects null bytes and empty paths, which is consistent with
// [GetFullPathNameW] on Windows.
path.canonicalize().and_then(BasePathBuf::new)
}

pub(crate) fn push(base: &mut BasePathBuf, path: &Path) {
if !path.as_os_str().is_empty() {
base.0.push(path);
}
}
26 changes: 0 additions & 26 deletions src/common/normalize.rs

This file was deleted.

17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ pub mod error;
mod imp;
#[cfg(feature = "localization")]
use imp::localize;
use imp::normalize;

/// Additional methods added to [`Path`].
pub trait PathExt: private::Sealed {
Expand Down Expand Up @@ -189,6 +188,10 @@ pub trait PathExt: private::Sealed {

/// Normalizes `self` relative to the current directory.
///
/// The purpose of normalization is to remove `.` and `..` components of a
/// path if possible and make it absolute. This may be necessary for
/// operations on the path string to be more reliable.
///
/// This method will access the file system to normalize the path. If the
/// path might not exist, [`normalize_virtually`] can be used instead, but
/// it is only available on Windows. Other platforms require file system
Expand All @@ -208,10 +211,10 @@ pub trait PathExt: private::Sealed {
/// - shared partition paths do not cause an error.
/// ([rust-lang/rust#52440])
///
/// However, [verbatim] paths will not be modified, so they might still
/// contain `.` or `..` components. [`BasePath::join`] and
/// [`BasePathBuf::push`] can normalize them before they become part of the
/// path.
/// [Verbatim] paths will not be modified, so they might still contain `.`
/// or `..` components. [`BasePath::join`] and [`BasePathBuf::push`] can
/// normalize them before they become part of the path. Junction points
/// will additionally not be resolved with the current implementation.
///
/// # Implementation
///
Expand Down Expand Up @@ -321,13 +324,13 @@ impl PathExt for Path {

#[inline]
fn normalize(&self) -> io::Result<BasePathBuf> {
normalize::normalize(self)
imp::normalize(self)
}

#[cfg(any(doc, windows))]
#[inline]
fn normalize_virtually(&self) -> io::Result<BasePathBuf> {
normalize::normalize_virtually(self)
imp::normalize_virtually(self)
}
}

Expand Down
Loading

0 comments on commit efcaca9

Please sign in to comment.