app-path
Create portable applications that keep files together with the executable.
Simple, zero-dependency library for creating portable applications where configuration, data, and executable stay together as a deployable unit.
use app_path;
// Files relative to your executable - not current directory!
let config = app_path!; // → /path/to/exe/config.toml
let database = app_path!; // → /path/to/exe/data/users.db
// Acts just like std::path::Path
if config.exists
// Environment override magic for deployment ✨
let logs = app_path!;
// → Uses LOG_PATH if set, otherwise /path/to/exe/logs/app.log
database.ensure_parent_dirs?; // Creates data/ directory if it doesn't exist
Why Choose AppPath?
| Approach | Problem | AppPath Solution |
|---|---|---|
current_dir() |
Depends on where user runs program | ✅ Always relative to executable |
| System directories | Scatters files across system | ✅ Self-contained, portable |
| Hardcoded paths | Breaks when moved | ✅ Works anywhere |
API Overview
The app_path! Macro (Recommended)
use app_path;
// Simple paths
let config = app_path!;
// → /path/to/exe/config.toml
let database = app_path!;
// → /path/to/exe/data/users.db
// Environment variable overrides for deployment
let logs = app_path!;
// → Uses LOG_PATH if set, otherwise /path/to/exe/logs/app.log
let cache = app_path!;
// → Uses CACHE_DIR if set, otherwise /path/to/exe/cache
// Custom override logic with block expression
let data_dir = app_path!;
// → Uses DATA_DIR, then XDG_DATA_HOME/myapp, finally /path/to/exe/data
// Function-based override (great for XDG support)
let config_dir = app_path!;
// → /home/user/.config/myapp (Linux) or /path/to/exe/config (fallback)
// Simple override with optional value
let config = app_path!;
// → Uses CONFIG_PATH if set, otherwise /path/to/exe/config.toml
// Variable capturing in complex expressions
let version = "1.0";
let versioned_cache = app_path!;
// → /path/to/exe/cache-1.0
let temp_with_env = app_path!;
// → Uses TEMP_DIR if set, otherwise /path/to/exe/temp-1.0
// Directory creation with clear intent
logs.ensure_parent_dirs?; // Creates logs/ for the file
app_path!.ensure_dir_exists?; // Creates temp/ directory itself
Fallible try_app_path! Macro (Libraries)
For libraries or applications requiring explicit error handling:
use ;
// Returns Result<AppPath, AppPathError> instead of panicking
let config = try_app_path!?;
// → Ok(/path/to/exe/config.toml) or Err(AppPathError)
let database = try_app_path!?;
// → Ok with DATABASE_PATH or default path, or Err(AppPathError)
// Variable capturing with error handling
let version = "1.0";
let versioned_cache = try_app_path!?;
// → Ok(/path/to/exe/cache-1.0) or Err(AppPathError)
let temp_with_env = try_app_path!?;
// → Ok with TEMP_DIR or default path, or Err(AppPathError)
// Same syntax, graceful error handling
match try_app_path!
// → Either creates logs/ directory or prints error message
Constructor API (Alternative)
use AppPath;
let config = new;
// → /path/to/exe/config.toml (panics on system failure)
let database = with_override;
// → Uses DB_PATH if set, otherwise /path/to/exe/data/users.db
// For libraries requiring fallible behavior
let config = try_new?;
// → Ok(/path/to/exe/config.toml) or Err(AppPathError)
Real-World Examples
Configuration Management
use app_path;
CLI Tool with File Management
use app_path;
Deployment Flexibility
use app_path;
// Same binary, different environments:
// Development: uses "./config/app.toml"
// Production: CONFIG_PATH="/etc/myapp/config.toml" overrides to absolute path
let config = app_path!;
// Conditional deployment paths
let logs = if cfg! else ;
Directory Creation
AppPath provides intuitive methods with clear intent:
ensure_parent_dirs()- Creates parent directories for file pathsensure_dir_exists()- Creates the path as a directory
use app_path;
// Preparing to write files
let log_file = app_path!;
log_file.ensure_parent_dirs?; // Creates logs/ directory
write?;
// Creating directories
let cache_dir = app_path!;
cache_dir.ensure_dir_exists?; // Creates cache/ directory
Path Resolution
- Relative paths → executable directory:
"config.toml"→./config.toml - Absolute paths → used as-is:
"/etc/app.conf"→/etc/app.conf - Environment overrides → replace default when set
Error Handling
AppPath panics only on extremely rare system failures (executable location undetermined). For libraries requiring explicit error handling:
use ;
match try_new
Features
- 🚀 Zero dependencies - Only standard library
- ✨ Ergonomic macro - Clean syntax with
app_path! - 🌍 Cross-platform - Windows, Linux, macOS
- ⚡ High performance - Static caching, minimal allocations
- 🔧 Flexible deployment - Environment overrides
- 🛡️ Thread-safe - Concurrent access safe
- 📦 Portable - Entire app moves as one unit
Installation
[]
= "0.2"
For comprehensive API documentation, see docs.rs/app-path.