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()orfind_module_path_in_file(). - Module root detection via
module_root_from_file(). - File-to-module-path helpers via
module_path_from_file()andmodule_path_from_file_with_root(). - Module-path-to-file mapping via
module_path_to_file().
Usage
use ;
let = get_source_info.expect;
let module_path = find_module_path.expect;
let root = module_root_from_file;
let module_path_with_root =
find_module_path_in_file.expect;
let module_file = module_path_to_file
.expect;
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 undertests/,examples/,benches/, andsrc/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.