abs-file-macro

Crates.ioabs-file-macro
lib.rsabs-file-macro
version0.1.2
created_at2025-03-03 12:48:56.660693+00
updated_at2025-03-03 13:59:53.13673+00
descriptionA macro that returns the absolute file path of the Rust source file in which it is invoked.
homepage
repositoryhttps://github.com/jzombie/rust-abs-file-macro
max_upload_size
id1575522
size11,413
Jeremy Harris (jzombie)

documentation

README

abs-file-macro

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

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.

Install

cargo add abs-file-macro

Usage

Example

use abs_file_macro::abs_file;

let path = abs_file!();

assert!(
    std::fs::metadata(&path).is_ok(),
    "abs_file!() should point to a real file, but {:?} does not exist",
    path
);

Testing

To ensure correctness, test this macro in two ways:

  1. As part of the full workspace

    cargo test --workspace
    

    This verifies that the macro functions correctly across all workspace members.

  2. Within an individual workspace member

    cd test-workspace-1
    cargo test
    

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

License

MIT License (c) 2025 Jeremy Harris.

Commit count: 7

cargo fmt