Xdg

Struct Xdg 

Source
pub struct Xdg { /* private fields */ }
Expand description

This strategy implements the XDG Base Directories Specification. It is the most common on Linux, but is increasingly being adopted elsewhere.

This initial example removes all the XDG environment variables to show the strategy’s use of the XDG default directories.

use etcetera::base_strategy::BaseStrategy;
use etcetera::base_strategy::Xdg;
use std::path::Path;

// Remove the environment variables that the strategy reads from.
unsafe {
std::env::remove_var("XDG_CONFIG_HOME");
std::env::remove_var("XDG_DATA_HOME");
std::env::remove_var("XDG_CACHE_HOME");
std::env::remove_var("XDG_STATE_HOME");
std::env::remove_var("XDG_RUNTIME_DIR");
}

let base_strategy = Xdg::new().unwrap();

let home_dir = etcetera::home_dir().unwrap();

assert_eq!(
    base_strategy.home_dir(),
    &home_dir
);
assert_eq!(
    base_strategy.config_dir().strip_prefix(&home_dir),
    Ok(Path::new(".config/"))
);
assert_eq!(
    base_strategy.data_dir().strip_prefix(&home_dir),
    Ok(Path::new(".local/share/"))
);
assert_eq!(
    base_strategy.cache_dir().strip_prefix(&home_dir),
    Ok(Path::new(".cache/"))
);
assert_eq!(
    base_strategy.state_dir().unwrap().strip_prefix(&home_dir),
    Ok(Path::new(".local/state"))
);
assert_eq!(
    base_strategy.runtime_dir(),
    None
);

This next example gives the environment variables values:

use etcetera::base_strategy::BaseStrategy;
use etcetera::base_strategy::Xdg;
use std::path::Path;

// We need to conditionally set these to ensure that they are absolute paths both on Windows and other systems.
let config_path = if cfg!(windows) {
    "C:\\foo\\"
} else {
    "/foo/"
};
let data_path = if cfg!(windows) {
    "C:\\bar\\"
} else {
    "/bar/"
};
let cache_path = if cfg!(windows) {
    "C:\\baz\\"
} else {
    "/baz/"
};
let state_path = if cfg!(windows) {
    "C:\\foobar\\"
} else {
    "/foobar/"
};
let runtime_path = if cfg!(windows) {
    "C:\\qux\\"
} else {
    "/qux/"
};

unsafe {
std::env::set_var("XDG_CONFIG_HOME", config_path);
std::env::set_var("XDG_DATA_HOME", data_path);
std::env::set_var("XDG_CACHE_HOME", cache_path);
std::env::set_var("XDG_STATE_HOME", state_path);
std::env::set_var("XDG_RUNTIME_DIR", runtime_path);
}

let base_strategy = Xdg::new().unwrap();

assert_eq!(
    base_strategy.config_dir(),
    Path::new(config_path)
);
assert_eq!(
    base_strategy.data_dir(),
    Path::new(data_path)
);
assert_eq!(
    base_strategy.cache_dir(),
    Path::new(cache_path)
);
assert_eq!(
    base_strategy.state_dir().unwrap(),
    Path::new(state_path)
);
assert_eq!(
    base_strategy.runtime_dir().unwrap(),
    Path::new(runtime_path)
);

The XDG spec requires that when the environment variables’ values are not absolute paths, their values should be ignored. This example exemplifies this behaviour:

use etcetera::base_strategy::BaseStrategy;
use etcetera::base_strategy::Xdg;
use std::path::Path;

// Remove the environment variables that the strategy reads from.
unsafe {
std::env::set_var("XDG_CONFIG_HOME", "foo/");
std::env::set_var("XDG_DATA_HOME", "bar/");
std::env::set_var("XDG_CACHE_HOME", "baz/");
std::env::set_var("XDG_STATE_HOME", "foobar/");
std::env::set_var("XDG_RUNTIME_DIR", "qux/");
}

let base_strategy = Xdg::new().unwrap();

let home_dir = etcetera::home_dir().unwrap();

// We still get the default values.
assert_eq!(
    base_strategy.config_dir().strip_prefix(&home_dir),
    Ok(Path::new(".config/"))
);
assert_eq!(
    base_strategy.data_dir().strip_prefix(&home_dir),
    Ok(Path::new(".local/share/"))
);
assert_eq!(
    base_strategy.cache_dir().strip_prefix(&home_dir),
    Ok(Path::new(".cache/"))
);
assert_eq!(
    base_strategy.state_dir().unwrap().strip_prefix(&home_dir),
    Ok(Path::new(".local/state/"))
);
assert_eq!(
    base_strategy.runtime_dir(),
    None
);

Implementations§

Source§

impl Xdg

Source

pub fn new() -> Result<Self, HomeDirError>

Create a new Xdg BaseStrategy

Trait Implementations§

Source§

impl BaseStrategy for Xdg

Source§

fn home_dir(&self) -> &Path

Gets the home directory of the current user.
Source§

fn config_dir(&self) -> PathBuf

Gets the user’s configuration directory.
Source§

fn data_dir(&self) -> PathBuf

Gets the user’s data directory.
Source§

fn cache_dir(&self) -> PathBuf

Gets the user’s cache directory.
Source§

fn state_dir(&self) -> Option<PathBuf>

Gets the user’s state directory. Currently, only the Xdg strategy supports this.
Source§

fn runtime_dir(&self) -> Option<PathBuf>

Gets the user’s runtime directory. Currently, only the Xdg strategy supports this. Read more
Source§

impl Clone for Xdg

Source§

fn clone(&self) -> Xdg

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 Xdg

Source§

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

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

impl Hash for Xdg

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Xdg

Source§

fn cmp(&self, other: &Xdg) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Xdg

Source§

fn eq(&self, other: &Xdg) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Xdg

Source§

fn partial_cmp(&self, other: &Xdg) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Xdg

Source§

impl StructuralPartialEq for Xdg

Auto Trait Implementations§

§

impl Freeze for Xdg

§

impl RefUnwindSafe for Xdg

§

impl Send for Xdg

§

impl Sync for Xdg

§

impl Unpin for Xdg

§

impl UnwindSafe for Xdg

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