use std::borrow::Cow;
#[must_use]
pub fn normalize_package_separator(module_name: &str) -> Cow<'_, str> {
crate::name::normalize_package_separator(module_name)
}
#[must_use]
pub fn module_name_to_path(module_name: &str) -> String {
let normalized = normalize_package_separator(module_name);
format!("{}.pm", normalized.replace("::", "/"))
}
#[must_use]
pub fn module_path_to_name(module_path: &str) -> String {
let normalized = module_path.replace('\\', "/");
let without_ext = strip_perl_extension(&normalized);
without_ext.replace('/', "::")
}
#[must_use]
pub fn file_path_to_module_name(file_path: &str) -> String {
let normalized = file_path.replace('\\', "/");
let without_ext = strip_perl_extension(&normalized);
if let Some(relative_module_path) = strip_to_lib_relative_path(without_ext) {
return module_path_to_name(relative_module_path);
}
without_ext
.rsplit('/')
.next()
.filter(|segment| !segment.is_empty())
.unwrap_or(without_ext)
.to_string()
}
fn strip_to_lib_relative_path(path: &str) -> Option<&str> {
if let Some(stripped) = path.strip_prefix("lib/") {
return Some(stripped);
}
path.rfind("/lib/").map(|lib_idx| &path[lib_idx + "/lib/".len()..])
}
fn strip_perl_extension(path: &str) -> &str {
if let Some(stripped) = path.strip_suffix(".pm") {
stripped
} else if let Some(stripped) = path.strip_suffix(".pl") {
stripped
} else {
path
}
}