include_file_path

Crates.ioinclude_file_path
lib.rsinclude_file_path
version0.1.2
sourcesrc
created_at2024-03-01 07:07:46.120691
updated_at2024-03-05 08:57:24.661461
descriptionMacro to returns absolute path of a specified file
homepage
repositoryhttps://github.com/Swoorup/include_file_path
max_upload_size
id1158574
size6,613
Swoorup Joshi (Swoorup)

documentation

https://docs.rs/include_file_path

README

include_file_path macro library

Latest Version docs.rs

This Rust project provides a procedural macro include_file_path! that returns the absolute path of a file or directory. The macro accepts both relative and absolute paths, and it will panic if the specified file does not exist.

Usage

Add the following to your Cargo.toml:

[dependencies]
include_file_path = { path = "0.1" }

Then, in your Rust code, you can use the include_file_path! macro as follows:

const FILE: &'static str = include_file_path!("src/main.rs");

This will set FILE to the absolute path of the src/main.rs file relative to the file where the macro is called.

How It Works

The include_file_path! macro works by parsing the input path and checking if it's absolute. If it is, it returns the path as is. If it's not, it concatenates the path with the directory of the file where the macro is called to get the absolute path.

The macro uses the proc_macro::Span::call_site().source_file().path() function to get the path of the file where the macro is called. Currently this function is available as of this writing only on nightly via proc_macro_span feature

Before returning the path, the macro checks if the file exists. If the file does not exist, it panics with a message indicating the file does not exist.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Commit count: 0

cargo fmt