pub struct ProjectDirs { /* private fields */ }
Expand description

ProjectDirs computes the location of cache, config or data directories for a specific application, which are derived from the standard directories and the name of the project/organization.

Examples

All examples on this page are computed with a user named Alice, and a ProjectDirs struct created with ProjectDirs::from("com", "Foo Corp", "Bar App").

use directories::ProjectDirs;
if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp",  "Bar App") {
    proj_dirs.config_dir();
    // Linux:   /home/alice/.config/barapp
    // Windows: C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App
    // macOS:   /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
}

Implementations§

source§

impl ProjectDirs

source

pub fn from_path(project_path: PathBuf) -> Option<ProjectDirs>

Creates a ProjectDirs struct directly from a PathBuf value. The argument is used verbatim and is not adapted to operating system standards.

The use of ProjectDirs::from_path is strongly discouraged, as its results will not follow operating system standards on at least two of three platforms.

Use ProjectDirs::from instead.

source

pub fn from( qualifier: &str, organization: &str, application: &str ) -> Option<ProjectDirs>

Creates a ProjectDirs struct from values describing the project.

The returned value depends on the operating system and is either

  • Some, containing project directory paths based on the state of the system’s paths at the time new() was invoked, or
  • None, if no valid home directory path could be retrieved from the operating system.

To determine whether a system provides a valid $HOME path, please refer to BaseDirs::new

The use of ProjectDirs::from (instead of ProjectDirs::from_path) is strongly encouraged, as its results will follow operating system standards on Linux, macOS and Windows.

Parameters
  • qualifier – The reverse domain name notation of the application, excluding the organization or application name itself.
    An empty string can be passed if no qualifier should be used (only affects macOS).
    Example values: "com.example", "org", "uk.co", "io", ""
  • organization – The name of the organization that develops this application, or for which the application is developed.
    An empty string can be passed if no organization should be used (only affects macOS and Windows).
    Example values: "Foo Corp", "Alice and Bob Inc", ""
  • application – The name of the application itself.
    Example values: "Bar App", "ExampleProgram", "Unicorn-Programme"
source

pub fn project_path(&self) -> &Path

Returns the project path fragment used to compute the project’s cache/config/data directories. The value is derived from the ProjectDirs::from call and is platform-dependent.

source

pub fn cache_dir(&self) -> &Path

Returns the path to the project’s cache directory.

PlatformValueExample
Linux$XDG_CACHE_HOME/_project_path_ or $HOME/.cache/_project_path_/home/alice/.cache/barapp
macOS$HOME/Library/Caches/_project_path_/Users/Alice/Library/Caches/com.Foo-Corp.Bar-App
Windows{FOLDERID_LocalAppData}\_project_path_\cacheC:\Users\Alice\AppData\Local\Foo Corp\Bar App\cache
source

pub fn config_dir(&self) -> &Path

Returns the path to the project’s config directory.

PlatformValueExample
Linux$XDG_CONFIG_HOME/_project_path_ or $HOME/.config/_project_path_/home/alice/.config/barapp
macOS$HOME/Library/Application Support/_project_path_/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
Windows{FOLDERID_RoamingAppData}\_project_path_\configC:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config
source

pub fn config_local_dir(&self) -> &Path

Returns the path to the project’s local config directory.

PlatformValueExample
Linux$XDG_CONFIG_HOME/_project_path_ or $HOME/.config/_project_path_/home/alice/.config/barapp
macOS$HOME/Library/Application Support/_project_path_/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
Windows{FOLDERID_LocalAppData}\_project_path_\configC:\Users\Alice\AppData\Local\Foo Corp\Bar App\config
source

pub fn data_dir(&self) -> &Path

Returns the path to the project’s data directory.

PlatformValueExample
Linux$XDG_DATA_HOME/_project_path_ or $HOME/.local/share/_project_path_/home/alice/.local/share/barapp
macOS$HOME/Library/Application Support/_project_path_/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
Windows{FOLDERID_RoamingAppData}\_project_path_\dataC:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\data
source

pub fn data_local_dir(&self) -> &Path

Returns the path to the project’s local data directory.

PlatformValueExample
Linux$XDG_DATA_HOME/_project_path_ or $HOME/.local/share/_project_path_/home/alice/.local/share/barapp
macOS$HOME/Library/Application Support/_project_path_/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
Windows{FOLDERID_LocalAppData}\_project_path_\dataC:\Users\Alice\AppData\Local\Foo Corp\Bar App\data
source

pub fn preference_dir(&self) -> &Path

Returns the path to the project’s preference directory.

PlatformValueExample
Linux$XDG_CONFIG_HOME/_project_path_ or $HOME/.config/_project_path_/home/alice/.config/barapp
macOS$HOME/Library/Preferences/_project_path_/Users/Alice/Library/Preferences/com.Foo-Corp.Bar-App
Windows{FOLDERID_RoamingAppData}\_project_path_\configC:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config
source

pub fn runtime_dir(&self) -> Option<&Path>

Returns the path to the project’s runtime directory.

The runtime directory contains transient, non-essential data (like sockets or named pipes) that is expected to be cleared when the user’s session ends.

PlatformValueExample
Linux$XDG_RUNTIME_DIR/_project_path_/run/user/1001/barapp
macOS
Windows
source

pub fn state_dir(&self) -> Option<&Path>

Returns the path to the project’s state directory.

The state directory contains data that should be retained between sessions (unlike the runtime directory), but may not be important/portable enough to be synchronized across machines (unlike the config/preferences/data directories).

The returned value depends on the operating system and is either a Some, containing a value from the following table, or a None.

PlatformValueExample
Linux$XDG_STATE_HOME/_project_path_ or $HOME/.local/state/_project_path_/home/alice/.local/state/barapp
macOS
Windows

Trait Implementations§

source§

impl Clone for ProjectDirs

source§

fn clone(&self) -> ProjectDirs

Returns a copy 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 ProjectDirs

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.