use std::borrow::Cow;
pub fn normalize_path(path: &str) -> Cow<'_, str> {
if path.contains('\\') || path.contains("./") {
Cow::Owned(path.replace("\\", "/").replace("./", ""))
} else {
Cow::Borrowed(path)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn returns_borrowed_for_clean_paths() {
let path = "src/main.rs";
let result = normalize_path(path);
assert!(matches!(result, Cow::Borrowed(_)));
assert_eq!(result, "src/main.rs");
}
#[test]
fn normalizes_windows_paths() {
assert_eq!(normalize_path("src\\main.rs"), "src/main.rs");
}
#[test]
fn removes_dot_slash_at_start() {
assert_eq!(normalize_path("./src/main.rs"), "src/main.rs");
}
#[test]
fn removes_dot_slash_in_middle() {
assert_eq!(
normalize_path("packages/./api/src/main.rs"),
"packages/api/src/main.rs"
);
}
#[test]
fn handles_multiple_issues() {
assert_eq!(normalize_path(".\\packages\\.\\api"), "packages/api");
}
#[test]
fn handles_empty_path() {
assert_eq!(normalize_path(""), "");
}
}