| Crates.io | include_absolute_path |
| lib.rs | include_absolute_path |
| version | 0.1.7 |
| created_at | 2024-04-04 15:24:30.22497+00 |
| updated_at | 2025-06-27 07:58:56.740913+00 |
| description | Macro to returns absolute path of a specified file or directory |
| homepage | |
| repository | https://github.com/Swoorup/include_absolute_path |
| max_upload_size | |
| id | 1196393 |
| size | 21,425 |
include_absolute_path macro libraryThis Rust project provides a procedural macro include_absolute_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.
While include_str exists, this can be useful for build/test scripts or for hot reloading files knowing that the relative path wouldn't change.
Add the following to your Cargo.toml:
[dependencies]
include_absolute_path = { path = "0.1" }
Then, in your Rust code, you can use the include_absolute_path! macro as follows:
const FILE: &'static str = include_absolute_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.
The include_absolute_path! macro also supports environment variables. You can use environment variables in the path, and they will be expanded before the path is resolved.
const HOME_DIR: &'static str = include_absolute_path!("$HOME");
This will set HOME_DIR to the absolute path of the home directory.
The include_absolute_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().local_file().path() function to get the path of the file where the macro is called. This functionality is available on stable Rust since version 1.88.0.
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.
This project is licensed under the MIT License. See the LICENSE file for details.