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
//! `path-clean` is a Rust port of the the `cleanname` procedure from the Plan 9 C library, and is similar to
//! [`path.Clean`](https://golang.org/pkg/path/#Clean) from the Go standard library. It works as follows:
//!
//! 1. Reduce multiple slashes to a single slash.
//! 2. Eliminate `.` path name elements (the current directory).
//! 3. Eliminate `..` path name elements (the parent directory) and the non-`.` non-`..`, element that precedes them.
//! 4. Eliminate `..` elements that begin a rooted path, that is, replace `/..` by `/` at the beginning of a path.
//! 5. Leave intact `..` elements that begin a non-rooted path.
//!
//! If the result of this process is an empty string, return the string `"."`, representing the current directory.
//!
//! It performs this transform lexically, without touching the filesystem. Therefore it doesn't do
//! any symlink resolution or absolute path resolution. For more information you can see ["Getting Dot-Dot
//! Right"](https://9p.io/sys/doc/lexnames.html).
//!
//! For convenience, the [`PathClean`] trait is exposed and comes implemented for [`std::path::PathBuf`].
//!
//! ```rust
//! use std::path::PathBuf;
//! use joat_path::{clean, PathClean};
//! assert_eq!(clean("hello/world/.."), "hello");
//! assert_eq!(
//!     PathBuf::from("/test/../path/").clean(),
//!     PathBuf::from("/path")
//! );
//! ```
use self::internal::PathCharacteristics;
use std::path::PathBuf;

/// The Clean trait implements a `clean` method. It's recommended you use the provided [`clean`]
/// function.
pub trait PathClean<T> {
    fn clean(&self) -> T;
}

/// PathClean implemented for PathBuf
impl PathClean<PathBuf> for PathBuf {
    fn clean(&self) -> PathBuf {
        PathBuf::from(clean(self.to_str().unwrap_or("")))
    }
}

mod internal {
    pub trait PathCharacteristics {
        /// Primary separator character for this type of path
        const CANONICAL_SEPARATOR: char;

        /// Returns true if string is any valid separator character, false otherwise
        fn is_separator(path: &str) -> bool;

        /// Returns true if string starts with any valid separator character, false otherwise
        fn starts_with_separator(path: &str) -> bool;

        /// Removes separators from end of string
        fn trim_end_matches_separator(path: &str) -> &str;

        /// Split path on separators
        fn split_on_separators(path: &str) -> Vec<&str>;
    }

    /// Characteristics for Unix-style paths
    /// * Path separator is always a single forward slash "/"
    pub struct UnixPath;

    impl PathCharacteristics for UnixPath {
        const CANONICAL_SEPARATOR: char = '/';

        fn is_separator(path: &str) -> bool {
            path == "/"
        }

        fn starts_with_separator(path: &str) -> bool {
            path.starts_with('/')
        }

        fn trim_end_matches_separator(path: &str) -> &str {
            path.trim_end_matches('/')
        }

        fn split_on_separators(path: &str) -> Vec<&str> {
            path.split('/').collect()
        }
    }

    /// Characteristics for Windows-style paths
    /// * Path separator can be a single forward slash "/" or backslash "\\"
    pub struct WindowsPath;

    impl PathCharacteristics for WindowsPath {
        const CANONICAL_SEPARATOR: char = '\\';

        fn is_separator(path: &str) -> bool {
            path == "\\" || path == "/"
        }

        fn starts_with_separator(path: &str) -> bool {
            path.starts_with(['\\', '/'])
        }

        fn trim_end_matches_separator(path: &str) -> &str {
            path.trim_end_matches(['\\', '/'])
        }

        fn split_on_separators(path: &str) -> Vec<&str> {
            path.split(['\\', '/']).collect()
        }
    }

    /// Get normalized version of special path if path is special
    ///
    /// # Arguments
    ///
    /// * `path` - Path
    pub fn special_path<P: PathCharacteristics>(path: &str) -> Option<String> {
        if P::is_separator(path) {
            return Some(P::CANONICAL_SEPARATOR.to_string());
        }
        match path {
            "" => Some(String::from(".")),
            "." => Some(String::from(".")),
            ".." => Some(String::from("..")),
            _ => None,
        }
    }

    /// Determine if path is rooted
    ///
    /// # Arguments
    ///
    /// * `path` - Path
    pub fn is_root<P: PathCharacteristics>(path: &str) -> bool {
        P::starts_with_separator(path)
    }

    /// Trim trailing path separators from end of path
    ///
    /// # Arguments
    ///
    /// * `path` - Path
    pub fn trim_end_path<P: PathCharacteristics>(path: &str) -> &str {
        P::trim_end_matches_separator(path)
    }

    /// Split path into segments based on path characteristics
    ///
    /// # Arguments
    ///
    /// * `path` - Path
    pub fn split_path_segments<P: PathCharacteristics>(path: &str) -> Vec<&str> {
        P::split_on_separators(path)
    }

    /// Join path segments to create a path based on path characteristics
    ///
    /// # Arguments
    ///
    /// * `segments` - Segments
    pub fn join_path_segments<P: PathCharacteristics>(segments: Vec<&str>) -> String {
        segments.join(&P::CANONICAL_SEPARATOR.to_string())
    }

    /// Make an absolute path based on path characteristics
    ///
    /// # Arguments
    ///
    /// * `path` - Path
    pub fn make_absolute<P: PathCharacteristics>(path: &str) -> String {
        P::CANONICAL_SEPARATOR.to_string() + path
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_special_path_unix() {
            assert_eq!(Some(String::from(".")), special_path::<UnixPath>(""));
            assert_eq!(Some(String::from(".")), special_path::<UnixPath>("."));
            assert_eq!(Some(String::from("..")), special_path::<UnixPath>(".."));
            assert_eq!(Some(String::from("/")), special_path::<UnixPath>("/"));
            assert_eq!(None, special_path::<UnixPath>("\\"));
            assert_eq!(None, special_path::<UnixPath>("aaa"))
        }

        #[test]
        fn test_special_path_windows() {
            assert_eq!(Some(String::from(".")), special_path::<WindowsPath>(""));
            assert_eq!(Some(String::from(".")), special_path::<WindowsPath>("."));
            assert_eq!(Some(String::from("..")), special_path::<WindowsPath>(".."));
            assert_eq!(Some(String::from("\\")), special_path::<WindowsPath>("/"));
            assert_eq!(Some(String::from("\\")), special_path::<WindowsPath>("\\"));
            assert_eq!(None, special_path::<WindowsPath>("aaa"))
        }

        #[test]
        fn test_is_root_unix() {
            assert!(is_root::<UnixPath>("/a"));
            assert!(!is_root::<UnixPath>("\\a"));
            assert!(!is_root::<UnixPath>("a"));
        }

        #[test]
        fn test_is_root_windows() {
            assert!(is_root::<WindowsPath>("/a"));
            assert!(is_root::<WindowsPath>("\\a"));
            assert!(!is_root::<WindowsPath>("a"));
        }

        #[test]
        fn test_trim_end_path_unix() {
            assert_eq!("aaa", trim_end_path::<UnixPath>("aaa"));
            assert_eq!("aaa", trim_end_path::<UnixPath>("aaa/"));
            assert_eq!("aaa", trim_end_path::<UnixPath>("aaa/////"));
        }

        #[test]
        fn test_trim_end_path_windows() {
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa"));
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa/"));
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa\\"));
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa/////"));
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa\\\\\\\\\\"));
            assert_eq!("aaa", trim_end_path::<WindowsPath>("aaa/\\/\\/"));
        }

        #[test]
        fn test_split_path_segments_first_empty_unix() {
            let segments = split_path_segments::<UnixPath>("/a/b/c");
            assert_eq!(4, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("a", segments[1]);
            assert_eq!("b", segments[2]);
            assert_eq!("c", segments[3])
        }

        #[test]
        fn test_split_path_segments_last_empty_unix() {
            let segments = split_path_segments::<UnixPath>("/a/b/c/");
            assert_eq!(5, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("a", segments[1]);
            assert_eq!("b", segments[2]);
            assert_eq!("c", segments[3]);
            assert_eq!("", segments[4])
        }

        #[test]
        fn test_split_path_segments_empty_unix() {
            let segments = split_path_segments::<UnixPath>("");
            assert_eq!(1, segments.len());
            assert_eq!("", segments[0])
        }

        #[test]
        fn test_split_path_segments_multiple_empty_unix() {
            let segments = split_path_segments::<UnixPath>("//");
            assert_eq!(3, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("", segments[1]);
            assert_eq!("", segments[2])
        }

        #[test]
        fn test_split_path_segments_first_empty_unix_backslashes() {
            let segments = split_path_segments::<UnixPath>("/a\\b\\c");
            assert_eq!(2, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("a\\b\\c", segments[1]);
        }

        #[test]
        fn test_split_path_segments_first_empty_windows_backslashes() {
            let segments = split_path_segments::<WindowsPath>("\\a\\b\\c");
            assert_eq!(4, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("a", segments[1]);
            assert_eq!("b", segments[2]);
            assert_eq!("c", segments[3])
        }

        #[test]
        fn test_split_path_segments_first_empty_windows_mixture() {
            let segments = split_path_segments::<WindowsPath>("/a\\b/c");
            assert_eq!(4, segments.len());
            assert_eq!("", segments[0]);
            assert_eq!("a", segments[1]);
            assert_eq!("b", segments[2]);
            assert_eq!("c", segments[3])
        }

        #[test]
        fn test_join_path_segments_unix() {
            assert_eq!("", join_path_segments::<UnixPath>(vec![]));
            assert_eq!("a/b/c", join_path_segments::<UnixPath>(vec!["a", "b", "c"]))
        }

        #[test]
        fn test_join_path_segments_windows() {
            assert_eq!("", join_path_segments::<WindowsPath>(vec![]));
            assert_eq!(
                "a\\b\\c",
                join_path_segments::<WindowsPath>(vec!["a", "b", "c"])
            )
        }

        #[test]
        fn test_make_absolute_unix() {
            assert_eq!("/aaa", make_absolute::<UnixPath>("aaa"));
            assert_eq!("//aaa", make_absolute::<UnixPath>("/aaa"));
            assert_eq!("/\\aaa", make_absolute::<UnixPath>("\\aaa"))
        }

        #[test]
        fn test_make_absolute_windows() {
            assert_eq!("\\aaa", make_absolute::<WindowsPath>("aaa"));
            assert_eq!("\\/aaa", make_absolute::<WindowsPath>("/aaa"));
            assert_eq!("\\\\aaa", make_absolute::<WindowsPath>("\\aaa"))
        }
    }
}

/// The core implementation. It performs the following, lexically:
/// 1. Reduce multiple slashes to a single slash.
/// 2. Eliminate `.` path name elements (the current directory).
/// 3. Eliminate `..` path name elements (the parent directory) and the non-`.` non-`..`, element that precedes them.
/// 4. Eliminate `..` elements that begin a rooted path, that is, replace `/..` by `/` at the beginning of a path.
/// 5. Leave intact `..` elements that begin a non-rooted path.
///
/// If the result of this process is an empty string, return the string `"."`, representing the current directory.
pub fn clean(path: &str) -> String {
    #[cfg(not(target_os = "windows"))]
    type PlatformPath = internal::UnixPath;
    #[cfg(target_os = "windows")]
    type PlatformPath = internal::WindowsPath;

    clean_core::<PlatformPath>(path)
}

pub fn clean_unix(path: &str) -> String {
    clean_core::<internal::UnixPath>(path)
}

pub fn clean_windows(path: &str) -> String {
    clean_core::<internal::WindowsPath>(path)
}

fn clean_core<P: PathCharacteristics>(path: &str) -> String {
    use internal::*;

    match special_path::<P>(path) {
        Some(s) => return s,
        _ => {}
    }

    let mut out = vec![];
    let is_root = is_root::<P>(path);

    let path = trim_end_path::<P>(path);
    let segments = split_path_segments::<P>(path);
    let num_segments = segments.len();

    for segment in segments {
        match segment {
            "" => continue,
            "." => {
                if num_segments == 1 {
                    out.push(segment);
                };
                continue;
            }
            ".." => {
                let previous = out.pop();
                if previous.is_some() && !can_backtrack(previous.unwrap()) {
                    out.push(previous.unwrap());
                    out.push(segment);
                } else if previous.is_none() && !is_root {
                    out.push(segment);
                };
                continue;
            }
            _ => {
                out.push(segment);
            }
        };
    }

    let out_str_0 = join_path_segments::<P>(out);

    let out_str_1 = if is_root {
        make_absolute::<P>(&out_str_0)
    } else {
        out_str_0
    };

    if out_str_1.is_empty() {
        ".".to_string()
    } else {
        out_str_1
    }
}

fn can_backtrack(segment: &str) -> bool {
    match segment {
        "." => false,
        ".." => false,
        _ => true,
    }
}

#[cfg(test)]
mod tests {
    use super::test_helpers::*;
    use super::{clean_unix, clean_windows, PathClean};

    use std::path::PathBuf;

    #[test]
    fn test_empty_path_is_current_dir() {
        assert_eq!(clean_unix(""), ".");
        assert_eq!(clean_windows(&to_windows("")), to_windows("."))
    }

    #[test]
    fn test_clean_paths_dont_change() {
        let tests = vec![(".", "."), ("..", ".."), ("/", "/")];

        for test in tests {
            assert_eq!(clean_unix(test.0), test.1);
            assert_eq!(clean_windows(&to_windows(test.0)), to_windows(test.1))
        }
    }

    #[test]
    fn test_replace_multiple_slashes() {
        let tests = vec![
            ("/", "/"),
            ("//", "/"),
            ("///", "/"),
            (".//", "."),
            ("//..", "/"),
            ("..//", ".."),
            ("/..//", "/"),
            ("/.//./", "/"),
            ("././/./", "."),
            ("path//to///thing", "path/to/thing"),
            ("/path//to///thing", "/path/to/thing"),
        ];

        for test in tests {
            assert_eq!(clean_unix(test.0), test.1);
            assert_eq!(clean_windows(&to_windows(test.0)), to_windows(test.1))
        }
    }

    #[test]
    fn test_eliminate_current_dir() {
        let tests = vec![
            ("./", "."),
            ("/./", "/"),
            ("./test", "test"),
            ("./test/./path", "test/path"),
            ("/test/./path/", "/test/path"),
            ("test/path/.", "test/path"),
        ];

        for test in tests {
            assert_eq!(clean_unix(test.0), test.1);
            assert_eq!(clean_windows(&to_windows(test.0)), to_windows(test.1))
        }
    }

    #[test]
    fn test_eliminate_parent_dir() {
        let tests = vec![
            ("/..", "/"),
            ("/../test", "/test"),
            ("test/..", "."),
            ("test/path/..", "test"),
            ("test/../path", "path"),
            ("/test/../path", "/path"),
            ("test/path/../../", "."),
            ("test/path/../../..", ".."),
            ("/test/path/../../..", "/"),
            ("/test/path/../../../..", "/"),
            ("test/path/../../../..", "../.."),
            ("test/path/../../another/path", "another/path"),
            ("test/path/../../another/path/..", "another"),
            ("../test", "../test"),
            ("../test/", "../test"),
            ("../test/path", "../test/path"),
            ("../test/..", ".."),
        ];

        for test in tests {
            assert_eq!(clean_unix(test.0), test.1);
            assert_eq!(clean_windows(&to_windows(test.0)), to_windows(test.1))
        }
    }

    #[test]
    fn test_pathbuf_trait() {
        assert_eq!(
            PathBuf::from("/test/../path/").clean(),
            PathBuf::from("/path")
        );
    }
}

#[cfg(test)]
mod test_helpers {
    pub fn to_windows(p: &str) -> String {
        p.replace('/', "\\")
    }
}