Crate xdir

Source
Expand description

Platform-agnistic standard directory locations.

xdir is a minimal and opinionated library for retrieving platform-agnostic XDG-compliant standard locations of directories.

§Usage

Calling a directory’s corresponding function will return its standard location if it can be detected. If the path could not be found (most likely due to misconfiguration of environment variables), the function call will fail, returning None.

§Directories

FunctionEnvironmentDefault
home$HOMEPlatform-specific
cache$XDG_CACHE_HOME$HOME/.cache
config$XDG_CONFIG_HOME$HOME/.config
bin$XDG_BIN_HOME$HOME/.local/bin
data$XDG_DATA_HOME$HOME/.local/share
state$XDG_STATE_HOME$HOME/.local/state
runtime$XDG_RUNTIME_DIRNone

§Examples

To get the configuration file of an application:

fn config() -> PathBuf {
    xdir::config()
        // Append the application name to the path to avoid cluttering the
        // general config directory.
        .map(|path| path.join("myapp"))
        // If the standard path could not be found (e.g.`$HOME` is not set),
        // default to the current directory.
        .unwrap_or_default()
        // Finally, append the config file to the directory path.
        .join("config.toml")
}

Functions§

bin
Returns the path to the user’s executable directory.
cache
Returns the path to the user’s cache directory.
config
Returns the path to the user’s config directory.
data
Returns the path to the user’s data directory.
home
Returns the path of the current user’s home directory using environment variables or OS-specific APIs.
runtime
Returns the path to the user’s runtime directory.
state
Returns the path to the user’s state directory.