aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent2ea9dc0ea11c3fa6aeb8768537c3b48c67881d7a (diff)
Use XDG_CACHE_HOME for output.
Diffstat (limited to 'src')
-rw-r--r--src/configuration.rs74
-rw-r--r--src/main.rs25
2 files changed, 70 insertions, 29 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);
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 9dd296a..d0ed57c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+mod configuration;
mod file_finder;
mod file_handler;
@@ -5,10 +6,12 @@ use std::env::current_dir;
use std::fs::{create_dir_all, remove_dir_all};
use std::io::{Error, ErrorKind, Result};
-use crate::file_finder::find_files;
-use crate::file_handler::FileHandler;
+use configuration::Configuration;
+use file_finder::find_files;
+use file_handler::FileHandler;
fn main() -> Result<()> {
+ let configuration = Configuration::new()?;
let source = current_dir()?;
let source_name = source
.file_name()
@@ -19,16 +22,14 @@ fn main() -> Result<()> {
)
})?
.to_string_lossy();
- let parent = source.parent().ok_or_else(|| {
- Error::new(
- ErrorKind::InvalidData,
- "Current directory parent was not readable.",
- )
- })?;
- let gemini_destination_name = format!("{source_name}_gemini");
- let gemini_destination = parent.join(gemini_destination_name);
- let html_destination_name = format!("{source_name}_html");
- let html_destination = parent.join(html_destination_name);
+ let gemini_destination = configuration
+ .output_directory
+ .join(format!("{source_name}"))
+ .join("gemini");
+ let html_destination = configuration
+ .output_directory
+ .join(format!("{source_name}"))
+ .join("html");
// Step 1. Identify the files
let files = find_files(&source);