use std::path::{Path, PathBuf};
pub fn calculate_display_path(resolved_path: &Path, display_root: &Path) -> PathBuf {
pathdiff::diff_paths(resolved_path, display_root).unwrap_or_else(|| resolved_path.to_path_buf())
}
#[cfg(test)]
pub fn normalize_path_string(path: &str) -> String {
if path == "/" {
return "/".to_string();
}
let mut result = String::new();
let mut prev_was_slash = false;
let starts_with_slash = path.starts_with('/');
for ch in path.chars() {
if ch == '/' || ch == '\\' {
if !prev_was_slash && (!result.is_empty() || starts_with_slash) {
result.push('/');
}
prev_was_slash = true;
} else {
prev_was_slash = false;
result.push(ch);
}
}
if result.starts_with("./") {
result.drain(0..2);
}
if result.len() > 1 && result.ends_with('/') {
result.pop();
}
if result.is_empty() {
".".to_string()
} else {
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalize_path_string() {
assert_eq!(normalize_path_string("./foo/bar"), "foo/bar");
assert_eq!(normalize_path_string("foo//bar"), "foo/bar");
assert_eq!(normalize_path_string("foo/bar/"), "foo/bar");
assert_eq!(normalize_path_string("./"), ".");
assert_eq!(normalize_path_string("/"), "/");
}
#[test]
fn test_normalize_path_string_absolute_edges() {
assert_eq!(normalize_path_string("//var///log/"), "/var/log");
assert_eq!(normalize_path_string("///"), "/");
assert_eq!(normalize_path_string("/foo//bar///baz/"), "/foo/bar/baz");
assert_eq!(normalize_path_string(r".\src\main.rs"), "src/main.rs");
assert_eq!(normalize_path_string("foo//bar///"), "foo/bar");
assert_eq!(normalize_path_string("./foo//./bar"), "foo/./bar");
assert_eq!(normalize_path_string(""), ".");
assert_eq!(normalize_path_string("."), ".");
assert_eq!(normalize_path_string(".."), "..");
assert_eq!(normalize_path_string("./.."), "..");
}
#[test]
fn test_calculate_display_path() {
let resolved = PathBuf::from("/home/user/project/src/main.rs");
let display_root = PathBuf::from("/home/user/project");
let result = calculate_display_path(&resolved, &display_root);
assert_eq!(result, PathBuf::from("src/main.rs"));
}
}