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 AppPath;
// All files relative to your executable
let config = new;
let database = new;
// Works like standard paths
if config.exists
// Create directories with clear intent
database.ensure_parent_dirs?; // Creates data/ directory for the file
Quick Start
use AppPath;
// Basic usage - files relative to your executable
let config = new;
let data_dir = new;
// Check if files exist
if config.exists
// Create directories as needed
data_dir.ensure_dir_exists?; // Creates data/ directory
// Works with all standard path operations
let log_file = new.join;
log_file.ensure_parent_dirs?; // Creates logs/ directory (parent of file)
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 |
Real-World Examples
Configuration Management
use AppPath;
use Deserialize;
CLI Tool with Templates
use AppPath;
Logging Setup
use AppPath;
Override API for Flexible Deployment
Deploy the same binary with different configurations:
use AppPath;
use env;
// Use environment variable if available, fallback to default
let config = with_override;
// Support multiple environment variables with priority
let data_dir = with_override_fn;
// Example usage in different environments:
// Production: APP_CONFIG not set → uses "./config.toml"
// Development: APP_CONFIG="/dev/config.toml" → uses absolute path
// CI/Testing: DATA_DIR="/tmp/test-data" → uses custom location
Path Resolution
- Relative paths → executable directory:
"config.toml"becomes./config.toml - Absolute paths → used as-is:
"/etc/app.conf"stays/etc/app.conf
This enables both portable deployment and system integration.
Directory Creation
AppPath provides clear, intuitive methods for directory creation:
use AppPath;
// For files: create parent directories
let config_file = new;
config_file.ensure_parent_dirs?; // Creates config/ directory
write?;
// For directories: create the directory itself
let cache_dir = new;
cache_dir.ensure_dir_exists?; // Creates cache/ directory
let data_file = cache_dir.join;
Method Guide:
ensure_parent_dirs()- Creates parent directories for file pathsensure_dir_exists()- Creates the path as a directory
Note: create_dir_all() is deprecated. Use the methods above for clearer intent.
Ergonomic Macro
The app_path! macro provides clean syntax for common patterns:
use app_path;
// Simple paths
let config = app_path!;
let data = app_path!;
// Environment variable override
let config = app_path!;
// Custom override
let logs = app_path!;
Error Handling
AppPath panics only on system-level failures (determining executable location). This is extremely rare and indicates unrecoverable system issues.
For libraries requiring fallible behavior:
use AppPath;
let config = try_new?;
let config = try_with_override?;
Fallible API (For Libraries)
use ;
// Handle potential errors explicitly
let config = try_new?;
match try_new
Features
- 🚀 Zero dependencies - Only standard library
- 🌍 Cross-platform - Windows, Linux, macOS
- ⚡ High performance - Static caching, minimal allocations
- 🔧 Ergonomic - Works with all path types
- 🛡️ Thread-safe - Concurrent access safe
- 📦 Portable - Entire app moves as one unit
Installation
[]
= "0.2"
For more examples and API documentation, see docs.rs/app-path.