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 Into<PathBuf>) -> Result<Self, Error>
pub fn try_new(path: impl Into<PathBuf>) -> Result<Self, Error>
Creates file paths relative to the executable location.
This method accepts any type that can be converted into a PathBuf,
allowing for efficient ownership transfer when possible.
Behavior with 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
§Arguments
path- A path that will be resolved relative to the executable. Can be&str,String,&Path,PathBuf, etc.
§Examples
use app_path::AppPath;
use std::path::PathBuf;
// Relative paths are resolved relative to the executable
let config = AppPath::try_new("config.toml")?;
let data = AppPath::try_new("data/users.db")?;
// Absolute paths are used as-is (portable apps usually want relative paths)
let system_config = AppPath::try_new("/etc/app/config.toml")?;
let temp_file = AppPath::try_new(r"C:\temp\cache.dat")?;
// From String (moves ownership)
let filename = "logs/app.log".to_string();
let logs = AppPath::try_new(filename)?; // filename is moved
// From PathBuf (moves ownership)
let path_buf = PathBuf::from("cache/data.bin");
let cache = AppPath::try_new(path_buf)?; // path_buf is moved
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.
For other path types like PathBuf, use AppPath::try_new directly.
§Examples
use app_path::AppPath;
use std::convert::TryFrom;
use std::path::PathBuf;
// From &str
let config = AppPath::try_from("config.toml")?;
// From String (moves ownership)
let data_file = "data/users.db".to_string();
let data = AppPath::try_from(data_file)?;
// For PathBuf, use try_new directly (moves ownership)
let path_buf = PathBuf::from("logs/app.log");
let logs = AppPath::try_new(path_buf)?;