]>
Commit | Line | Data |
---|---|---|
1 | use std::env; | |
2 | use std::path::PathBuf; | |
3 | ||
4 | pub struct Configuration { | |
5 | // Default Base Directories, default to XDG dirs but can be | |
6 | pub output_directory: PathBuf, | |
7 | } | |
8 | ||
9 | impl Configuration { | |
10 | ||
11 | pub fn new() -> Self { | |
12 | let output_directory = Configuration::directory( | |
13 | "PAGE_OUTPUT_DIRECTORY", | |
14 | "XDG_CACHE_HOME", | |
15 | ".cache", | |
16 | "page" | |
17 | ); | |
18 | ||
19 | Configuration { | |
20 | output_directory, | |
21 | } | |
22 | } | |
23 | ||
24 | fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf { | |
25 | match env::var(user_override) { | |
26 | Ok(directory) => PathBuf::from(directory), | |
27 | Err(_) => match env::var(default_value) { | |
28 | Ok(directory) => PathBuf::from(directory), | |
29 | Err(_) => match env::var("HOME") { | |
30 | Ok(directory) => PathBuf::from(directory).join(home_fallback), | |
31 | Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", user_override, default_value), | |
32 | }, | |
33 | }, | |
34 | }.join(path) | |
35 | } | |
36 | } | |
37 |