#unicode #utf8 #paths #filesystem

typed-path

Provides typed variants of Path and PathBuf for Unix and Windows

6 releases

0.3.2 Mar 27, 2023
0.3.1 Mar 14, 2023
0.3.0 Feb 15, 2023
0.2.1 Dec 12, 2022
0.1.0 Aug 25, 2022

#101 in Filesystem

Download history 163/week @ 2023-02-09 178/week @ 2023-02-16 110/week @ 2023-02-23 260/week @ 2023-03-02 271/week @ 2023-03-09 172/week @ 2023-03-16 146/week @ 2023-03-23 317/week @ 2023-03-30 182/week @ 2023-04-06 153/week @ 2023-04-13 412/week @ 2023-04-20 378/week @ 2023-04-27 648/week @ 2023-05-04 412/week @ 2023-05-11 624/week @ 2023-05-18 131/week @ 2023-05-25

1,840 downloads per month
Used in 5 crates (4 directly)

MIT/Apache

395KB
7K SLoC

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.3"

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

Byte paths

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<_>>()
    );
}

UTF8-enforced paths

Alongside the byte paths, this library also supports UTF8-enforced paths through UTF8Path<T> and UTF8PathBuf<T>, which internally use str and String. An encoding generic type is provided to dictate how the underlying characters are parsed in order to support consistent path functionality no matter what operating system you are compiling against!

use typed_path::Utf8WindowsPath;

fn main() {
    // On all platforms, this prints out:
    //
    // * Prefix(Utf8WindowsPrefixComponent { raw: "C:", parsed: Disk(67) })
    // * RootDir
    // * Normal("path")
    // * Normal("to")
    // * Normal("file.txt")]
    //
    println!(
        "{:?}",
        Utf8WindowsPath::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.

No runtime deps