Expand description

Typed Path

Crates.io Docs.rs CI RustC 1.58.1+

Provides typed variants of Path and PathBuf for Unix and Windows.

Install

[dependencies]
typed-path = "0.1"

Why?

Some applications need to manipulate Windows or UNIX paths on different platforms, for a variety of reasons: constructing portable file formats, parsing files from other platforms, handling archive formats, working with certain network protocols, and so on.

– Josh Triplett

Check out this issue of a discussion for this. The functionality actually exists within the standard library, but is not exposed!

This means that parsing a path like C:\path\to\file.txt will be parsed differently by std::path::Path depending on which platform you are on!

use std::path::Path;

fn main() {
    // On Windows, this prints out:
    //
    // * Prefix(PrefixComponent { raw: "C:", parsed: Disk(67) })
    // * RootDir
    // * Normal("path")
    // * Normal("to")
    // * Normal("file.txt")]
    //
    // But on Unix, this prints out:
    //
    // * Normal("C:\\path\\to\\file.txt")
    println!(
        "{:?}",
        Path::new(r"C:\path\to\file.txt")
            .components()
            .collect::<Vec<_>>()
    );
}

Usage

The library provides a generic Path<T> and PathBuf<T> that use [u8] and Vec<u8> underneath instead of OsStr and OsString. An encoding generic type is provided to dictate how the underlying bytes are parsed in order to support consistent path functionality no matter what operating system you are compiling against!

use typed_path::WindowsPath;

fn main() {
    // On all platforms, this prints out:
    //
    // * Prefix(PrefixComponent { raw: "C:", parsed: Disk(67) })
    // * RootDir
    // * Normal("path")
    // * Normal("to")
    // * Normal("file.txt")]
    //
    println!(
        "{:?}",
        WindowsPath::new(r"C:\path\to\file.txt")
            .components()
            .collect::<Vec<_>>()
    );
}

License

This project is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or apache-license) MIT license (LICENSE-MIT or mit-license) at your option.

Re-exports

pub use native::NativePath;
pub use native::NativePathBuf;
pub use unix::UnixEncoding;
pub use unix::UnixPath;
pub use unix::UnixPathBuf;
pub use windows::WindowsEncoding;
pub use windows::WindowsPath;
pub use windows::WindowsPathBuf;

Modules

Structs

An iterator over Path and its ancestors.

Helper struct for safely printing paths with format! and {}.

An iterator over the Components of a Path, as [[u8]] slices.

A slice of a path (akin to str).

An owned, mutable path that mirrors std::path::PathBuf, but operatings using an Encoding to determine how to parse the underlying bytes.

An error returned if the prefix was not found.

Traits

Interface representing a component in a Path

Interface of an iterator over a collection of Components

Interface to provide meaning to a byte slice such that paths can be derived

Interface to try to perform a cheap reference-to-reference conversion.

Type Definitions