Crates.io | repo_path |
lib.rs | repo_path |
version | 1.2.3 |
created_at | 2025-01-12 20:02:07.124107+00 |
updated_at | 2025-09-24 11:25:49.039081+00 |
description | Access paths in your repository, with compile-time checks. |
homepage | |
repository | https://codeberg.org/trem/repo_path/ |
max_upload_size | |
id | 1513568 |
size | 10,789 |
Access paths in your repository, with compile-time checks.
This is useful in particular when you're writing your CI code in Rust, e.g. via the cargo-xtask pattern.
use repo_path::repo_path;
let readme = repo_path!("README.md");
let assets_dir = repo_path!("assets/");
let repo_root = repo_path!(); //equivalent to repo_path!(".")
The macro checks whether the path exists and constructs a PathBuf
containing an absolute path.
If the path does not exist, the compiler will error during compilation:
use repo_path::repo_path;
let non_existent = repo_path!("non/existent/path.txt");
This allows you to move files around without having to worry that a reference silently breaks, similar to the way std::include_str!()
works.
If you'd like to paths to be relative to a different directory in your repository, you can generate your own macro with custom_base_path_macro!()
:
use repo_path::custom_base_path_macro;
custom_base_path_macro!(assets_path, "assets/"); //generates the assets_path!() macro
let file = assets_path!("file.txt");
This is useful when you need to reference a particular sub-directory often or in case your Rust code is placed in a sub-directory of the repository.
During compilation, the macro looks for the .git
directory to determine the root of your repository.
Then it joins the path you specified to the root (or your custom base path) and checks if the resulting path exists and lies within your repository.
The path of your repository root is cached during compilation, so that you can use repo_path!()
many times without a significant compile-time burden.
Because the macro fetches the path during compilation, you cannot use it in libraries that want to access your user's repository (it will return paths for your own repository).
For that purpose, use the repo_path_lib crate instead.
See the changelog here: https://codeberg.org/trem/repo_path/src/branch/main/CHANGELOG.md