use std::path::Path;
pub fn display_path(path: &Path) -> String {
let value = path.to_string_lossy();
#[cfg(windows)]
{
if let Some(suffix) = value.strip_prefix(r"\\?\UNC\") {
return format!(r"\\{suffix}");
}
if let Some(suffix) = value.strip_prefix(r"\\?\") {
return suffix.to_string();
}
}
value.into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn an_ordinary_path_is_unchanged() {
let path = PathBuf::from(if cfg!(windows) {
r"C:\repositories\work"
} else {
"/home/user/work"
});
assert_eq!(display_path(&path), path.to_string_lossy());
}
#[cfg(windows)]
#[test]
fn a_verbatim_path_loses_its_prefix() {
assert_eq!(
display_path(std::path::Path::new(r"\\?\C:\repositories\work")),
r"C:\repositories\work"
);
}
#[cfg(windows)]
#[test]
fn a_verbatim_unc_path_keeps_its_share() {
assert_eq!(
display_path(std::path::Path::new(r"\\?\UNC\server\share\work")),
r"\\server\share\work"
);
}
}