aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
blob: a09d78042ff721ef99081c48cbbff9637cc20420 (plain)
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
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)
    }
}