PathBuf Macro
A macro for ergonomic and safe construction of file and directory paths in Rust, with context-aware resolution.
- Supports both string literals and formatted paths with arguments.
- Automatically determines whether to allow a path relative to the current working directory, the executable directory, or the user data directory.
P.S. More useful macros are available in the macron package.
Features:
- Empty call:
path!()returnsPathBuf::new(). - Executable prefix
$:path!("$")→std::env::current_exe(). - Executable directory
$/:path!("$/foo/bar")→current_exe().parent().join("foo/bar"). - Home directory
~/:path!("~/foo/bar")→$HOME/foo/bar(USERPROFILEon Windows). - User data directory
%/:OS Default Directory Windows C:/Users/User/AppData/Roaming/macOS ~/Library/Application Support/Linux ~/.local/share - Formatting:
path!("foo/{arg1}/{}", arg2). - Expressions:
path!(some_path_var). - Plain paths:
path!("foo/bar").
Examples:
use path;
// empty PathBuf:
let empty = path!;
// plain path (relative to CWD):
let relative = path!;
// executable path:
let exe_path = path!;
// executable directory:
let exe_dir_path = path!;
// home directory:
let home_path = path!;
// user data directory:
let data_path = path!;
// formatting:
let user_path = path!;
let dynamic = path!;
Licensing:
Distributed under the MIT license.
Feedback:
You can contact me via GitHub or send a message to my Telegram @fuderis. This library is actively evolving, and your suggestions and feedback are always welcome!
P.S. More useful macros are available in the macron package.