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.
See more macros: docs, repository.
Path Patterns
The path! macro provides an intuitive template engine to resolve system-standard cross-platform directories at runtime without adding heavy external dependencies.
Application Name Resolution
When appending a trailing dollar sign ($) to a token (e.g., $config$), the macro isolates data within an application-specific subdirectory.
- It checks for an ambient
APP_NAMEconstant in the calling context. - If
APP_NAMEis not found, it seamlessly falls back to theCARGO_PKG_NAMEdefined at compile time.
1. Executable & Environment Layouts
Dynamic layouts tied directly to the location of the running binary.
| Pattern | Description | Linux / macOS / Windows Target |
|---|---|---|
$ |
Exact path to the current running binary | /path/to/bin/executable |
$/ |
The directory containing the binary | /path/to/bin/ |
2. System & Configuration Layouts (Isolated Contexts)
These targets are used for internal configuration, cache, and application runtimes. They utilize hidden folders on Unix systems or specific system locations on Windows.
| Pattern | Linux / Unix Target | macOS Target | Windows Target |
|---|---|---|---|
~, $home |
/home/user |
/Users/user |
C:\Users\user |
$config |
~/.config (XDG_CONFIG_HOME) |
~/Library/Application Support |
%APPDATA% |
$share |
~/.local/share (XDG_DATA_HOME) |
~/Library/Application Support |
%LOCALAPPDATA% |
$state |
~/.local/state (XDG_STATE_HOME) |
~/Library/Application Support |
%LOCALAPPDATA% |
$cache |
~/.cache (XDG_CACHE_HOME) |
~/Library/Caches |
%LOCALAPPDATA% |
$temp |
/tmp |
/tmp |
%LOCALAPPDATA%\Temp |
3. User Directories (Open Contexts)
Standard destination folders accessible directly by the user. When application routing is applied ($token$), directories are intentionally kept unhidden (without a leading dot) across all platforms for visibility.
| Pattern | Target Subdirectory Name | Linux / macOS / Windows Target |
|---|---|---|
$downloads |
Downloads |
~/Downloads/ |
$documents |
Documents |
~/Documents/ |
$music |
Music |
~/Music/ |
$pictures |
Pictures |
~/Pictures/ |
$videos |
Videos (or Movies on macOS) |
~/Videos/ (or ~/Movies/ on macOS) |
Examples
use path;
use PathBuf;
// The application name is automatically integrated via your crate's APP_NAME constant
pub const APP_NAME: &str = "ovsy";
License & Feedback
This library is distributed under the MIT license.
You can contact me via GitHub or send a message to my E-Mail. This library is actively evolving, and your suggestions and feedback are always welcome!