module_path_extractor 0.2.4

Derive a Rust module path from a macro call-site span
Documentation

module_path_extractor

A small helper crate for proc macros that need to resolve module paths and module files from a call-site span.

What it provides

  • Call-site file/line discovery via get_source_info().
  • Module path resolution via find_module_path() or find_module_path_in_file().
  • Module root detection via module_root_from_file().
  • File-to-module-path helpers via module_path_from_file() and module_path_from_file_with_root().
  • Module-path-to-file mapping via module_path_to_file().

Usage

use module_path_extractor::{
    find_module_path, find_module_path_in_file, get_source_info, module_path_to_file,
    module_root_from_file,
};

let (file, line) = get_source_info().expect("no call-site info");
let module_path = find_module_path(&file, line).expect("no module path");

let root = module_root_from_file(&file);
let module_path_with_root =
    find_module_path_in_file(&file, line, &root).expect("no module path");

let module_file = module_path_to_file(&module_path, &file, &root)
    .expect("module file not found");

Resolution strategy

  • Detects the package root from the current file by walking up to Cargo.toml.
  • Builds a module graph from Cargo roots (src/lib.rs, src/main.rs, direct files under tests/, examples/, benches/, and src/bin).
  • Follows file modules (mod foo;, foo.rs, foo/mod.rs) and #[path = "..."] overrides.
  • Merges inline module nesting at the target line to produce the final module path.
  • Falls back to legacy path heuristics if graph-based resolution cannot be built.

Notes

  • Works on stable Rust.
  • Module root resolution assumes a standard Cargo layout.