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};
const USER_CONFIG_SIMPLE_FILENAME: &str = "config.toml";
const DEBUG_USER_CONFIG_FILENAME: &str = "config.user.toml";
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UserConfig<'a> {
#[serde(borrow)]
pub defaults: Option<HashMap<&'a str, &'a str>>,
#[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() {
let home_dir_config_path = BaseDirs::new()
.map(|bd| bd.home_dir().join(".config").join(CONFIG_FILENAME_JEN))
.filter(|p| p.exists());
return if home_dir_config_path.is_none() {
Some(config_path)
} else {
home_dir_config_path
};
}
return Some(config_path);
}
None
}