2021-08-28 14:59:09 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
fn expand_tilde_with_home(path: impl AsRef<Path>, home: Option<PathBuf>) -> PathBuf {
|
2021-08-28 14:59:09 +02:00
|
|
|
let path = path.as_ref();
|
|
|
|
|
|
|
|
if !path.starts_with("~") {
|
|
|
|
return path.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
match home {
|
|
|
|
None => path.into(),
|
|
|
|
Some(mut h) => {
|
|
|
|
if h == Path::new("/") {
|
|
|
|
// Corner case: `h` is a root directory;
|
|
|
|
// don't prepend extra `/`, just drop the tilde.
|
|
|
|
path.strip_prefix("~").unwrap_or(path).into()
|
|
|
|
} else {
|
|
|
|
if let Ok(p) = path.strip_prefix("~/") {
|
|
|
|
h.push(p)
|
|
|
|
}
|
|
|
|
h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Expand tilde ("~") into a home directory if it is the first path component
|
|
|
|
pub fn expand_tilde(path: impl AsRef<Path>) -> PathBuf {
|
|
|
|
// TODO: Extend this to work with "~user" style of home paths
|
2022-01-04 23:30:34 +01:00
|
|
|
expand_tilde_with_home(path, dirs_next::home_dir())
|
2021-08-28 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn check_expanded(s: &str) {
|
|
|
|
let home = Path::new("/home");
|
|
|
|
let buf = Some(PathBuf::from(home));
|
2022-01-04 23:30:34 +01:00
|
|
|
assert!(expand_tilde_with_home(Path::new(s), buf).starts_with(&home));
|
2021-08-28 14:59:09 +02:00
|
|
|
|
|
|
|
// Tests the special case in expand_tilde for "/" as home
|
|
|
|
let home = Path::new("/");
|
|
|
|
let buf = Some(PathBuf::from(home));
|
2022-01-04 23:30:34 +01:00
|
|
|
assert!(!expand_tilde_with_home(Path::new(s), buf).starts_with("//"));
|
2021-08-28 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_not_expanded(s: &str) {
|
|
|
|
let home = PathBuf::from("/home");
|
2022-01-04 23:30:34 +01:00
|
|
|
let expanded = expand_tilde_with_home(Path::new(s), Some(home));
|
2021-08-28 14:59:09 +02:00
|
|
|
assert!(expanded == Path::new(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_with_tilde() {
|
|
|
|
check_expanded("~");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_with_tilde_forward_slash() {
|
|
|
|
check_expanded("~/test/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_with_tilde_double_forward_slash() {
|
|
|
|
check_expanded("~//test/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_expand_tilde_if_tilde_is_not_first_character() {
|
|
|
|
check_not_expanded("1~1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[test]
|
|
|
|
fn string_with_tilde_backslash() {
|
|
|
|
check_expanded("~\\test/test2/test3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[test]
|
|
|
|
fn string_with_double_tilde_backslash() {
|
|
|
|
check_expanded("~\\\\test\\test2/test3");
|
|
|
|
}
|
|
|
|
}
|