#path #ci #conversion #file #convert #tiny #slash

path-slash

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

6 releases

0.1.4 Jan 15, 2021
0.1.3 Jul 1, 2020
0.1.2 Jun 16, 2020
0.1.1 Jan 12, 2019
0.0.1 Dec 30, 2018

#20 in Filesystem

Download history 7775/week @ 2020-11-04 9359/week @ 2020-11-11 10470/week @ 2020-11-18 9944/week @ 2020-11-25 13219/week @ 2020-12-02 13012/week @ 2020-12-09 9189/week @ 2020-12-16 4772/week @ 2020-12-23 9327/week @ 2020-12-30 13002/week @ 2021-01-06 12828/week @ 2021-01-13 12912/week @ 2021-01-20 13504/week @ 2021-01-27 13077/week @ 2021-02-03 10513/week @ 2021-02-10 12147/week @ 2021-02-17

41,874 downloads per month
Used in 22 crates (15 directly)

MIT license

26KB
349 lines

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

crates.io documentation CI

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. So any conversion is not necessary. But on Windows, file path separator \ needs to be replaced with slash / (and of course \s for escaping characters should not be replaced).

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

Usage

path_slash::PathExt and path_slash::PathBufExt traits are defined. By using them, std::path::Path and std::path::PathBuf gains some methods and associated functions

  • PathExt
    • Path::to_slash(&self) -> Option<String>
    • Path::to_slash_lossy(&self) -> String
  • PathBufExt
    • PathBuf::from_slash<S: AsRef<str>>(s: S) -> PathBuf
    • PathBuf::from_slash_lossy<S: AsRef<OsStr>>(s: S) -> PathBuf
    • PathBuf::to_slash(&self) -> Option<String>
    • PathBuf::to_slash_lossy(&self) -> String
fn example_path_ext() {
    // Trait for extending std::path::Path
    use path_slash::PathExt;

    // On Windows
    assert_eq!(
        Path::new(r"foo\bar\piyo.txt").to_slash(),
        Some("foo/bar/piyo.txt".to_string()),
    );
    assert_eq!(
        Path::new(r"C:\foo\bar\piyo.txt").to_slash(),
        Some("C:/foo/bar/piyo.txt".to_string()),
    );
}

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

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

Please read documents for more details.

Installation

Add path-slash to dependencies:

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

License

the MIT License

No runtime deps