Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add from_backslash() and _lossy() functions #8

Merged
merged 5 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Rust library to convert a file path from/to slash path
[`path-slash`][crates-io] 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
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).

Expand All @@ -25,6 +25,8 @@ and `std::path::PathBuf` gains some methods and associated functions
- `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<String>`
- `PathBuf::to_slash_lossy(&self) -> String`

Expand Down
39 changes: 39 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ impl PathExt for Path {
pub trait PathBufExt {
fn from_slash<S: AsRef<str>>(s: S) -> Self;
fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
fn from_backslash<S: AsRef<str>>(s: S) -> Self;
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
fn to_slash(&self) -> Option<String>;
fn to_slash_lossy(&self) -> String;
}
Expand Down Expand Up @@ -313,6 +315,43 @@ impl PathBufExt for PathBuf {
PathBuf::from(s)
}

/// Convert the backslash path (path separated with '\') to [`std::path::PathBuf`].
///
/// Any '\' in the slash path is replaced with the file path separator.
/// The replacements only happen on non-Windows.
#[cfg(not(target_os = "windows"))]
fn from_backslash<S: AsRef<str>>(s: S) -> Self {
use std::path;

let s = s
.as_ref()
.chars()
.map(|c| match c {
'\\' => path::MAIN_SEPARATOR,
c => c,
})
.collect::<String>();
PathBuf::from(s)
}

#[cfg(target_os = "windows")]
fn from_backslash<S: AsRef<str>>(s: S) -> Self {
PathBuf::from(s.as_ref())
}

/// Convert the backslash path (path separated with '\') to [`std::path::PathBuf`].
///
/// Any '\' in the slash path is replaced with the file path separator.
#[cfg(not(target_os = "windows"))]
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
PathBuf::from(s.as_ref().to_string_lossy().replace(r"\", "/").chars().as_str())
}

#[cfg(target_os = "windows")]
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
PathBuf::from(s.as_ref())
}

/// Convert the slash path (path separated with '/') to [`std::path::PathBuf`].
///
/// Any '/' in the slash path is replaced with the file path separator.
Expand Down