forked from extern/nushell
Path expand fixes (#3505)
* Throw an error if path failed to expand Previously, it just repeated the non-expanded path. * Allow expanding non-existent paths This commit has a strange error in examples. * Specify span manually in examples; Add an example * Expand relative path without requiring cwd * Remove redundant tilde expansion This makes the tilde expansion in relative paths dependant on "dirs" feature. * Add missing example result * Adjust path expand description * Fix import error with missing feature
This commit is contained in:
@ -1,6 +1,25 @@
|
||||
use std::io;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
pub fn resolve_dots<P>(path: P) -> PathBuf
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let mut result = PathBuf::new();
|
||||
|
||||
path.as_ref()
|
||||
.components()
|
||||
.for_each(|component| match component {
|
||||
Component::ParentDir => {
|
||||
result.pop();
|
||||
}
|
||||
Component::CurDir => {}
|
||||
_ => result.push(component),
|
||||
});
|
||||
|
||||
dunce::simplified(&result).to_path_buf()
|
||||
}
|
||||
|
||||
pub fn absolutize<P, Q>(relative_to: P, path: Q) -> PathBuf
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
@ -67,7 +86,7 @@ where
|
||||
|
||||
// borrowed from here https://stackoverflow.com/questions/54267608/expand-tilde-in-rust-path-idiomatically
|
||||
#[cfg(feature = "dirs")]
|
||||
fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Option<PathBuf> {
|
||||
pub fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Option<PathBuf> {
|
||||
let p = path_user_input.as_ref();
|
||||
if !p.starts_with("~") {
|
||||
return Some(p.to_path_buf());
|
||||
|
Reference in New Issue
Block a user