simple_expand_tilde/lib.rs
1use std::path::*;
2
3/// Expand the tilde (`~`) from within the provided path.
4pub fn expand_tilde(path: impl AsRef<Path>) -> Option<PathBuf> {
5 let p = path.as_ref();
6
7 let expanded = if p.starts_with("~") {
8 #[allow(deprecated)]
9 let mut base = std::env::home_dir()?;
10
11 if !p.ends_with("~") {
12 base.extend(p.components().skip(1));
13 }
14 base
15 } else {
16 p.to_path_buf()
17 };
18 Some(expanded)
19}