aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2020-05-09 16:04:06 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2020-05-09 16:04:06 +0200
commit76c84042120b97ee98af6a1c40ab86aca062408a (patch)
treee7e298a13c0b91a28aa1d939aae0408cae0a24dd /src/config.rs
parenta19ae25a7d20d338243e2fef5a1fd45437701c2a (diff)
downloaddiplonat-76c84042120b97ee98af6a1c40ab86aca062408a.tar.gz
diplonat-76c84042120b97ee98af6a1c40ab86aca062408a.zip
Rewrite with anyhow
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs38
1 files changed, 10 insertions, 28 deletions
diff --git a/src/config.rs b/src/config.rs
index bbc1b33..2cf2061 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,5 +1,5 @@
use std::env;
-
+use anyhow::{Result, anyhow};
use log::*;
pub struct DiplonatConfig {
@@ -9,41 +9,23 @@ pub struct DiplonatConfig {
pub expiration_time: u32
}
-pub fn load_env() -> Result<DiplonatConfig, String> {
+pub fn load_env() -> Result<DiplonatConfig> {
let env_private_ip = "DIPLONAT_PRIVATE_IP";
- let private_ip = match env::var(env_private_ip) {
- Ok(val) => val,
- Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)),
- };
-
let env_refresh_time = "DIPLONAT_REFRESH_TIME";
- let refresh_time: u32 = match env::var(env_refresh_time) {
- Ok(val) => val.parse().unwrap(),
- Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e))
- };
-
let env_expiration_time = "DIPLONAT_EXPIRATION_TIME";
- let expiration_time: u32 = match env::var(env_expiration_time) {
- Ok(val) => val.parse().unwrap(),
- Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e))
- };
-
let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME";
- let consul_node_name = match env::var(env_consul_node_name) {
- Ok(val) => val,
- Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e))
+
+ let config = DiplonatConfig {
+ private_ip: env::var(env_private_ip)?,
+ refresh_time: env::var(env_refresh_time)?.parse()?,
+ expiration_time: env::var(env_expiration_time)?.parse()?,
+ consul_node_name: env::var(env_consul_node_name)?
};
- if refresh_time * 2 > expiration_time {
- return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time))
+ if config.refresh_time * 2 > config.expiration_time {
+ return Err(anyhow!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", config.expiration_time, config.refresh_time))
}
- let config = DiplonatConfig {
- private_ip: private_ip,
- refresh_time: refresh_time,
- expiration_time: expiration_time,
- consul_node_name: consul_node_name
- };
info!("Consul node name: {}", config.consul_node_name);
info!("Private IP address: {}", config.private_ip);
info!("Refresh time: {} seconds", config.refresh_time);