nu_path/
components.rs

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
//! A wrapper around `Path::components()` that preserves trailing slashes.
//!
//! Trailing slashes are semantically important for us. For example, POSIX says
//! that path resolution should always follow the final symlink if it has
//! trailing slashes. Here's a demonstration:
//!
//! ```sh
//! mkdir foo
//! ln -s foo link
//!
//! cp -r link bar      # This copies the symlink, so bar is now a symlink to foo
//! cp -r link/ baz     # This copies the directory, so baz is now a directory
//! ```
//!
//! However, `Path::components()` normalizes trailing slashes away, and so does
//! other APIs that uses `Path::components()` under the hood, such as
//! `Path::parent()`. This is not ideal for path manipulation.
//!
//! This module provides a wrapper around `Path::components()` that produces an
//! empty component when there's a trailing slash.
//!
//! You can reconstruct a path with a trailing slash by concatenating the
//! components returned by this function using `PathBuf::push()` or
//! `Path::join()`. It works because `PathBuf::push("")` will add a trailing
//! slash when the original path doesn't have one.

use std::{
    ffi::OsStr,
    path::{Component, Path},
};

use crate::trailing_slash::has_trailing_slash;

/// Like `Path::components()`, but produces an extra empty component at the end
/// when `path` contains a trailing slash.
///
/// Example:
///
/// ```
/// # use std::path::{Path, Component};
/// # use std::ffi::OsStr;
/// use nu_path::components;
///
/// let path = Path::new("/foo/bar/");
/// let mut components = components(path);
///
/// assert_eq!(components.next(), Some(Component::RootDir));
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo"))));
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("bar"))));
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
/// assert_eq!(components.next(), None);
/// ```
pub fn components(path: &Path) -> impl Iterator<Item = Component> {
    let mut final_component = Some(Component::Normal(OsStr::new("")));
    path.components().chain(std::iter::from_fn(move || {
        if has_trailing_slash(path) {
            final_component.take()
        } else {
            None
        }
    }))
}

#[cfg(test)]
mod test {
    //! We'll go through every variant of Component, with or without trailing
    //! slashes. Then we'll try reconstructing the path on some typical use cases.

    use crate::assert_path_eq;
    use std::{
        ffi::OsStr,
        path::{Component, Path, PathBuf},
    };

    #[test]
    fn empty_path() {
        let path = Path::new("");
        let mut components = crate::components(path);

        assert_eq!(components.next(), None);
    }

    #[test]
    #[cfg(windows)]
    fn prefix_only() {
        let path = Path::new("C:");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::Prefix(_))));
        assert_eq!(components.next(), None);
    }

    #[test]
    #[cfg(windows)]
    fn prefix_with_trailing_slash() {
        let path = Path::new("C:\\");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::Prefix(_))));
        assert!(matches!(components.next(), Some(Component::RootDir)));
        assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn root() {
        let path = Path::new("/");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::RootDir)));
        assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn cur_dir_only() {
        let path = Path::new(".");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::CurDir)));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn cur_dir_with_trailing_slash() {
        let path = Path::new("./");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::CurDir)));
        assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn parent_dir_only() {
        let path = Path::new("..");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::ParentDir)));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn parent_dir_with_trailing_slash() {
        let path = Path::new("../");
        let mut components = crate::components(path);

        assert!(matches!(components.next(), Some(Component::ParentDir)));
        assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
        assert_eq!(components.next(), None);
    }

    #[test]
    fn normal_only() {
        let path = Path::new("foo");
        let mut components = crate::components(path);

        assert_eq!(
            components.next(),
            Some(Component::Normal(OsStr::new("foo")))
        );
        assert_eq!(components.next(), None);
    }

    #[test]
    fn normal_with_trailing_slash() {
        let path = Path::new("foo/");
        let mut components = crate::components(path);

        assert_eq!(
            components.next(),
            Some(Component::Normal(OsStr::new("foo")))
        );
        assert_eq!(components.next(), Some(Component::Normal(OsStr::new(""))));
        assert_eq!(components.next(), None);
    }

    #[test]
    #[cfg(not(windows))]
    fn reconstruct_unix_only() {
        let path = Path::new("/home/Alice");

        let mut buf = PathBuf::new();
        for component in crate::components(path) {
            buf.push(component);
        }

        assert_path_eq!(path, buf);
    }

    #[test]
    #[cfg(not(windows))]
    fn reconstruct_unix_with_trailing_slash() {
        let path = Path::new("/home/Alice/");

        let mut buf = PathBuf::new();
        for component in crate::components(path) {
            buf.push(component);
        }

        assert_path_eq!(path, buf);
    }

    #[test]
    #[cfg(windows)]
    fn reconstruct_windows_only() {
        let path = Path::new("C:\\WINDOWS\\System32");

        let mut buf = PathBuf::new();
        for component in crate::components(path) {
            buf.push(component);
        }

        assert_path_eq!(path, buf);
    }

    #[test]
    #[cfg(windows)]
    fn reconstruct_windows_with_trailing_slash() {
        let path = Path::new("C:\\WINDOWS\\System32\\");

        let mut buf = PathBuf::new();
        for component in crate::components(path) {
            buf.push(component);
        }

        assert_path_eq!(path, buf);
    }
}