pub struct AppPath { /* private fields */ }Expand description
Creates paths relative to the executable location for applications.
All files and directories stay together with the executable, making your application truly portable. Perfect for:
- Portable applications that run from USB drives
- Development tools that should work anywhere
- Corporate environments where you can’t install software
§Examples
use app_path::AppPath;
// Basic usage
let config = AppPath::try_new("settings.toml")?;
let data_dir = AppPath::try_new("data")?;
// Check if files exist
if config.exists() {
let settings = std::fs::read_to_string(config.path())?;
}
// Create directories
data_dir.create_dir_all()?;
Implementations§
Source§impl AppPath
impl AppPath
Sourcepub fn try_new(path: impl AsRef<Path>) -> Result<Self, Error>
pub fn try_new(path: impl AsRef<Path>) -> Result<Self, Error>
Creates file paths relative to the executable location.
§Arguments
path- A relative path that will be resolved relative to the executable
§Returns
Returns Ok(AppPath) on success, or an std::io::Error if the executable
directory cannot be determined.
§Examples
use app_path::AppPath;
let config = AppPath::try_new("config.toml")?;
let nested = AppPath::try_new("data/users.db")?;
Sourcepub fn with_base(self, base: impl AsRef<Path>) -> Self
pub fn with_base(self, base: impl AsRef<Path>) -> Self
Override the base directory (useful for testing or custom layouts).
This method allows you to specify a different base directory instead of using the executable’s directory. Useful for testing or when you want a different layout.
§Arguments
base- The new base directory to use
§Examples
use app_path::AppPath;
use std::env;
let config = AppPath::try_new("config.toml")?
.with_base(env::temp_dir());
Sourcepub fn input(&self) -> &Path
pub fn input(&self) -> &Path
Get the original input path (before resolution).
Returns the path as it was originally provided to AppPath::try_new,
before any resolution or joining with the base directory.
§Examples
use app_path::AppPath;
let app_path = AppPath::try_new("config/settings.toml")?;
assert_eq!(app_path.input().to_str(), Some("config/settings.toml"));
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Get the full resolved path.
This is the primary method for getting the actual filesystem path where your file or directory is located.
§Examples
use app_path::AppPath;
let config = AppPath::try_new("config.toml")?;
// Get the path for use with standard library functions
println!("Config path: {}", config.path().display());
// The path is always absolute
assert!(config.path().is_absolute());
Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Check if the path exists.
§Examples
use app_path::AppPath;
let config = AppPath::try_new("config.toml")?;
if config.exists() {
println!("Config file found!");
} else {
println!("Config file not found, using defaults.");
}
Sourcepub fn create_dir_all(&self) -> Result<()>
pub fn create_dir_all(&self) -> Result<()>
Create parent directories if they don’t exist.
This is equivalent to calling std::fs::create_dir_all on the
parent directory of this path.
§Examples
use app_path::AppPath;
use std::env;
// Use a temporary directory for the example
let temp_dir = env::temp_dir().join("app_path_example");
let data_file = AppPath::try_new("data/users/profile.json")?
.with_base(&temp_dir);
// Ensure the "data/users" directory exists
data_file.create_dir_all()?;
// Verify the directory was created
assert!(data_file.path().parent().unwrap().exists());
Trait Implementations§
Source§impl TryFrom<&str> for AppPath
Ergonomic fallible conversion from string types.
impl TryFrom<&str> for AppPath
Ergonomic fallible conversion from string types.
These implementations allow you to create AppPath instances directly
from strings, with proper error handling for the fallible operation.
§Examples
use app_path::AppPath;
use std::convert::TryFrom;
// From &str
let config = AppPath::try_from("config.toml")?;
// From String
let data_file = "data/users.db".to_string();
let data = AppPath::try_from(data_file)?;
// From &String
let path_string = "logs/app.log".to_string();
let logs = AppPath::try_from(&path_string)?;