aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2024-07-17 19:00:41 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2024-07-17 19:00:41 +0200
commit45fbf824248215a737d5d0b52920b43b68941ad3 (patch)
treeb4df9a6e0e92724b745f55dff2a459bcd98eae51 /src/configuration.rs
parentd843a0b2c2590b96781a6c12af6c856b8056bf64 (diff)
Use sections instead of divs1.3.2
Diffstat (limited to 'src/configuration.rs')
-rw-r--r--src/configuration.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/configuration.rs b/src/configuration.rs
new file mode 100644
index 0000000..a09d780
--- /dev/null
+++ b/src/configuration.rs
@@ -0,0 +1,37 @@
+use std::env;
+use std::path::PathBuf;
+
+pub struct Configuration {
+ // Default Base Directories, default to XDG dirs but can be
+ pub output_directory: PathBuf,
+}
+
+impl Configuration {
+
+ pub fn new() -> Self {
+ let output_directory = Configuration::directory(
+ "PAGE_OUTPUT_DIRECTORY",
+ "XDG_CACHE_HOME",
+ ".cache",
+ "page"
+ );
+
+ Configuration {
+ output_directory,
+ }
+ }
+
+ fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf {
+ match env::var(user_override) {
+ Ok(directory) => PathBuf::from(directory),
+ Err(_) => match env::var(default_value) {
+ Ok(directory) => PathBuf::from(directory),
+ 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),
+ },
+ },
+ }.join(path)
+ }
+}
+