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
use std::path::{Path, PathBuf};
use serde::Deserialize;
use figment::{
providers::{Format, Toml},
Figment
};
use crate::err::Error;
#[derive(Debug, Default, Deserialize)]
pub struct Config {
pub channel: Option<u8>,
pub auth: Option<Auth>,
pub sender: Option<Sender>,
pub receiver: Option<Receiver>
}
#[derive(Debug, Default, Deserialize)]
pub struct Auth {
pub name: Option<String>,
pub pass: Option<String>,
#[serde(rename = "pass-file")]
pub pass_file: Option<String>,
pub token: Option<String>,
#[serde(rename = "token-file")]
pub token_file: Option<String>
}
#[derive(Debug, Default, Deserialize)]
pub struct Sender {
pub mgmtif: Option<String>,
pub msgif: Option<String>
}
#[derive(Debug, Default, Deserialize)]
pub struct Receiver {
pub mgmtif: Option<String>,
pub subif: Option<String>,
#[serde(rename = "sub-retries")]
pub sub_retries: Option<u32>,
#[serde(rename = "sub-retry-delay")]
pub sub_retry_delay: Option<String>,
#[serde(rename = "push-listenif")]
pub push_listenif: Option<String>
}
pub fn load_conf(fname: Option<&Path>) -> Result<Option<Config>, Error> {
let f = match fname {
Some(p) => p.to_path_buf(),
None => match std::env::var_os("DDMW_APPCONF") {
Some(val) => PathBuf::from(val),
None => PathBuf::from("ddmwapp.toml")
}
};
if !f.exists() {
Ok(None)
} else {
let conf = Figment::new().merge(Toml::file(f)).extract()?;
Ok(conf)
}
}