aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
commitd0f582b98712d967b2f95d0405886d063bd89468 (patch)
treecc644c21278d336772557366bcdd3e46b22065db /src/configuration.rs
parent8b3b94a38b443c50afc5b42cca45db7c18ce280d (diff)
Get stricter clippy
Diffstat (limited to 'src/configuration.rs')
-rw-r--r--src/configuration.rs80
1 files changed, 60 insertions, 20 deletions
diff --git a/src/configuration.rs b/src/configuration.rs
index afee277..b598764 100644
--- a/src/configuration.rs
+++ b/src/configuration.rs
@@ -1,4 +1,5 @@
use std::env;
+use std::io::{Error, ErrorKind, Result};
use std::path::PathBuf;
pub struct Configuration {
@@ -25,21 +26,21 @@ pub struct Configuration {
}
impl Configuration {
- pub fn new() -> Self {
+ pub fn new() -> Result<Self> {
let config_directory = Configuration::directory(
"BLOG_CONFIG_DIRECTORY",
"XDG_CONFIG_HOME",
".config",
"blog",
- );
+ )?;
let data_directory = Configuration::directory(
"BLOG_DATA_DIRECTORY",
"XDG_DATA_HOME",
".local/share",
"blog",
- );
+ )?;
let output_directory =
- Configuration::directory("BLOG_OUTPUT_DIRECTORY", "XDG_CACHE_HOME", ".cache", "blog");
+ Configuration::directory("BLOG_OUTPUT_DIRECTORY", "XDG_CACHE_HOME", ".cache", "blog")?;
let max_posts: u8 = env::var("BLOG_MAX_POSTS")
.ok()
@@ -56,7 +57,7 @@ impl Configuration {
let blog_output_directory = output_directory.join("blog");
let archive_output_directory = output_directory.join("archive");
- Configuration {
+ Ok(Configuration {
config_directory,
data_directory,
output_directory,
@@ -68,7 +69,7 @@ impl Configuration {
templates_directory,
blog_output_directory,
archive_output_directory,
- }
+ })
}
fn directory(
@@ -76,19 +77,22 @@ impl Configuration {
default_value: &str,
home_fallback: &str,
path: &str,
- ) -> PathBuf {
+ ) -> Result<PathBuf> {
match env::var(user_override) {
- Ok(directory) => PathBuf::from(directory),
+ Ok(directory) => Ok(PathBuf::from(directory).join(path)),
Err(_) => match env::var(default_value) {
- Ok(directory) => PathBuf::from(directory),
- Err(_) => env::var("HOME")
- .map_or_else(
- |_| panic!("Could not find required directory, {user_override} or {default_value} should be set and readable"),
- |directory| PathBuf::from(directory).join(home_fallback)
- )
+ Ok(directory) => Ok(PathBuf::from(directory).join(path)),
+ Err(_) => match env::var("HOME") {
+ Ok(directory) => Ok(PathBuf::from(directory).join(home_fallback).join(path)),
+ Err(_) => Err(Error::new(
+ ErrorKind::NotFound,
+ format!(
+ "Could not find required directory, {user_override} or {default_value} should be set and readable"
+ ),
+ )),
+ },
},
}
- .join(path)
}
}
@@ -102,6 +106,8 @@ mod tests {
#[test]
fn test_reads_configuration_from_configuration_variable_if_set() {
let test_dir = setup_test_dir();
+
+ // SAFETY: Run only in single-threaded mode
unsafe {
env::set_var("TEST_BLOG_CONFIG_DIRECTORY", &test_dir);
}
@@ -111,15 +117,19 @@ mod tests {
"XDG_CONFIG_HOME",
".config",
"beepboop",
- );
+ )
+ .expect("Could not get config directory");
assert_eq!(config_directory, test_dir.join("beepboop"));
+
cleanup_test_dir(&test_dir);
}
#[test]
fn test_reads_configuration_from_default_location_if_configuration_variable_not_set() {
let test_dir = setup_test_dir();
+
+ // SAFETY: Run only in single-threaded mode
unsafe {
env::set_var("TEST_XDG_CONFIG_HOME", &test_dir);
}
@@ -129,9 +139,11 @@ mod tests {
"TEST_XDG_CONFIG_HOME",
".config",
"beepboop",
- );
+ )
+ .expect("Could not get config directory");
assert_eq!(config_directory, test_dir.join("beepboop"));
+
cleanup_test_dir(&test_dir);
}
@@ -144,23 +156,51 @@ mod tests {
"UNDEFINED_FAKE_XDG_CONFIG_HOME",
".config",
"beepboop",
- );
+ )
+ .expect("Could not get config directory");
assert_eq!(config_directory, path.join(".config/beepboop"));
}
#[test]
+ fn test_fails_configuration_if_no_variable_is_set() {
+ let original_home = env::var("HOME").unwrap();
+
+ // SAFETY: Run only in single-threaded mode
+ unsafe {
+ env::remove_var("HOME");
+ }
+
+ assert!(
+ Configuration::directory(
+ "UNDEFINED_BLOG_CONFIG_DIRECTORY",
+ "UNDEFINED_FAKE_XDG_CONFIG_HOME",
+ ".config",
+ "beepboop",
+ )
+ .is_err()
+ );
+
+ // SAFETY: Run only in single-threaded mode
+ unsafe {
+ env::set_var("HOME", original_home);
+ }
+ }
+
+ #[test]
fn test_sets_correct_configuration_directories() {
- let default_configuration = Configuration::new();
+ let default_configuration = Configuration::new().unwrap();
let test_dir = setup_test_dir();
+
+ // SAFETY: Run only in single-threaded mode
unsafe {
env::set_var("BLOG_CONFIG_DIRECTORY", test_dir.join("config"));
env::set_var("BLOG_DATA_DIRECTORY", test_dir.join("data"));
env::set_var("BLOG_OUTPUT_DIRECTORY", test_dir.join("output"));
env::set_var("BLOG_MAX_POSTS", "99");
}
- let override_configuration = Configuration::new();
+ let override_configuration = Configuration::new().unwrap();
// Ensure our overrides were applied