use std::path::{Path, PathBuf};
pub(crate) fn canonicalize(path: &Path) -> std::io::Result<PathBuf> {
std::fs::canonicalize(path).map(simplify)
}
#[cfg(windows)]
fn simplify(path: PathBuf) -> PathBuf {
let Some(text) = path.to_str() else {
return path;
};
let Some(rest) = text.strip_prefix(r"\\?\") else {
return path;
};
let bytes = rest.as_bytes();
let drive_rooted = bytes.len() >= 2
&& bytes[0].is_ascii_alphabetic()
&& bytes[1] == b':'
&& (bytes.len() == 2 || bytes[2] == b'\\');
if drive_rooted {
PathBuf::from(rest)
} else {
path
}
}
#[cfg(not(windows))]
fn simplify(path: PathBuf) -> PathBuf {
path
}