Crate app_path

Source
Expand description

§app-path

Create file paths relative to your executable for truly portable applications.

§Quick Start

use app_path::AppPath;
use std::convert::TryFrom;
use std::path::PathBuf;

// Create paths relative to your executable - accepts any path-like type
let config = AppPath::try_new("config.toml")?;
let data = AppPath::try_new("data/users.db")?;

// Efficient ownership transfer for owned types
let log_file = "logs/app.log".to_string();
let logs = AppPath::try_new(log_file)?; // String is moved

let path_buf = PathBuf::from("cache/data.bin");
let cache = AppPath::try_new(path_buf)?; // PathBuf is moved

// Works with any path-like type
let from_path = AppPath::try_new(std::path::Path::new("temp.txt"))?;

// Alternative: Use TryFrom for string types
let settings = AppPath::try_from("settings.json")?;

// Absolute paths are used as-is (for system integration)
let system_log = AppPath::try_new("/var/log/app.log")?;
let windows_temp = AppPath::try_new(r"C:\temp\cache.dat")?;

// Get the paths for use with standard library functions
println!("Config: {}", config.path().display());
println!("Data: {}", data.path().display());

// Check existence and create directories
if !logs.exists() {
    logs.create_dir_all()?;
}

§Path Resolution Behavior

AppPath intelligently handles different path types:

  • Relative paths (e.g., "config.toml", "data/file.txt") are resolved relative to the executable’s directory
  • Absolute paths (e.g., "/etc/config", "C:\\temp\\file.txt") are used as-is, ignoring the executable’s directory

This design enables both portable applications and system integration.

§Performance

AppPath is optimized for efficient ownership transfer:

  • String and PathBuf: Moved into AppPath (no cloning)
  • Generic types: Uses impl Into<PathBuf> for zero-copy where possible
  • References: Efficient conversion without unnecessary allocations

Structs§

AppPath
Creates paths relative to the executable location for applications.