Skip to main content

file_path_to_module_name

Function file_path_to_module_name 

Source
pub fn file_path_to_module_name(file_path: &str) -> String
Expand description

Convert a filesystem source path into a likely module name.

This is intended for file-rename workflows where a concrete source path needs to map back to the module import name. It follows these rules:

  1. Strip .pm or .pl suffix
  2. If a lib/ segment exists, use everything after the last lib/
  3. Otherwise, fall back to the file stem

ยงExamples

use perl_module_path::file_path_to_module_name;

assert_eq!(file_path_to_module_name("/workspace/lib/Foo/Bar.pm"), "Foo::Bar");
assert_eq!(file_path_to_module_name("/workspace/script.pl"), "script");
assert_eq!(file_path_to_module_name(r"C:\workspace\lib\Foo\Bar.pm"), "Foo::Bar");