#file-path #convert-file #path #slash

path-slash

Conversion to/from a file path from/to slash path

9 releases

0.2.1 Aug 6, 2022
0.2.0 Jul 5, 2022
0.1.5 Jun 29, 2022
0.1.4 Jan 15, 2021
0.1.0 Dec 30, 2018

#19 in Filesystem

Download history 107139/week @ 2024-07-21 112309/week @ 2024-07-28 119472/week @ 2024-08-04 99026/week @ 2024-08-11 102721/week @ 2024-08-18 100847/week @ 2024-08-25 100705/week @ 2024-09-01 100508/week @ 2024-09-08 94514/week @ 2024-09-15 107778/week @ 2024-09-22 98702/week @ 2024-09-29 116246/week @ 2024-10-06 110793/week @ 2024-10-13 113706/week @ 2024-10-20 102057/week @ 2024-10-27 102299/week @ 2024-11-03

435,216 downloads per month
Used in 246 crates (103 directly)

MIT license

25KB
200 lines

Rust library to convert a file path from/to slash path

crates.io documentation CI codecov-badge

path-slash is a tiny library to convert a file path (e.g. foo/bar, foo\bar or C:\foo\bar) from/to slash path (e.g. foo/bar, C:/foo/bar).

On Unix-like OS, path separator is slash / by default. One may want to convert a Windows path. But on Windows, file path separator \ needs to be replaced with slash / (and of course \s for escaping characters should not be replaced).

Supported Rust version is 1.38.0 or later.

This package was inspired by Go's path/filepath.FromSlash and path/filepath.ToSlash.

Usage

path_slash::PathExt, path_slash::PathBufExt and path_slash::CowExt traits are defined. By using them, std::path::Path, std::path::PathBuf and std::borrow::Cow<'_, Path> gain some methods and associated functions.

  • PathExt
    • Path::to_slash(&self) -> Option<Cow<'_, Path>>
    • Path::to_slash_lossy(&self) -> Cow<'_, Path>
  • PathBufExt
    • PathBuf::from_slash<S: AsRef<str>>(s: S) -> PathBuf
    • PathBuf::from_slash_lossy<S: AsRef<OsStr>>(s: S) -> PathBuf
    • PathBuf::from_backslash<S: AsRef<str>>(s: S) -> PathBuf
    • PathBuf::from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> PathBuf
    • PathBuf::to_slash(&self) -> Option<Cow<'_, Path>>
    • PathBuf::to_slash_lossy(&self) -> Cow<'_, Path>
  • CowExt
    • Cow::<Path>::from_slash(s: &str) -> Self
    • Cow::<Path>::from_slash_lossy(s: &OsStr) -> Self
    • Cow::<Path>::from_backslash(s: &str) -> Self
    • Cow::<Path>::from_backslash_lossy(s: &OsStr) -> Self
fn example_path_ext() {
    // Trait for extending std::path::Path
    use path_slash::PathExt as _;

    let p = Path::from_slash("foo/bar/piyo.txt");

    // On Windows
    assert_eq!(p, Path::new(r"foo\bar\piyo.txt"));

    // Convert to slash path
    assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
    assert_eq!(p.to_slash_lossy(), "foo/bar/piyo.txt");
}

fn example_pathbuf_ext() {
    // Trait for extending std::path::PathBuf
    use path_slash::PathBufExt as _;

    // On Windows
    let p = PathBuf::from_slash("foo/bar/piyo.txt");
    assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));

    // Convert to slash path
    assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
    assert_eq!(p.to_slash_lossy(), "foo/bar/piyo.txt");
}

fn example_cow_ext() {
    // Trait for extending std::borrow::Cow<'_, Path>
    use path_slash::CowExt as _;

    let p = Cow::from_slash("foo/bar/piyo.txt");
    // On Windows
    assert_eq!(p, Cow::Owned(PathBuf::from(r"foo\bar\piyo.txt")));
    // On non-Windows
    assert_eq!(p, Cow::Borrowed(Path::new("foo/bar/piyo.txt")));

    // Convert to slash path
    assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
    assert_eq!(p.to_slash_lossy(), "foo/bar/piyo.txt");
}

Please read documents for more details.

Installation

Add path-slash to dependencies:

[dependencies]
path-slash = "0.x"

License

the MIT License

No runtime deps