[−][src]Crate path_slash
A library for converting file paths to and from "slash paths."
A "slash path" is a path whose components are always separated by /
and never \
.
On Unix-like OSes, the path separator is /
. So any conversion is not necessary.
But on Windows, the file path separator is \
, and needs to be replaced with /
. Of course, \
s used
for escaping characters should not be replaced.
For example, a file path foo\bar\piyo.txt
can be converted to/from a slash path foo/bar/piyo.txt
.
This package was inspired by Go's path/filepath.FromSlash
and path/filepath.ToSlash
.
use std::path::{Path, PathBuf}; // Trait for extending std::path::Path use path_slash::PathExt; // Trait for extending std::path::PathBuf use path_slash::PathBufExt; #[cfg(target_os = "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()), ); 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())); } #[cfg(not(target_os = "windows"))] { assert_eq!( Path::new("foo/bar/piyo.txt").to_slash(), Some("foo/bar/piyo.txt".to_string()), ); assert_eq!( Path::new("/foo/bar/piyo.txt").to_slash(), Some("/foo/bar/piyo.txt".to_string()), ); 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())); }
Traits
PathBufExt | Trait to extend |
PathExt | Trait to extend |