aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-10-01 22:18:28 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-10-01 22:18:28 +0200
commite26d1da40e42ccb8e62c57a41fee2607d75812a3 (patch)
tree10fbe10df735215aa1f4e655c979bc6f30696274 /src/configuration.rs
parent2ea9dc0ea11c3fa6aeb8768537c3b48c67881d7a (diff)
Use XDG_CACHE_HOME for output.
Diffstat (limited to 'src/configuration.rs')
-rw-r--r--src/configuration.rs74
1 files changed, 57 insertions, 17 deletions
diff --git a/src/configuration.rs b/src/configuration.rs
index a09d780..333a27f 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 {
@@ -7,31 +8,70 @@ pub struct Configuration {
}
impl Configuration {
+ pub fn new() -> Result<Self> {
+ let output_directory =
+ Configuration::directory("PAGE_OUTPUT_DIRECTORY", "XDG_CACHE_HOME", ".cache", "page")?;
- pub fn new() -> Self {
- let output_directory = Configuration::directory(
- "PAGE_OUTPUT_DIRECTORY",
- "XDG_CACHE_HOME",
- ".cache",
- "page"
- );
-
- Configuration {
- output_directory,
- }
+ Ok(Configuration { output_directory })
}
- fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf {
+ fn directory(
+ user_override: &str,
+ default_value: &str,
+ home_fallback: &str,
+ path: &str,
+ ) -> 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),
+ Ok(directory) => Ok(PathBuf::from(directory).join(path)),
Err(_) => match env::var("HOME") {
- Ok(directory) => PathBuf::from(directory).join(home_fallback),
- Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", user_override, default_value),
+ 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)
+ }
}
}
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::env;
+
+ use test_utilities::*;
+
+ #[test]
+ fn test_sets_correct_configuration_directories() {
+ let default_configuration = Configuration::new().unwrap();
+
+ let test_dir = setup_test_dir();
+
+ // SAFETY: Run only in single-threaded mode
+ unsafe {
+ env::set_var("PAGE_OUTPUT_DIRECTORY", test_dir.join("output"));
+ }
+ let override_configuration = Configuration::new().unwrap();
+
+ // Ensure our overrides were applied
+
+ assert_eq!(
+ override_configuration.output_directory,
+ test_dir.join("output/page")
+ );
+
+ // Ensure all the defaults are different from defaults
+
+ assert_ne!(
+ default_configuration.output_directory,
+ override_configuration.output_directory
+ );
+
+ cleanup_test_dir(&test_dir);
+ }
+}