Skip to main content

perl_module/token/
mod.rs

1//! Boundary-safe Perl module token replacement helpers.
2//!
3//! Handles canonical (`Foo::Bar`) and legacy (`Foo'Bar`) separator variants
4//! and delegates standalone token scanning to the boundary module.
5
6use crate::boundary::{contains_standalone_module_token, find_standalone_module_token_ranges};
7
8/// Build canonical + legacy module rename pairs.
9///
10/// The returned vector always includes the canonical `::` pair. It also
11/// includes the legacy `'` pair when it differs.
12pub use crate::name::module_variant_pairs;
13
14/// Returns `true` when `line` contains `module_name` as a standalone module
15/// token, respecting module boundaries.
16#[must_use]
17pub fn contains_module_token(line: &str, module_name: &str) -> bool {
18    contains_standalone_module_token(line, module_name)
19}
20
21/// Replace standalone `from` module token occurrences in `line` with `to`.
22///
23/// Returns `(rewritten_line, changed)`.
24#[must_use]
25pub fn replace_module_token(line: &str, from: &str, to: &str) -> (String, bool) {
26    if from.is_empty() || line.is_empty() {
27        return (line.to_string(), false);
28    }
29
30    let mut ranges = find_standalone_module_token_ranges(line, from).peekable();
31    if ranges.peek().is_none() {
32        return (line.to_string(), false);
33    }
34
35    let mut out = String::with_capacity(line.len());
36    let mut cursor = 0usize;
37
38    for range in ranges {
39        out.push_str(&line[cursor..range.start]);
40        out.push_str(to);
41        cursor = range.end;
42    }
43
44    out.push_str(&line[cursor..]);
45    (out, true)
46}