location_macros/
lib.rs

1//! # location-macros
2//!
3//! A collection of macros for obtaining the absolute path of the project root.
4use crate::core::{locate, LocationKind};
5use proc_macro::TokenStream;
6
7mod core;
8mod error;
9
10/// Expands to a string literal that contains the absolute path of the root directory of the current crate.
11///
12/// Even if the crate is a workspace member, this expands to the path to the crate root, not the workspace root.
13/// To obtain the path to the workspace root, you can use [`workspace_dir!`].
14///
15/// # Examples
16///
17/// ```
18/// use location_macros::crate_dir;
19///
20/// let crate_dir = crate_dir!();
21/// println!("The current crate root is {}", crate_dir);
22/// ```
23#[proc_macro]
24pub fn crate_dir(input: TokenStream) -> TokenStream {
25    locate(LocationKind::CrateDir, input)
26}
27
28/// Expands to a string literal that contains the absolute path of the root directory of the current workspace.
29///
30/// If the current crate is not in a workspace, this expands as same as [`crate_dir!`].
31///
32/// # Examples
33///
34/// ```
35/// use location_macros::workspace_dir;
36///
37/// let workspace_dir = workspace_dir!();
38/// println!("The current workspace root is {}", workspace_dir);
39/// ```
40#[proc_macro]
41pub fn workspace_dir(input: TokenStream) -> TokenStream {
42    locate(LocationKind::WorkspaceDir, input)
43}