resolve-path 0.1.0

Easily resolve tilde paths and relative paths
Documentation
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
//! A crate for resolving relative (`./`) and tilde paths (`~/`) in Rust.
//!
//! Note that this does not perform _path canonicalization_, i.e. it will
//! not eliminate segments like `..` or `./././` in a path. This crate
//! is intended simply to anchor relative paths such that they have an
//! absolute path from the root.
//!
//! # Motivation
//!
//! Rust has `Path` and `PathBuf` in the standard library for working with
//! file paths, but unfortunately there is no easy and ergonomic way to
//! resolve relative paths in the following ways:
//!
//! - with respect to the process current-working-directory (CWD)
//! - with respect to the active user's home directory (`~/`)
//! - with respect to a user-provided absolute path
//!
//! # API
//!
//! This crate provides an extension trait [`PathResolveExt`] with extension
//! methods for path-like types. The following methods are provided:
//!
//! ## `resolve` and `try_resolve`
//!
//! These methods will resolve relative paths (`./...`) with respect to the
//! process current-working-directory, and will also resolve tilde-paths (`~/...`)
//! to the active user's home directory.
//!
//! Assuming a home directory of `/home/user` and a CWD of `/home/user/Documents`,
//! the `resolve` methods will evaluate in the following ways:
//!
//! ```no_run
//! use std::path::Path;
//! use resolve_path::PathResolveExt;
//!
//! // Direct variant (may panic)
//! assert_eq!("~/.vimrc".resolve(), Path::new("/home/user/.vimrc"));
//! assert_eq!("./notes.txt".resolve(), Path::new("/home/user/Documents/notes.txt"));
//!
//! // Try variant (returns Result)
//! assert_eq!("~/.vimrc".try_resolve().unwrap(), Path::new("/home/user/.vimrc"));
//! assert_eq!("./notes.txt".try_resolve().unwrap(), Path::new("/home/user/Documents/notes.txt"));
//! ```
//!
//! ## `resolve_in` and `try_resolve_in`
//!
//! These methods will resolve tilde-paths (`~/...`) in the normal way, but will
//! resolve relative paths (`./...`) with respect to a provided base directory.
//! This can be very useful, for example when evaluating paths given in a config
//! file with respect to the location of the config file, rather than with respect
//! to the process CWD.
//!
//! Assuming the same home directory of `/home/user` and CWD of `/home/user/Documents`,
//! the `resolve_in` methods will evaluate in the following ways:
//!
//! ```no_run
//! use std::path::Path;
//! use resolve_path::PathResolveExt;
//!
//! // Direct variant (may panic)
//! assert_eq!("~/.vimrc".resolve_in("~/.config/alacritty/"), Path::new("/home/user/.vimrc"));
//! assert_eq!("./alacritty.yml".resolve_in("~/.config/alacritty/"), Path::new("/home/user/.config/alacritty/alacritty.yml"));
//!
//! // Try variant (returns Result)
//! assert_eq!("~/.vimrc".try_resolve_in("~/.config/alacritty/").unwrap(), Path::new("/home/user/.vimrc"));
//! assert_eq!("./alacritty.yml".try_resolve_in("~/.config/alacritty/").unwrap(), Path::new("/home/user/.config/alacritty/alacritty.yml"));
//! ```
//!
//! ## Why use `Cow<Path>`?
//!
//! If any of the [`PathResolveExt`] methods are called on a path that does not
//! actually need to be resolved (i.e. a path that is already absolute), then
//! the resolver methods will simply return `Cow::Borrowed(&Path)` with the original
//! path ref within. If resolution _does_ occur, then the path will one way or another
//! be edited (e.g. by adding an absolute path prefix), and will be returned as
//! a `Cow::Owned(PathBuf)`. This way we can avoid allocation where it is unnecessary.

use std::borrow::Cow;
use std::ffi::OsStr;
use std::io::{Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};

type Result<T, E = IoError> = core::result::Result<T, E>;

/// Extension trait for resolving paths against a base path.
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use resolve_path::PathResolveExt as _;
/// assert_eq!(Path::new("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
/// ```
pub trait PathResolveExt {
    /// Resolves the path in the process's current directory
    ///
    /// # Example
    ///
    /// ```no_run
    /// use std::path::Path;
    /// use resolve_path::PathResolveExt;
    /// std::env::set_current_dir("/home/user/.config/alacritty").unwrap();
    /// let resolved = "./alacritty.yml".resolve();
    /// assert_eq!(resolved, Path::new("/home/user/.config/alacritty"));
    /// ```
    ///
    /// # Panics
    ///
    /// This function panics if:
    ///
    /// - It is unable to detect the current working directory
    /// - It is unable to resolve the home directory for a tilde (`~`)
    ///
    /// See [`try_resolve`][`PathResolveExt::try_resolve`] for a non-panicking API.
    fn resolve(&self) -> Cow<Path> {
        self.try_resolve()
            .expect("should resolve path in current directory")
    }

    /// Attempts to resolve the path in the process's current directory
    ///
    /// Returns an error if:
    ///
    /// - It is unable to detect the current working directory
    /// - It is unable to resolve the home directory for a tilde (`~`)
    fn try_resolve(&self) -> Result<Cow<Path>> {
        let cwd = std::env::current_dir()?;
        let resolved = self.try_resolve_in(&cwd)?;
        Ok(resolved)
    }

    /// Resolves this path against a given base path.
    ///
    /// # Example
    ///
    /// ```
    /// use std::path::{Path, PathBuf};
    /// use resolve_path::PathResolveExt as _;
    ///
    /// assert_eq!("./config.yml".resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
    /// assert_eq!(String::from("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
    /// assert_eq!(Path::new("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
    /// assert_eq!(PathBuf::from("./config.yml").resolve_in("/home/user/.app"), Path::new("/home/user/.app/config.yml"));
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if we attempt to resolve a `~` in either path and are
    /// unable to determine the home directory from the environment
    /// (using the `dirs` crate). See [`try_resolve_in`][`PathResolveExt::try_resolve_in`]
    /// for a non-panicking option.
    fn resolve_in<P: AsRef<Path>>(&self, base: P) -> Cow<Path> {
        self.try_resolve_in(base).expect("should resolve path")
    }

    /// Resolves this path against a given base path, returning an error
    /// if unable to resolve a home directory.
    fn try_resolve_in<P: AsRef<Path>>(&self, base: P) -> Result<Cow<Path>>;
}

impl<T: AsRef<OsStr>> PathResolveExt for T {
    fn try_resolve_in<P: AsRef<Path>>(&self, base: P) -> Result<Cow<Path>> {
        try_resolve_path(base.as_ref(), Path::new(self))
    }
}

fn try_resolve_path<'a>(base: &Path, to_resolve: &'a Path) -> Result<Cow<'a, Path>> {
    // If the path to resolve is absolute, there's no relativity to resolve
    if to_resolve.is_absolute() {
        return Ok(Cow::Borrowed(to_resolve));
    }

    // If the path to resolve has a tilde, resolve it to home and be done
    if to_resolve.starts_with(Path::new("~")) {
        let resolved = resolve_tilde(to_resolve)?;
        return Ok(resolved);
    }

    // Resolve the base path by expanding tilde if needed
    let absolute_base = if base.is_absolute() {
        base.to_owned()
    } else {
        // Attempt to resolve a tilde in the base path
        let base_resolved_tilde = resolve_tilde(base)?;
        if base_resolved_tilde.is_relative() {
            return Err(IoError::new(
                ErrorKind::InvalidData,
                "the base path must be able to resolve to an absolute path",
            ));
        }

        base_resolved_tilde.into_owned()
    };

    // If the base path points to a file, use that file's parent directory as the base
    let base_directory = match std::fs::metadata(&absolute_base) {
        Ok(meta) => {
            // If we know this path points to a file, use the file's parent dir
            if meta.is_file() {
                match absolute_base.parent() {
                    Some(parent) => parent.to_path_buf(),
                    None => {
                        return Err(IoError::new(
                            ErrorKind::NotFound,
                            "the base path points to a file with no parent directory",
                        ))
                    }
                }
            } else {
                // If we know this path points to a dir, use it
                absolute_base
            }
        }
        // If we cannot get FS metadata about this path, just use it as-is
        Err(_) => absolute_base,
    };

    let resolved = base_directory.join(to_resolve);
    Ok(Cow::Owned(resolved))
}

/// Resolve a tilde in the given path to the home directory, if a tilde is present.
///
/// - If the path does not begin with a tilde, returns the original path
/// - If the path is not valid UTF-8, returns the original path
/// - If the tilde names another user (e.g. `~user`), returns the original path
/// - Otherwise, resolves the tilde to the homedir and joins with the remaining path
///
/// # Example
///
/// ```ignore
/// # use std::path::Path;
/// # use resolve_path::resolve_tilde;
/// assert_eq!(resolve_tilde(Path::new("~")).unwrap(), Path::new("/home/test"));
/// assert_eq!(resolve_tilde(Path::new("~/.config")).unwrap(), Path::new("/home/test/.config"));
/// assert_eq!(resolve_tilde(Path::new("/tmp/hello")).unwrap(), Path::new("/tmp/hello"));
/// assert_eq!(resolve_tilde(Path::new("./configure")).unwrap(), Path::new("./configure"));
/// ```
fn resolve_tilde(path: &Path) -> Result<Cow<Path>> {
    let home = home_dir().ok_or_else(|| IoError::new(ErrorKind::NotFound, "homedir not found"))?;
    Ok(resolve_tilde_with_home(home, path))
}

/// Resolve a tilde in a given path to a _given_ home directory.
///
/// - If the path does not begin with a tilde, returns the original path
/// - If the path is not valid UTF-8, returns the original path
/// - If the tilde names another user (e.g. `~user`), returns the original path
/// - Otherwise, resolves the tilde to the homedir and joins with the remaining path
///
/// # Example
///
/// ```ignore
/// # use std::path::{Path, PathBuf};
/// # use resolve_path::resolve_tilde_with_home;
/// assert_eq!(resolve_tilde_with_home(PathBuf::from("/home/test"), Path::new("~")), Path::new("/home/test"));
/// assert_eq!(resolve_tilde_with_home(PathBuf::from("/home/test"), Path::new("~/.config")), Path::new("/home/test/.config"));
/// assert_eq!(resolve_tilde_with_home(PathBuf::from("/home/test"), Path::new("/tmp/hello")), Path::new("/tmp/hello"));
/// assert_eq!(resolve_tilde_with_home(PathBuf::from("/home/test"), Path::new("./configure")), Path::new("./configure"));
/// ```
fn resolve_tilde_with_home(home: PathBuf, path: &Path) -> Cow<Path> {
    // If this path has no tilde, return it as-is
    if !path.starts_with(Path::new("~")) {
        return Cow::Borrowed(path);
    }

    // If we have a tilde, strip it and convert the remainder to UTF-8 str slice
    let path_str = match path.to_str() {
        Some(s) => s,
        None => return Cow::Borrowed(path),
    };
    let stripped = &path_str[1..];

    // Support a solo "~" with no trailing path
    if stripped.is_empty() {
        return Cow::Owned(home);
    }

    // Support a path starting with "~/..."
    if stripped.starts_with('/') {
        let stripped = stripped.trim_start_matches('/');
        let resolved = home.join(stripped);
        return Cow::Owned(resolved);
    }

    // If we have something like "~user", return original path
    Cow::Borrowed(path)
}

#[allow(unused)]
#[cfg(not(test))]
fn home_dir() -> Option<PathBuf> {
    dirs::home_dir()
}

/// During testing, always resolve home to /home/test
#[allow(unused)]
#[cfg(test)]
fn home_dir() -> Option<PathBuf> {
    Some(PathBuf::from("/home/test"))
}

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

    #[test]
    fn test_resolve_tilde() {
        assert_eq!("~".resolve(), Path::new("/home/test"));
        assert_eq!("~".to_string().resolve(), Path::new("/home/test"));
        assert_eq!(Path::new("~").resolve(), Path::new("/home/test"));
        assert_eq!(PathBuf::from("~").resolve(), Path::new("/home/test"));
        assert_eq!(OsStr::new("~").resolve(), Path::new("/home/test"));
        assert_eq!(OsString::from("~").resolve(), Path::new("/home/test"));
    }

    #[test]
    fn test_resolve_tilde_slash() {
        assert_eq!("~/".resolve(), Path::new("/home/test"));
    }

    #[test]
    fn test_resolve_tilde_path() {
        assert_eq!(
            "~/.config/alacritty/alacritty.yml".resolve(),
            Path::new("/home/test/.config/alacritty/alacritty.yml")
        );
    }

    #[test]
    fn test_resolve_tilde_multislash() {
        assert_eq!("~/////".resolve(), Path::new("/home/test"));
    }

    #[test]
    fn test_resolve_tilde_multislash_path() {
        assert_eq!("~/////.config".resolve(), Path::new("/home/test/.config"));
    }

    #[test]
    fn test_resolve_tilde_with_relative_segments() {
        assert_eq!(
            "~/.config/../.vim/".resolve(),
            Path::new("/home/test/.config/../.vim/")
        )
    }

    #[test]
    fn test_resolve_path() {
        assert_eq!(
            "./config.yml".resolve_in("/home/user/.app"),
            Path::new("/home/user/.app/config.yml")
        );
    }

    #[test]
    fn test_resolve_path_base_trailing_slash() {
        assert_eq!(
            "./config.yml".resolve_in("/home/user/.app/"),
            Path::new("/home/user/.app/config.yml")
        );
    }

    #[test]
    fn test_resolve_path_with_tilde() {
        assert_eq!(
            "./config.yml".resolve_in("~/.app"),
            Path::new("/home/test/.app/config.yml")
        );
    }

    #[test]
    fn test_resolve_absolute_path() {
        assert_eq!(
            "/etc/nixos/configuration.nix".resolve_in("/home/usr/.app"),
            Path::new("/etc/nixos/configuration.nix")
        );
    }

    #[test]
    fn test_resolve_absolute_path2() {
        assert_eq!(
            "~/.config/alacritty/alacritty.yml".resolve_in("/tmp"),
            Path::new("/home/test/.config/alacritty/alacritty.yml")
        );
    }

    #[test]
    fn test_resolve_relative_path() {
        assert_eq!(
            "../.app2/config.yml".resolve_in("/home/user/.app"),
            Path::new("/home/user/.app/../.app2/config.yml")
        );
    }

    #[test]
    fn test_resolve_current_dir() {
        assert_eq!(".".resolve_in("/home/user"), Path::new("/home/user"));
    }

    #[test]
    fn test_resolve_cwd() {
        std::env::set_current_dir("/tmp").unwrap();
        assert_eq!("garbage.txt".resolve(), Path::new("/tmp/garbage.txt"));
    }

    #[test]
    fn test_resolve_base_file() {
        let base_path = "/tmp/path-resolve-test.txt";
        std::fs::write(base_path, "Hello!").unwrap();
        assert_eq!(
            "./other-tmp-file.txt".resolve_in(base_path),
            Path::new("/tmp/other-tmp-file.txt")
        );
    }
}