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
use crate::config::CONFIG_FILENAME_JEN;
use directories_next::{BaseDirs, ProjectDirs};
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf};

/// The config filename when running in release mode.
const USER_CONFIG_SIMPLE_FILENAME: &str = "config.toml";

/// The config filename when running in debug mode.
//#[cfg(debug_assertions)]
const DEBUG_USER_CONFIG_FILENAME: &str = "config.user.toml";

/// The user configuration file.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UserConfig<'a> {
    /// A list of default values that will always be used to populate templates.
    #[serde(borrow)]
    pub defaults: Option<HashMap<&'a str, &'a str>>,
    /// Custom profiles that can be used as a shortcut.
    #[serde(borrow)]
    pub profiles: Option<HashMap<&'a str, &'a str>>,
}

pub fn user_config_file_path() -> Option<PathBuf> {
    if cfg!(debug_assertions) {
        return Some(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(DEBUG_USER_CONFIG_FILENAME));
    }

    if let Some(project_dirs) = ProjectDirs::from("net", "zerotask", "jen") {
        let config_path = project_dirs.config_dir().join(USER_CONFIG_SIMPLE_FILENAME);
        if !config_path.exists() {
            // Convenience lookup inside the home directory.
            let home_dir_config_path = BaseDirs::new()
                .map(|bd| bd.home_dir().join(".config").join(CONFIG_FILENAME_JEN))
                .filter(|p| p.exists());

            // However, if the home directory config file is not available, we still return
            // the platform-specific file location.
            return if home_dir_config_path.is_none() {
                Some(config_path)
            } else {
                home_dir_config_path
            };
        }

        return Some(config_path);
    }

    None
}