use std::fmt;
use std::path::{Path, PathBuf};
const PREFIX: &str = "acton";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigLocationError {
#[cfg(any(unix, test))]
BaseDirectories(String),
#[cfg(any(not(unix), test))]
AppDataUnset,
}
impl fmt::Display for ConfigLocationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(any(unix, test))]
Self::BaseDirectories(cause) => write!(
f,
"could not determine the XDG configuration directories: {cause}; \
set HOME or XDG_CONFIG_HOME to a readable directory"
),
#[cfg(any(not(unix), test))]
Self::AppDataUnset => f.write_str(
"the APPDATA environment variable is unset or empty, so there is no \
configuration directory to search; set APPDATA to the roaming \
application data directory",
),
}
}
}
impl std::error::Error for ConfigLocationError {}
#[cfg(any(not(unix), test))]
fn app_data_config_path(app_data_root: &Path, relative: &Path) -> PathBuf {
app_data_root.join(PREFIX).join(relative)
}
#[cfg(any(not(unix), test))]
fn find_in_app_data(app_data_root: &Path, relative: &Path) -> Option<PathBuf> {
let candidate = app_data_config_path(app_data_root, relative);
candidate.is_file().then_some(candidate)
}
#[cfg(not(unix))]
fn app_data_root() -> Result<PathBuf, ConfigLocationError> {
std::env::var_os("APPDATA")
.filter(|value| !value.is_empty())
.map(PathBuf::from)
.ok_or(ConfigLocationError::AppDataUnset)
}
#[cfg(unix)]
pub fn find_config_file(relative: &Path) -> Result<Option<PathBuf>, ConfigLocationError> {
let dirs = xdg::BaseDirectories::with_prefix(PREFIX)
.map_err(|e| ConfigLocationError::BaseDirectories(e.to_string()))?;
Ok(dirs.find_config_file(relative))
}
#[cfg(not(unix))]
pub fn find_config_file(relative: &Path) -> Result<Option<PathBuf>, ConfigLocationError> {
let root = app_data_root()?;
Ok(find_in_app_data(&root, relative))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn app_data_path_inserts_the_acton_prefix() {
let path = app_data_config_path(Path::new("/roaming"), Path::new("config.toml"));
assert_eq!(path, Path::new("/roaming/acton/config.toml"));
}
#[test]
fn app_data_path_preserves_a_nested_relative_path() {
let path = app_data_config_path(Path::new("/roaming"), &Path::new("myapp").join("ipc.toml"));
assert_eq!(path, Path::new("/roaming/acton/myapp/ipc.toml"));
}
#[test]
fn app_data_lookup_finds_an_existing_file() {
let root = TempDir::new().expect("temp dir");
let dir = root.path().join("acton");
fs::create_dir_all(&dir).expect("create acton dir");
fs::write(dir.join("config.toml"), "").expect("write config");
let found = find_in_app_data(root.path(), Path::new("config.toml"));
assert_eq!(found, Some(dir.join("config.toml")));
}
#[test]
fn app_data_lookup_reports_a_missing_file_as_none() {
let root = TempDir::new().expect("temp dir");
let found = find_in_app_data(root.path(), Path::new("config.toml"));
assert_eq!(found, None);
}
#[test]
fn app_data_lookup_rejects_a_directory_at_the_config_path() {
let root = TempDir::new().expect("temp dir");
fs::create_dir_all(root.path().join("acton").join("config.toml")).expect("create dir");
let found = find_in_app_data(root.path(), Path::new("config.toml"));
assert_eq!(
found, None,
"a directory named config.toml is not a configuration file"
);
}
#[cfg(unix)]
#[test]
fn unix_lookup_finds_a_file_under_xdg_config_home() {
let root = TempDir::new().expect("temp dir");
let dir = root.path().join("acton");
fs::create_dir_all(&dir).expect("create acton dir");
fs::write(dir.join("config.toml"), "").expect("write config");
let previous = std::env::var_os("XDG_CONFIG_HOME");
std::env::set_var("XDG_CONFIG_HOME", root.path());
let found = find_config_file(Path::new("config.toml"));
match previous {
Some(value) => std::env::set_var("XDG_CONFIG_HOME", value),
None => std::env::remove_var("XDG_CONFIG_HOME"),
}
assert_eq!(found, Ok(Some(dir.join("config.toml"))));
}
#[test]
fn app_data_unset_names_the_variable_to_set() {
let rendered = ConfigLocationError::AppDataUnset.to_string();
assert!(
rendered.contains("APPDATA"),
"message should name the variable, got: {rendered}"
);
}
#[test]
fn base_directories_error_carries_the_underlying_cause() {
let rendered =
ConfigLocationError::BaseDirectories("HOME is not set".to_string()).to_string();
assert!(
rendered.contains("HOME is not set"),
"message should carry the cause, got: {rendered}"
);
}
}