1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! 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 OS, the path separator is `/`. So any conversion is not necessary.
//! But on Windows, the file path separator is `\`, and needs to be replaced with `/` for converting
//! the paths to "slash paths". 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`.
//!
//! Supported Rust version is 1.38.0 or later.
//!
//! This package was inspired by Go's [`path/filepath.FromSlash`](https://golang.org/pkg/path/filepath/#FromSlash)
//! and [`path/filepath.ToSlash`](https://golang.org/pkg/path/filepath/#ToSlash).
//!
//! ```rust
//! use std::path::{Path, PathBuf};
//! use std::borrow::Cow;
//!
//! // Trait for extending std::path::Path
//! use path_slash::PathExt as _;
//! // Trait for extending std::path::PathBuf
//! use path_slash::PathBufExt as _;
//! // Trait for extending std::borrow::Cow
//! use path_slash::CowExt as _;
//!
//! #[cfg(target_os = "windows")]
//! {
//!     // Convert from `Path`
//!     assert_eq!(
//!         Path::new(r"foo\bar\piyo.txt").to_slash().unwrap(),
//!         "foo/bar/piyo.txt",
//!     );
//!
//!     // Convert to/from PathBuf
//!     let p = PathBuf::from_slash("foo/bar/piyo.txt");
//!     assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));
//!     assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
//!
//!     // Convert to/from Cow<'_, Path>
//!     let p = Cow::from_slash("foo/bar/piyo.txt");
//!     assert_eq!(p, Cow::<Path>::Owned(PathBuf::from(r"foo\bar\piyo.txt")));
//!     assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
//! }
//!
//! #[cfg(not(target_os = "windows"))]
//! {
//!     // Convert from `Path`
//!     assert_eq!(
//!         Path::new("foo/bar/piyo.txt").to_slash().unwrap(),
//!         "foo/bar/piyo.txt",
//!     );
//!
//!     // Convert to/from PathBuf
//!     let p = PathBuf::from_slash("foo/bar/piyo.txt");
//!     assert_eq!(p, PathBuf::from("foo/bar/piyo.txt"));
//!     assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
//!
//!     // Convert to/from Cow<'_, Path>
//!     let p = Cow::from_slash("foo/bar/piyo.txt");
//!     assert_eq!(p, Cow::Borrowed(Path::new("foo/bar/piyo.txt")));
//!     assert_eq!(p.to_slash().unwrap(), "foo/bar/piyo.txt");
//! }
//! ```
#![forbid(unsafe_code)]
#![warn(clippy::dbg_macro, clippy::print_stdout)]

use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};

#[cfg(target_os = "windows")]
mod windows {
    use super::*;
    use std::os::windows::ffi::OsStrExt as _;

    // Workaround for Windows. There is no way to extract raw byte sequence from `OsStr` (in `Path`).
    // And `OsStr::to_string_lossy` may cause extra heap allocation.
    pub(crate) fn ends_with_main_sep(p: &Path) -> bool {
        p.as_os_str().encode_wide().last() == Some(MAIN_SEPARATOR as u16)
    }
}

fn str_to_path(s: &str, sep: char) -> Cow<'_, Path> {
    let mut buf = String::new();

    for (i, c) in s.char_indices() {
        if c == sep {
            if buf.is_empty() {
                buf.reserve(s.len());
                buf.push_str(&s[..i]);
            }
            buf.push(MAIN_SEPARATOR);
        } else if !buf.is_empty() {
            buf.push(c);
        }
    }

    if buf.is_empty() {
        Cow::Borrowed(Path::new(s))
    } else {
        Cow::Owned(PathBuf::from(buf))
    }
}

fn str_to_pathbuf<S: AsRef<str>>(s: S, sep: char) -> PathBuf {
    let s = s
        .as_ref()
        .chars()
        .map(|c| if c == sep { MAIN_SEPARATOR } else { c })
        .collect::<String>();
    PathBuf::from(s)
    // Note: When MAIN_SEPARATOR_STR is stabilized, replace this implementation with the following:
    // PathBuf::from(s.as_ref().replace(sep, MAIN_SEPARATOR_STR))
}

/// Trait to extend [`Path`].
///
/// ```
/// # use std::path::Path;
/// # use std::borrow::Cow;
/// use path_slash::PathExt as _;
///
/// assert_eq!(
///     Path::new("foo").to_slash(),
///     Some(Cow::Borrowed("foo")),
/// );
/// ```
pub trait PathExt {
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_str`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'. Only when the replacement
    /// happens, heap allocation happens and `Cow::Owned` is returned.
    /// When the path contains non-Unicode sequence, this method returns None.
    ///
    /// ```
    /// # use std::path::Path;
    /// # use std::borrow::Cow;
    /// use path_slash::PathExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = Path::new(r"foo\bar\piyo.txt");
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = Path::new("foo/bar/piyo.txt");
    ///
    /// assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt")));
    /// ```
    fn to_slash(&self) -> Option<Cow<'_, str>>;
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_string_lossy`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'.
    /// Any non-Unicode sequences are replaced with U+FFFD.
    ///
    /// ```
    /// # use std::path::Path;
    /// use path_slash::PathExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = Path::new(r"foo\bar\piyo.txt");
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = Path::new("foo/bar/piyo.txt");
    ///
    /// assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt");
    /// ```
    fn to_slash_lossy(&self) -> Cow<'_, str>;
}

impl PathExt for Path {
    #[cfg(not(target_os = "windows"))]
    fn to_slash_lossy(&self) -> Cow<'_, str> {
        self.to_string_lossy()
    }
    #[cfg(target_os = "windows")]
    fn to_slash_lossy(&self) -> Cow<'_, str> {
        use std::path::Component;

        let mut buf = String::new();
        for c in self.components() {
            match c {
                Component::RootDir => { /* empty */ }
                Component::CurDir => buf.push('.'),
                Component::ParentDir => buf.push_str(".."),
                Component::Prefix(prefix) => {
                    buf.push_str(&prefix.as_os_str().to_string_lossy());
                    // C:\foo is [Prefix, RootDir, Normal]. Avoid C://
                    continue;
                }
                Component::Normal(s) => buf.push_str(&s.to_string_lossy()),
            }
            buf.push('/');
        }

        if !windows::ends_with_main_sep(self) && buf != "/" && buf.ends_with('/') {
            buf.pop(); // Pop last '/'
        }

        Cow::Owned(buf)
    }

    #[cfg(not(target_os = "windows"))]
    fn to_slash(&self) -> Option<Cow<'_, str>> {
        self.to_str().map(Cow::Borrowed)
    }
    #[cfg(target_os = "windows")]
    fn to_slash(&self) -> Option<Cow<'_, str>> {
        use std::path::Component;

        let mut buf = String::new();
        for c in self.components() {
            match c {
                Component::RootDir => { /* empty */ }
                Component::CurDir => buf.push('.'),
                Component::ParentDir => buf.push_str(".."),
                Component::Prefix(prefix) => {
                    buf.push_str(prefix.as_os_str().to_str()?);
                    // C:\foo is [Prefix, RootDir, Normal]. Avoid C://
                    continue;
                }
                Component::Normal(s) => buf.push_str(s.to_str()?),
            }
            buf.push('/');
        }

        if !windows::ends_with_main_sep(self) && buf != "/" && buf.ends_with('/') {
            buf.pop(); // Pop last '/'
        }

        Some(Cow::Owned(buf))
    }
}

/// Trait to extend [`PathBuf`].
///
/// ```
/// # use std::path::PathBuf;
/// use path_slash::PathBufExt as _;
///
/// assert_eq!(
///     PathBuf::from_slash("foo/bar/piyo.txt").to_slash().unwrap(),
///     "foo/bar/piyo.txt",
/// );
/// ```
pub trait PathBufExt {
    /// Convert the slash path (path separated with '/') to [`PathBuf`].
    ///
    /// Any '/' in the slash path is replaced with the file path separator.
    /// The replacements only happen on Windows since the file path separators on Unix-like OS are
    /// the same as '/'.
    ///
    /// On non-Windows OS, it is simply equivalent to [`PathBuf::from`].
    ///
    /// ```
    /// # use std::path::PathBuf;
    /// use path_slash::PathBufExt as _;
    ///
    /// let p = PathBuf::from_slash("foo/bar/piyo.txt");
    ///
    /// #[cfg(target_os = "windows")]
    /// assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// assert_eq!(p, PathBuf::from("foo/bar/piyo.txt"));
    /// ```
    fn from_slash<S: AsRef<str>>(s: S) -> Self;
    /// Convert the [`OsStr`] slash path (path separated with '/') to [`PathBuf`].
    ///
    /// Any '/' in the slash path is replaced with the file path separator.
    /// The replacements only happen on Windows since the file path separators on Unix-like OS are
    /// the same as '/'.
    ///
    /// On Windows, any non-Unicode sequences are replaced with U+FFFD while the conversion.
    /// On non-Windows OS, it is simply equivalent to [`PathBuf::from`] and there is no
    /// loss while conversion.
    ///
    /// ```
    /// # use std::path::PathBuf;
    /// # use std::ffi::OsStr;
    /// use path_slash::PathBufExt as _;
    ///
    /// let s: &OsStr = "foo/bar/piyo.txt".as_ref();
    /// let p = PathBuf::from_slash_lossy(s);
    ///
    /// #[cfg(target_os = "windows")]
    /// assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// assert_eq!(p, PathBuf::from("foo/bar/piyo.txt"));
    /// ```
    fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
    /// Convert the backslash path (path separated with '\\') to [`PathBuf`].
    ///
    /// Any '\\' in the slash path is replaced with the file path separator.
    /// The replacements only happen on non-Windows.
    fn from_backslash<S: AsRef<str>>(s: S) -> Self;
    /// Convert the [`OsStr`] backslash path (path separated with '\\') to [`PathBuf`].
    ///
    /// Any '\\' in the slash path is replaced with the file path separator.
    fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_str`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'. Only when the replacement
    /// happens, heap allocation happens and `Cow::Owned` is returned.
    /// When the path contains non-Unicode sequence, this method returns None.
    ///
    /// ```
    /// # use std::path::PathBuf;
    /// # use std::borrow::Cow;
    /// use path_slash::PathBufExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = PathBuf::from(r"foo\bar\piyo.txt");
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = PathBuf::from("foo/bar/piyo.txt");
    ///
    /// assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt")));
    /// ```
    fn to_slash(&self) -> Option<Cow<'_, str>>;
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_string_lossy`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'.
    /// Any non-Unicode sequences are replaced with U+FFFD.
    ///
    /// ```
    /// # use std::path::PathBuf;
    /// use path_slash::PathBufExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = PathBuf::from(r"foo\bar\piyo.txt");
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = PathBuf::from("foo/bar/piyo.txt");
    ///
    /// assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt");
    /// ```
    fn to_slash_lossy(&self) -> Cow<'_, str>;
}

impl PathBufExt for PathBuf {
    #[cfg(not(target_os = "windows"))]
    fn from_slash<S: AsRef<str>>(s: S) -> Self {
        PathBuf::from(s.as_ref())
    }
    #[cfg(target_os = "windows")]
    fn from_slash<S: AsRef<str>>(s: S) -> Self {
        str_to_pathbuf(s, '/')
    }

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

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

    #[cfg(not(target_os = "windows"))]
    fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
        str_to_pathbuf(&s.as_ref().to_string_lossy(), '\\')
    }
    #[cfg(target_os = "windows")]
    fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
        PathBuf::from(s.as_ref())
    }

    fn to_slash(&self) -> Option<Cow<'_, str>> {
        self.as_path().to_slash()
    }

    fn to_slash_lossy(&self) -> Cow<'_, str> {
        self.as_path().to_slash_lossy()
    }
}

/// Trait to extend [`Cow`].
///
/// ```
/// # use std::borrow::Cow;
/// use path_slash::CowExt as _;
///
/// assert_eq!(
///     Cow::from_slash("foo/bar/piyo.txt").to_slash_lossy(),
///     "foo/bar/piyo.txt",
/// );
/// ```
pub trait CowExt<'a> {
    /// Convert the slash path (path separated with '/') to [`Cow`].
    ///
    /// Any '/' in the slash path is replaced with the file path separator.
    /// Heap allocation may only happen on Windows since the file path separators on Unix-like OS
    /// are the same as '/'.
    ///
    /// ```
    /// # use std::borrow::Cow;
    /// # use std::path::Path;
    /// use path_slash::CowExt as _;
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// assert_eq!(
    ///     Cow::from_slash("foo/bar/piyo.txt"),
    ///     Path::new("foo/bar/piyo.txt"),
    /// );
    ///
    /// #[cfg(target_os = "windows")]
    /// assert_eq!(
    ///     Cow::from_slash("foo/bar/piyo.txt"),
    ///     Path::new(r"foo\\bar\\piyo.txt"),
    /// );
    /// ```
    fn from_slash(s: &'a str) -> Self;
    /// Convert the [`OsStr`] slash path (path separated with '/') to [`Cow`].
    ///
    /// Any '/' in the slash path is replaced with the file path separator.
    /// Heap allocation may only happen on Windows since the file path separators on Unix-like OS
    /// are the same as '/'.
    ///
    /// On Windows, any non-Unicode sequences are replaced with U+FFFD while the conversion.
    /// On non-Windows OS, there is no loss while conversion.
    fn from_slash_lossy(s: &'a OsStr) -> Self;
    /// Convert the backslash path (path separated with '\\') to [`Cow`].
    ///
    /// Any '\\' in the slash path is replaced with the file path separator. Heap allocation may
    /// only happen on non-Windows.
    ///
    /// ```
    /// # use std::borrow::Cow;
    /// # use std::path::Path;
    /// use path_slash::CowExt as _;
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// assert_eq!(
    ///     Cow::from_backslash(r"foo\\bar\\piyo.txt"),
    ///     Path::new("foo/bar/piyo.txt"),
    /// );
    ///
    /// #[cfg(target_os = "windows")]
    /// assert_eq!(
    ///     Cow::from_backslash(r"foo\\bar\\piyo.txt"),
    ///     Path::new(r"foo\\bar\\piyo.txt"),
    /// );
    /// ```
    fn from_backslash(s: &'a str) -> Self;
    /// Convert the [`OsStr`] backslash path (path separated with '\\') to [`Cow`].
    ///
    /// Any '\\' in the slash path is replaced with the file path separator. Heap allocation may
    /// only happen on non-Windows.
    fn from_backslash_lossy(s: &'a OsStr) -> Self;
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_str`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'. Only when the replacement
    /// happens, heap allocation happens and `Cow::Owned` is returned.
    /// When the path contains non-Unicode sequences, this method returns `None`.
    ///
    /// ```
    /// # use std::path::Path;
    /// # use std::borrow::Cow;
    /// use path_slash::CowExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt"));
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = Cow::Borrowed(Path::new("foo/bar/piyo.txt"));
    ///
    /// assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt")));
    /// ```
    fn to_slash(&self) -> Option<Cow<'_, str>>;
    /// Convert the file path into slash path as UTF-8 string. This method is similar to
    /// [`Path::to_string_lossy`], but the path separator is fixed to '/'.
    ///
    /// Any file path separators in the file path are replaced with '/'.
    /// Any non-Unicode sequences are replaced with U+FFFD.
    ///
    /// ```
    /// # use std::path::Path;
    /// # use std::borrow::Cow;
    /// use path_slash::CowExt as _;
    ///
    /// #[cfg(target_os = "windows")]
    /// let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt"));
    ///
    /// #[cfg(not(target_os = "windows"))]
    /// let s = Cow::Borrowed(Path::new("foo/bar/piyo.txt"));
    ///
    /// assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt");
    /// ```
    fn to_slash_lossy(&self) -> Cow<'_, str>;
}

impl<'a> CowExt<'a> for Cow<'a, Path> {
    #[cfg(not(target_os = "windows"))]
    fn from_slash(s: &'a str) -> Self {
        Cow::Borrowed(Path::new(s))
    }
    #[cfg(target_os = "windows")]
    fn from_slash(s: &'a str) -> Self {
        str_to_path(s, '/')
    }

    #[cfg(not(target_os = "windows"))]
    fn from_slash_lossy(s: &'a OsStr) -> Self {
        Cow::Borrowed(Path::new(s))
    }
    #[cfg(target_os = "windows")]
    fn from_slash_lossy(s: &'a OsStr) -> Self {
        match s.to_string_lossy() {
            Cow::Borrowed(s) => str_to_path(s, '/'),
            Cow::Owned(s) => Cow::Owned(str_to_pathbuf(&s, '/')),
        }
    }

    #[cfg(not(target_os = "windows"))]
    fn from_backslash(s: &'a str) -> Self {
        str_to_path(s, '\\')
    }
    #[cfg(target_os = "windows")]
    fn from_backslash(s: &'a str) -> Self {
        Cow::Borrowed(Path::new(s))
    }

    #[cfg(not(target_os = "windows"))]
    fn from_backslash_lossy(s: &'a OsStr) -> Self {
        match s.to_string_lossy() {
            Cow::Borrowed(s) => str_to_path(s, '\\'),
            Cow::Owned(s) => Cow::Owned(str_to_pathbuf(&s, '\\')),
        }
    }
    #[cfg(target_os = "windows")]
    fn from_backslash_lossy(s: &'a OsStr) -> Self {
        Cow::Borrowed(Path::new(s))
    }

    fn to_slash(&self) -> Option<Cow<'_, str>> {
        self.as_ref().to_slash()
    }

    fn to_slash_lossy(&self) -> Cow<'_, str> {
        self.as_ref().to_slash_lossy()
    }
}