Struct xdg::BaseDirectories

source ·
#[non_exhaustive]
pub struct BaseDirectories { pub shared_prefix: PathBuf, pub user_prefix: PathBuf, pub data_home: Option<PathBuf>, pub config_home: Option<PathBuf>, pub cache_home: Option<PathBuf>, pub state_home: Option<PathBuf>, pub data_dirs: Vec<PathBuf>, pub config_dirs: Vec<PathBuf>, pub runtime_dir: Option<PathBuf>, }
Expand description

BaseDirectories allows to look up paths to configuration, data, cache and runtime files in well-known locations according to the X Desktop Group Base Directory specification.

The Base Directory specification defines five kinds of files:

  • Configuration files store the application’s settings and are often modified during runtime;
  • Data files store supplementary data, such as graphic assets, precomputed tables, documentation, or architecture-independent source code;
  • Cache files store non-essential, transient data that provides a runtime speedup;
  • State files store logs, history, recently used files and application state (window size, open files, unsaved changes, …);
  • Runtime files include filesystem objects such are sockets or named pipes that are used for communication internal to the application. Runtime files must not be accessible to anyone except current user.

§Examples

To configure paths for application myapp:

let xdg_dirs = xdg::BaseDirectories::with_prefix("myapp");

To store configuration:

let config_path = xdg_dirs
    .place_config_file("config.ini")
    .expect("cannot create configuration directory");
let mut config_file = File::create(config_path)?;
write!(&mut config_file, "configured = 1")?;

The config.ini file will appear in the proper location for desktop configuration files, most likely ~/.config/myapp/config.ini. The leading directories will be automatically created.

To retrieve supplementary data:

let logo_path = xdg_dirs
    .find_data_file("logo.png")
    .expect("application data not present");
let mut logo_file = File::open(logo_path)?;
let mut logo = Vec::new();
logo_file.read_to_end(&mut logo)?;

The logo.png will be searched in the proper locations for supplementary data files, most likely ~/.local/share/myapp/logo.png, then /usr/local/share/myapp/logo.png and /usr/share/myapp/logo.png.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§shared_prefix: PathBuf

Prefix path appended to all path lookups in system directories as described in BaseDirectories::with_prefix. May be the empty path.

§user_prefix: PathBuf

Prefix path appended to all path lookups in user directories as described in BaseDirectories::with_profile. Note that this value already contains shared_prefix as prefix, and is identical to it when constructed with BaseDirectories::with_prefix. May be the empty path.

§data_home: Option<PathBuf>

Like BaseDirectories::get_data_home, but without any prefixes applied. Is guaranteed to not be None unless no HOME could be found.

§config_home: Option<PathBuf>

Like BaseDirectories::get_config_home, but without any prefixes applied. Is guaranteed to not be None unless no HOME could be found.

§cache_home: Option<PathBuf>

Like BaseDirectories::get_cache_home, but without any prefixes applied. Is guaranteed to not be None unless no HOME could be found.

§state_home: Option<PathBuf>

Like BaseDirectories::get_state_home, but without any prefixes applied. Is guaranteed to not be None unless no HOME could be found.

§data_dirs: Vec<PathBuf>

Like BaseDirectories::get_data_dirs, but without any prefixes applied.

§config_dirs: Vec<PathBuf>

Like BaseDirectories::get_config_dirs, but without any prefixes applied.

§runtime_dir: Option<PathBuf>

Like BaseDirectories::get_runtime_directory, but without any of the sanity checks on the directory (like permissions).

Implementations§

source§

impl BaseDirectories

source

pub fn new() -> BaseDirectories

Reads the process environment, determines the XDG base directories, and returns a value that can be used for lookup. The following environment variables are examined:

  • HOME; if not set: use the same fallback as std::env::home_dir();
  • XDG_DATA_HOME; if not set: assumed to be $HOME/.local/share.
  • XDG_CONFIG_HOME; if not set: assumed to be $HOME/.config.
  • XDG_CACHE_HOME; if not set: assumed to be $HOME/.cache.
  • XDG_STATE_HOME; if not set: assumed to be $HOME/.local/state.
  • XDG_DATA_DIRS; if not set: assumed to be /usr/local/share:/usr/share.
  • XDG_CONFIG_DIRS; if not set: assumed to be /etc/xdg.
  • XDG_RUNTIME_DIR; if not accessible or permissions are not 0700: record as inaccessible (can be queried with has_runtime_directory).

As per specification, if an environment variable contains a relative path, the behavior is the same as if it was not set.

source

pub fn with_prefix<P: AsRef<Path>>(prefix: P) -> BaseDirectories

Same as new(), but prefix is implicitly prepended to every path that is looked up. This is usually the application’s name, preferably in Reverse domain name notation (The spec does not mandate this though, it’s just a convention).

source

pub fn with_profile<P1, P2>(prefix: P1, profile: P2) -> BaseDirectories
where P1: AsRef<Path>, P2: AsRef<Path>,

Same as with_prefix(), with profile also implicitly prepended to every path that is looked up, but only for user-specific directories.

This allows each user to have mutliple “profiles” with different user-specific data.

For example:

let dirs = BaseDirectories::with_profile("program-name", "profile-name");
dirs.find_data_file("bar.jpg");
dirs.find_config_file("foo.conf");

will find /usr/share/program-name/bar.jpg (without profile-name) and ~/.config/program-name/profile-name/foo.conf.

source

pub fn get_runtime_directory(&self) -> Result<&PathBuf, Error>

Returns the user-specific runtime directory (set by XDG_RUNTIME_DIR).

source

pub fn has_runtime_directory(&self) -> bool

Returns true if XDG_RUNTIME_DIR is available, false otherwise.

source

pub fn get_config_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Like place_config_file(), but does not create any directories.

source

pub fn get_data_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Like place_data_file(), but does not create any directories.

source

pub fn get_cache_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Like place_cache_file(), but does not create any directories.

source

pub fn get_state_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Like place_state_file(), but does not create any directories.

source

pub fn get_runtime_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like place_runtime_file(), but does not create any directories. If XDG_RUNTIME_DIR is not available, returns an error.

source

pub fn place_config_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Given a relative path path, returns an absolute path in XDG_CONFIG_HOME where a configuration file may be stored. Leading directories in the returned path are pre-created; if that is not possible, an error is returned.

source

pub fn place_data_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like place_config_file(), but for a data file in XDG_DATA_HOME.

source

pub fn place_cache_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like place_config_file(), but for a cache file in XDG_CACHE_HOME.

source

pub fn place_state_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like place_config_file(), but for an application state file in XDG_STATE_HOME.

source

pub fn place_runtime_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like place_config_file(), but for a runtime file in XDG_RUNTIME_DIR. If XDG_RUNTIME_DIR is not available, returns an error.

source

pub fn find_config_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Given a relative path path, returns an absolute path to an existing configuration file, or None. Searches XDG_CONFIG_HOME and then XDG_CONFIG_DIRS.

source

pub fn find_config_files<P: AsRef<Path>>(&self, path: P) -> FileFindIterator

Given a relative path path, returns an iterator yielding absolute paths to existing configuration files, in XDG_CONFIG_DIRS and XDG_CONFIG_HOME. Paths are produced in order from lowest priority to highest.

source

pub fn find_data_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Given a relative path path, returns an absolute path to an existing data file, or None. Searches XDG_DATA_HOME and then XDG_DATA_DIRS.

source

pub fn find_data_files<P: AsRef<Path>>(&self, path: P) -> FileFindIterator

Given a relative path path, returns an iterator yielding absolute paths to existing data files, in XDG_DATA_DIRS and XDG_DATA_HOME. Paths are produced in order from lowest priority to highest.

source

pub fn find_cache_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Given a relative path path, returns an absolute path to an existing cache file, or None. Searches XDG_CACHE_HOME.

source

pub fn find_state_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Given a relative path path, returns an absolute path to an existing application state file, or None. Searches XDG_STATE_HOME.

source

pub fn find_runtime_file<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf>

Given a relative path path, returns an absolute path to an existing runtime file, or None. Searches XDG_RUNTIME_DIR. If XDG_RUNTIME_DIR is not available, returns None.

source

pub fn create_config_directory<P: AsRef<Path>>( &self, path: P ) -> Result<PathBuf>

Given a relative path path, returns an absolute path to a configuration directory in XDG_CONFIG_HOME. The directory and all directories leading to it are created if they did not exist; if that is not possible, an error is returned.

source

pub fn create_data_directory<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like create_config_directory(), but for a data directory in XDG_DATA_HOME.

source

pub fn create_cache_directory<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like create_config_directory(), but for a cache directory in XDG_CACHE_HOME.

source

pub fn create_state_directory<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Like create_config_directory(), but for an application state directory in XDG_STATE_HOME.

source

pub fn create_runtime_directory<P: AsRef<Path>>( &self, path: P ) -> Result<PathBuf>

Like create_config_directory(), but for a runtime directory in XDG_RUNTIME_DIR. If XDG_RUNTIME_DIR is not available, returns an error.

source

pub fn list_config_files<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Given a relative path path, list absolute paths to all files in directories with path path in XDG_CONFIG_HOME and XDG_CONFIG_DIRS.

source

pub fn list_config_files_once<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Like list_config_files, but only the first occurence of every distinct filename is returned.

source

pub fn list_data_files<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Given a relative path path, lists absolute paths to all files in directories with path path in XDG_DATA_HOME and XDG_DATA_DIRS.

source

pub fn list_data_files_once<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Like list_data_files, but only the first occurence of every distinct filename is returned.

source

pub fn list_cache_files<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Given a relative path path, lists absolute paths to all files in directories with path path in XDG_CACHE_HOME.

source

pub fn list_state_files<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Given a relative path path, lists absolute paths to all files in directories with path path in XDG_STATE_HOME.

source

pub fn list_runtime_files<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf>

Given a relative path path, lists absolute paths to all files in directories with path path in XDG_RUNTIME_DIR. If XDG_RUNTIME_DIR is not available, returns an empty Vec.

source

pub fn get_data_home(&self) -> Option<PathBuf>

Returns the user-specific data directory (set by XDG_DATA_HOME). Is guaranteed to not return None unless no HOME could be found.

source

pub fn get_config_home(&self) -> Option<PathBuf>

Returns the user-specific configuration directory (set by XDG_CONFIG_HOME or default fallback, plus the prefix and profile if configured). Is guaranteed to not return None unless no HOME could be found.

source

pub fn get_cache_home(&self) -> Option<PathBuf>

Returns the user-specific directory for non-essential (cached) data (set by XDG_CACHE_HOME or default fallback, plus the prefix and profile if configured). Is guaranteed to not return None unless no HOME could be found.

source

pub fn get_state_home(&self) -> Option<PathBuf>

Returns the user-specific directory for application state data (set by XDG_STATE_HOME or default fallback, plus the prefix and profile if configured). Is guaranteed to not return None unless no HOME could be found.

source

pub fn get_data_dirs(&self) -> Vec<PathBuf>

Returns a preference ordered (preferred to less preferred) list of supplementary data directories, ordered by preference (set by XDG_DATA_DIRS or default fallback, plus the prefix if configured).

source

pub fn get_config_dirs(&self) -> Vec<PathBuf>

Returns a preference ordered (preferred to less preferred) list of supplementary configuration directories (set by XDG_CONFIG_DIRS or default fallback, plus the prefix if configured).

Trait Implementations§

source§

impl Clone for BaseDirectories

source§

fn clone(&self) -> BaseDirectories

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 BaseDirectories

source§

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

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

impl Default for BaseDirectories

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for BaseDirectories

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for BaseDirectories

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> 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,

§

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 T
where 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 T
where 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,