1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
use std::env;
use std::io::{Error, ErrorKind, Result};
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() -> Result<Self> {
let output_directory =
Configuration::directory("PAGE_OUTPUT_DIRECTORY", "XDG_CACHE_HOME", ".cache", "page")?;
Ok(Configuration { output_directory })
}
fn directory(
user_override: &str,
default_value: &str,
home_fallback: &str,
path: &str,
) -> Result<PathBuf> {
match env::var(user_override) {
Ok(directory) => Ok(PathBuf::from(directory).join(path)),
Err(_) => match env::var(default_value) {
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"
),
)),
},
},
}
}
}
#[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);
}
}
|