2 use std::path::PathBuf;
4 pub struct Configuration {
5 // Default Base Directories, default to XDG dirs but can be
6 // configured by users.
7 pub config_directory: PathBuf,
8 pub data_directory: PathBuf,
9 pub output_directory: PathBuf,
13 // Derived directories: Config
14 pub remote_config: PathBuf,
16 // Derived directories: Data
17 pub posts_directory: PathBuf,
18 pub archive_directory: PathBuf,
19 pub static_directory: PathBuf,
20 pub templates_directory: PathBuf,
22 // Derived directories: Output
23 pub blog_output_directory: PathBuf,
24 pub archive_output_directory: PathBuf
29 pub fn new() -> Self {
30 let config_directory = Configuration::directory(
31 "BLOG_CONFIG_DIRECTORY",
36 let data_directory = Configuration::directory(
37 "BLOG_DATA_DIRECTORY",
42 let output_directory = Configuration::directory(
43 "BLOG_OUTPUT_DIRECTORY",
49 let max_posts: u8 = env::var("BLOG_MAX_POSTS")
51 .and_then(|v| v.parse().ok())
54 let remote_config = config_directory.join("blogremote");
56 let posts_directory = data_directory.join("posts");
57 let archive_directory = data_directory.join("archive");
58 let static_directory = data_directory.join("static");
59 let templates_directory = data_directory.join("templates");
61 let blog_output_directory = output_directory.join("blog");
62 let archive_output_directory = output_directory.join("archive");
74 blog_output_directory,
75 archive_output_directory
79 fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf {
80 match env::var(user_override) {
81 Ok(directory) => PathBuf::from(directory),
82 Err(_) => match env::var(default_value) {
83 Ok(directory) => PathBuf::from(directory),
84 Err(_) => match env::var("HOME") {
85 Ok(directory) => PathBuf::from(directory).join(home_fallback),
86 Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", user_override, default_value),