Skip to main content

perl_module/name/
mod.rs

1//! Perl module-name separator normalization and variant helpers.
2//!
3//! Normalize and project Perl module names across canonical (`::`) and
4//! legacy (`'`) package separator forms.
5
6use std::borrow::Cow;
7
8/// Normalize legacy package separator `'` to canonical `::`.
9#[must_use]
10pub fn normalize_package_separator(module_name: &str) -> Cow<'_, str> {
11    if module_name.contains('\'') {
12        Cow::Owned(module_name.replace('\'', "::"))
13    } else {
14        Cow::Borrowed(module_name)
15    }
16}
17
18/// Project canonical package separator `::` to legacy `'`.
19#[must_use]
20pub fn legacy_package_separator(module_name: &str) -> Cow<'_, str> {
21    if module_name.contains("::") {
22        Cow::Owned(module_name.replace("::", "'"))
23    } else {
24        Cow::Borrowed(module_name)
25    }
26}
27
28/// Build canonical + legacy module rename pairs.
29///
30/// The returned vector always includes the canonical `::` pair. It also
31/// includes the legacy `'` pair when it differs.
32#[must_use]
33pub fn module_variant_pairs(old_module: &str, new_module: &str) -> Vec<(String, String)> {
34    let canonical_old = normalize_package_separator(old_module).into_owned();
35    let canonical_new = normalize_package_separator(new_module).into_owned();
36
37    let canonical = (canonical_old.clone(), canonical_new.clone());
38    let legacy = (
39        legacy_package_separator(&canonical_old).into_owned(),
40        legacy_package_separator(&canonical_new).into_owned(),
41    );
42
43    if legacy == canonical { vec![canonical] } else { vec![canonical, legacy] }
44}