abs_file

Macro abs_file 

Source
macro_rules! abs_file {
    () => { ... };
}
Expand description

A macro that returns the absolute file path of the Rust source file in which it is invoked.

This macro ensures that the correct absolute path is resolved, even when used within a Cargo workspace or a nested crate. It prevents issues with duplicated path segments by properly handling the crate root.

§How It Works

  • file!() provides the relative path of the current source file.
  • env!("CARGO_MANIFEST_DIR") provides the absolute path of the crate root.
  • The macro joins these paths while ensuring no duplicate segments.

§Example

use abs_file_macro::abs_file;

let path = abs_file!();
println!("Absolute file path: {:?}", path);

§Edge Cases

  • If the file!() output already includes the crate name, it is stripped.
  • Ensures that the output is always an absolute path.
  • Does not depend on runtime conditions.

§Testing

This macro is tested in two ways:

  1. As part of the full workspace
    cargo test --workspace
  2. Within an individual crate
    cd test-workspace-1
    cargo test

This helps catch cases where path resolution might differ based on the working directory.