Skip to main content

SimplePath

Struct SimplePath 

Source
pub struct SimplePath {
    pub disallow_long: bool,
    pub allow_unknown_unc: bool,
    pub map_to_drive: bool,
    pub skip_dunce: bool,
    pub _unused: bool,
}
Expand description

Simplifies Win32 File Namespaces paths (the “\\?\” prefix) for better readability and compatibility.

The following code is a snap-in replacement of fs::canonicalize.

SimplePath::default().canonicalize(path);

If you have net use Z: \\server\share:

C:\dirZ:\x
fs::canonicalize\\?\C:\dir\\?\UNC\server\share\x
SimplePathC:\dir\\server\share\x
SimplePath with map_to_driveC:\dirZ:\x

Fields§

§disallow_long: bool

Disallow simplifications if the result is a “long path” (longer than 260 characters). Initially false.

Long paths may not be supported by some programs and APIs. In such cases, using the Win32 File Namespaces (the “\\?\” prefix) can often work around the limitation. Setting this option to true can improve the compatibility with such cases.

On the other hand, some other programs such as PowerShell v7 can handle long paths, but they can’t handle the “\\?\” prefix. They work best with false.

Please also see the Maximum Path Length Limitation.

§allow_unknown_unc: bool

Simplify all long UNC paths (prefixed by “\\?\UNC\”). Initially false.

Technically speaking, since the “\\?\” prefix (Win32 File Namespaces) disables all string parsing and sends the following string directly to the file system, simplifying the path is not always guaranteed to be safe or equivalent.

For this reason, the SimplePath simplifies connected network shares only by default. Set this option to true to simplify all paths prefixed by “\\?\UNC\”.

Please also see the safety note.

§Examples

let path = Path::new(r"\\?\UNC\server\share\dir");
let simple = SimplePath { allow_unknown_unc: true, ..Default::default() };
#[cfg(windows)]
assert_eq!(&*simple.simplify(path).unwrap().unwrap(), r"\\server\share\dir");
§map_to_drive: bool

Map to network share drive names when possible. Initially false.

§Examples

let path = "file.txt";
let simple = SimplePath { map_to_drive: true, ..Default::default() };
let canonicalized = simple.canonicalize(path)?;

If the file.txt is in a network drive, the result is Z:\dir\file.txt instead of \\server\share\dir\file.txt.

The following code tries to preserve the original form of the path.

SimplePath {
    map_to_drive: !path.as_os_str().as_encoded_bytes().starts_with(br"\\"),
    ..Default::default()
}.canonicalize(path)?;
§skip_dunce: bool

Skip the dunce simplification. Initially false.

§_unused: bool

It is highly recommended to always use , ..Default::default(). Otherwise builds fail when new fields are added.

This field is not used in any ways, but exists to allow using , ..Default::default() even when all other fields are specified.

Implementations§

Source§

impl SimplePath

Source

pub fn canonicalize(&self, path: impl AsRef<Path>) -> Result<PathBuf>

A snap-in replacement for fs::canonicalize. It calls fs::canonicalize and simplify.

On other platforms than Windows, this is equivalent to fs::canonicalize.

§Examples
use simple_path::SimplePath;
let canonicalized = SimplePath::default().canonicalize(path)?;
println!("{}", canonicalized.display());
Source

pub fn simplify<'a>(&self, path: &'a Path) -> Result<Option<Cow<'a, Path>>>

Try to simplify the given path.

Returns Ok(None) if no simplification is applied, or on other platforms than Windows.

Source

pub fn refresh() -> Result<()>

Refreshes the cached information.

Source

pub fn display<'a>(&'a self, path: &'a Path) -> Display<'a>

Returns an object that implements Display for printing simplified paths.

§Examples
let path = Path::new("file").canonicalize()?;
println!("{}", SimplePath::default().display(&path));
Source

pub fn strip_prefix( path: &Path, base: impl AsRef<Path>, ) -> Result<&Path, StripPrefixError>

A snap-in replacement for Path::strip_prefix with a fix for a leading directory separator “\” left for UNC paths on Windows.

§Examples
SimplePath::strip_prefix(path, base)

Trait Implementations§

Source§

impl Clone for SimplePath

Source§

fn clone(&self) -> SimplePath

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SimplePath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SimplePath

Source§

fn default() -> SimplePath

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.