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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use anyhow::{format_err, Error};
use std::{env::var, ops::Deref, path::Path, sync::Arc};
use crate::{latitude::Latitude, longitude::Longitude};
#[derive(Default, Debug)]
pub struct ConfigInner {
pub api_key: Option<String>,
pub api_endpoint: Option<String>,
pub api_path: Option<String>,
pub zipcode: Option<u64>,
pub country_code: Option<String>,
pub city_name: Option<String>,
pub lat: Option<Latitude>,
pub lon: Option<Longitude>,
}
#[derive(Default, Debug, Clone)]
pub struct Config(Arc<ConfigInner>);
macro_rules! set_config_ok {
($s:ident, $id:ident) => {
$s.$id = var(&stringify!($id).to_uppercase()).ok();
};
}
macro_rules! set_config_parse {
($s:ident, $id:ident) => {
$s.$id = var(&stringify!($id).to_uppercase())
.ok()
.and_then(|x| x.parse().ok());
};
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn init_config() -> Result<Self, Error> {
let fname = Path::new("config.env");
let config_dir = dirs::config_dir().ok_or_else(|| format_err!("No CONFIG directory"))?;
let default_fname = config_dir.join("weather_util").join("config.env");
let env_file = if fname.exists() {
fname
} else {
&default_fname
};
dotenv::dotenv().ok();
if env_file.exists() {
dotenv::from_path(env_file).ok();
}
let mut conf = ConfigInner::default();
set_config_ok!(conf, api_key);
set_config_ok!(conf, api_endpoint);
set_config_ok!(conf, api_path);
set_config_parse!(conf, zipcode);
set_config_ok!(conf, country_code);
set_config_ok!(conf, city_name);
set_config_parse!(conf, lat);
set_config_parse!(conf, lon);
Ok(Self(Arc::new(conf)))
}
}
impl Deref for Config {
type Target = ConfigInner;
fn deref(&self) -> &Self::Target {
&self.0
}
}