19 lines
605 B
Rust
19 lines
605 B
Rust
use std::path::{Component, PathBuf};
|
|
|
|
pub fn canon_path_str(path: impl AsRef<std::path::Path>) -> String {
|
|
let p = path.as_ref();
|
|
let mut normalized = PathBuf::new();
|
|
for component in p.components() {
|
|
match component {
|
|
Component::Prefix(p) => normalized.push(p.as_os_str()),
|
|
Component::RootDir => normalized.push("/"),
|
|
Component::CurDir => {}
|
|
Component::ParentDir => {
|
|
normalized.pop();
|
|
}
|
|
Component::Normal(c) => normalized.push(c),
|
|
}
|
|
}
|
|
normalized.to_string_lossy().into_owned()
|
|
}
|