Struct AppPath

Source
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

Source

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
Source

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());
Source

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"));
Source

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());
Source

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.");
}
Source

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 AsRef<Path> for AppPath

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for AppPath

Source§

fn clone(&self) -> AppPath

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AppPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for AppPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<AppPath> for PathBuf

Source§

fn from(app_path: AppPath) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<&String> for AppPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: &String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

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)?;
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for AppPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: String) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.