use pathdiff::diff_paths;
use std::{borrow::Cow, path::Path};
pub fn relative_import_path(dict_file_abs: &Path, from_dir_abs: &Path) -> String {
if let Some(relative) = diff_paths(dict_file_abs, from_dir_abs) {
let path = relative.to_string_lossy().replace('\\', "/");
if path.starts_with('.') {
path
} else {
format!("./{}", path)
}
} else {
dict_file_abs.to_string_lossy().replace('\\', "/")
}
}
fn is_normalized(path: &str) -> bool {
if path.as_bytes().contains(&b'\\') {
return false;
}
!has_upper_case_drive_letter(path)
}
fn has_upper_case_drive_letter(path: &str) -> bool {
let bytes = path.as_bytes();
bytes.len() >= 2 && bytes[1] == b':' && bytes[0].is_ascii_uppercase()
}
pub fn normalize_path(path: &str) -> Cow<'_, str> {
if is_normalized(path) {
return Cow::Borrowed(path);
}
let mut normalized = path.replace('\\', "/");
if has_upper_case_drive_letter(&normalized) {
normalized[0..1].make_ascii_lowercase();
}
Cow::Owned(normalized)
}