use std::path::{
Component,
Path,
PathBuf,
};
pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
let ends_with_slash = path.as_ref().to_str().is_some_and(|s| s.ends_with('/'));
let mut normalized = PathBuf::new();
for component in path.as_ref().components() {
match &component {
Component::ParentDir => {
if !normalized.pop() {
normalized.push(component);
}
}
_ => {
normalized.push(component);
}
}
}
if ends_with_slash {
normalized.push("");
}
normalized
}
#[cfg(test)]
mod path_normalize_tests {
use super::normalize_path;
fn check(
before: &str,
after: &str,
) {
println!("-----------------\nnormalizing {before:?}");
assert_eq!(normalize_path(before).to_string_lossy(), after);
}
#[test]
fn test_path_normalization() {
check("/abc/test/../thing.png", "/abc/thing.png");
check("/abc/def/../../thing.png", "/thing.png");
check("/home/dys/test", "/home/dys/test");
check("/home/dys", "/home/dys");
check("/home/dys/", "/home/dys/");
check("/home/dys/..", "/home");
check("/home/dys/../", "/home/");
check("/..", "/..");
check("../test", "../test");
check("/home/dys/../../../test", "/../test");
check("π/2", "π/2");
check(
"/home/dys/dev/broot/../../../canop/test",
"/home/canop/test",
);
}
}