Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xdg_*_directory functions #2439

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ derivative = "2.0.0"
dirs = "5.0.1"
dotenvy = "0.15"
edit-distance = "2.0.0"
etcetera = "0.8.0"
heck = "0.5.0"
lexiclean = "0.0.1"
libc = "0.2.0"
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,13 +1816,10 @@ for details.
`requirement`, e.g., `">=0.1.0"`, returning `"true"` if so and `"false"`
otherwise.

##### XDG Directories<sup>1.23.0</sup>
##### Directories<sup>1.23.0</sup>

These functions return paths to user-specific directories for things like
configuration, data, caches, executables, and the user's home directory. These
functions follow the
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html),
and are implemented with the
These functions return paths to standard operating system directories for things like
configuration, data, caches, executables, and the user's home directory. These are implemented with the
[`dirs`](https://docs.rs/dirs/latest/dirs/index.html) crate.

- `cache_directory()` - The user-specific cache directory.
Expand All @@ -1833,6 +1830,23 @@ and are implemented with the
- `executable_directory()` - The user-specific executable directory.
- `home_directory()` - The user's home directory.

##### XDG Directories

These functions return paths to XDG directories for things like
configuration, data, caches, executables, and the user's home directory. These
functions follow the
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) and fall back to the operating system standard directories,
and are implemented with the
[`etcetera`](https://docs.rs/etcetera/latest/etcetera/index.html) crate.

- `xdg_cache_directory()` - The XDG cache directory.
- `xdg_config_directory()` - The XDG configuration directory.
- `xdg_data_directory()` - The XDG data directory.
- `xdg_home_directory()` - The user's home directory.
- `xdg_runtime_directory()` - The local XDG runtime directory.
- `xdg_state_directory()` - The XDG state directory.


### Constants

A number of constants are predefined:
Expand Down
47 changes: 34 additions & 13 deletions src/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::*,
etcetera::{choose_base_strategy, BaseStrategy},
heck::{
ToKebabCase, ToLowerCamelCase, ToShoutyKebabCase, ToShoutySnakeCase, ToSnakeCase, ToTitleCase,
ToUpperCamelCase,
Expand Down Expand Up @@ -46,27 +47,27 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"arch" => Nullary(arch),
"blake3" => Unary(blake3),
"blake3_file" => Unary(blake3_file),
"cache_directory" => Nullary(|_| dir("cache", dirs::cache_dir)),
"cache_directory" => Nullary(|_| dir("cache", dirs::cache_dir())),
"canonicalize" => Unary(canonicalize),
"capitalize" => Unary(capitalize),
"choose" => Binary(choose),
"clean" => Unary(clean),
"config_directory" => Nullary(|_| dir("config", dirs::config_dir)),
"config_local_directory" => Nullary(|_| dir("local config", dirs::config_local_dir)),
"data_directory" => Nullary(|_| dir("data", dirs::data_dir)),
"data_local_directory" => Nullary(|_| dir("local data", dirs::data_local_dir)),
"config_directory" => Nullary(|_| dir("config", dirs::config_dir())),
"config_local_directory" => Nullary(|_| dir("local config", dirs::config_local_dir())),
"data_directory" => Nullary(|_| dir("data", dirs::data_dir())),
"data_local_directory" => Nullary(|_| dir("local data", dirs::data_local_dir())),
"datetime" => Unary(datetime),
"datetime_utc" => Unary(datetime_utc),
"encode_uri_component" => Unary(encode_uri_component),
"env" => UnaryOpt(env),
"env_var" => Unary(env_var),
"env_var_or_default" => Binary(env_var_or_default),
"error" => Unary(error),
"executable_directory" => Nullary(|_| dir("executable", dirs::executable_dir)),
"executable_directory" => Nullary(|_| dir("executable", dirs::executable_dir())),
"extension" => Unary(extension),
"file_name" => Unary(file_name),
"file_stem" => Unary(file_stem),
"home_directory" => Nullary(|_| dir("home", dirs::home_dir)),
"home_directory" => Nullary(|_| dir("home", dirs::home_dir())),
"invocation_directory" => Nullary(invocation_directory),
"invocation_directory_native" => Nullary(invocation_directory_native),
"is_dependency" => Nullary(is_dependency),
Expand Down Expand Up @@ -110,6 +111,25 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"uppercase" => Unary(uppercase),
"uuid" => Nullary(uuid),
"without_extension" => Unary(without_extension),
"xdg_cache_directory" => {
Nullary(|_| dir("cache", Some(choose_base_strategy().unwrap().cache_dir())))
}
"xdg_config_directory" => {
Nullary(|_| dir("config", Some(choose_base_strategy().unwrap().config_dir())))
}
"xdg_data_directory" => {
Nullary(|_| dir("data", Some(choose_base_strategy().unwrap().data_dir())))
}
"xdg_home_directory" => Nullary(|_| {
dir(
"home",
Some(choose_base_strategy().unwrap().home_dir().to_path_buf()),
)
}),
"xdg_runtime_directory" => {
Nullary(|_| dir("runtime", choose_base_strategy().unwrap().runtime_dir()))
}
"xdg_state_directory" => Nullary(|_| dir("state", choose_base_strategy().unwrap().state_dir())),
_ => return None,
};
Some(function)
Expand Down Expand Up @@ -223,8 +243,8 @@ fn clean(_context: Context, path: &str) -> FunctionResult {
Ok(Path::new(path).lexiclean().to_str().unwrap().to_owned())
}

fn dir(name: &'static str, f: fn() -> Option<PathBuf>) -> FunctionResult {
match f() {
fn dir(name: &'static str, path: Option<PathBuf>) -> FunctionResult {
match path {
Some(path) => path
.as_os_str()
.to_str()
Expand Down Expand Up @@ -701,17 +721,18 @@ mod tests {

#[test]
fn dir_not_found() {
assert_eq!(dir("foo", || None).unwrap_err(), "foo directory not found");
assert_eq!(dir("foo", None).unwrap_err(), "foo directory not found");
}

#[cfg(unix)]
#[test]
fn dir_not_unicode() {
use std::os::unix::ffi::OsStrExt;
assert_eq!(
dir("foo", || Some(
std::ffi::OsStr::from_bytes(b"\xe0\x80\x80").into()
))
dir(
"foo",
Some(std::ffi::OsStr::from_bytes(b"\xe0\x80\x80").into())
)
.unwrap_err(),
"unable to convert foo directory path to string: ���",
);
Expand Down
109 changes: 109 additions & 0 deletions tests/directories.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use etcetera::BaseStrategy;

#[test]
fn cache_directory() {
Expand Down Expand Up @@ -83,3 +84,111 @@ fn home_directory() {
.stdout(dirs::home_dir().unwrap_or_default().to_string_lossy())
.run();
}

#[test]
fn xdg_cache_directory() {
Test::new()
.justfile("x := xdg_cache_directory()")
.args(["--evaluate", "x"])
.stdout(
etcetera::choose_base_strategy()
.unwrap()
.cache_dir()
.to_string_lossy(),
)
.run();
}

#[test]
fn xdg_config_directory() {
Test::new()
.justfile("x := xdg_config_directory()")
.args(["--evaluate", "x"])
.stdout(
etcetera::choose_base_strategy()
.unwrap()
.config_dir()
.to_string_lossy(),
)
.run();
}

#[test]
fn xdg_data_directory() {
Test::new()
.justfile("x := xdg_data_directory()")
.args(["--evaluate", "x"])
.stdout(
etcetera::choose_base_strategy()
.unwrap()
.data_dir()
.to_string_lossy(),
)
.run();
}

#[test]
fn xdg_home_directory() {
Test::new()
.justfile("x := xdg_home_directory()")
.args(["--evaluate", "x"])
.stdout(
etcetera::choose_base_strategy()
.unwrap()
.home_dir()
.to_string_lossy(),
)
.run();
}

#[test]
fn xdg_runtime_directory() {
if let Some(runtime_dir) = etcetera::choose_base_strategy().unwrap().runtime_dir() {
Test::new()
.justfile("x := xdg_runtime_directory()")
.args(["--evaluate", "x"])
.stdout(runtime_dir.to_string_lossy())
.run();
} else {
Test::new()
.justfile("x := xdg_runtime_directory()")
.args(["--evaluate", "x"])
.stderr(
"
error: Call to function `xdg_runtime_directory` failed: runtime directory not found
——▶ justfile:1:6
1 │ x := xdg_runtime_directory()
│ ^^^^^^^^^^^^^^^^^^^^^
",
)
.status(EXIT_FAILURE)
.run();
}
}

#[test]
fn xdg_state_directory() {
if let Some(state_dir) = etcetera::choose_base_strategy().unwrap().state_dir() {
Test::new()
.justfile("x := xdg_state_directory()")
.args(["--evaluate", "x"])
.stdout(state_dir.to_string_lossy())
.run();
} else {
Test::new()
.justfile("x := xdg_state_directory()")
.args(["--evaluate", "x"])
.stderr(
"
error: Call to function `xdg_state_directory` failed: state directory not found
——▶ justfile:1:6
1 │ x := xdg_state_directory()
│ ^^^^^^^^^^^^^^^^^^^
",
)
.status(EXIT_FAILURE)
.run();
}
}