Rewrite with anyhow

This commit is contained in:
Quentin 2020-05-09 16:04:06 +02:00
parent a19ae25a7d
commit 76c8404212
4 changed files with 17 additions and 32 deletions

5
Cargo.lock generated
View File

@ -11,9 +11,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.26"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c"
checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff"
[[package]]
name = "attohttpc"
@ -124,6 +124,7 @@ checksum = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
name = "diplonat"
version = "0.1.0"
dependencies = [
"anyhow",
"igd",
"log",
"pretty_env_logger",

View File

@ -13,3 +13,4 @@ log = "0.4"
pretty_env_logger = "0.4"
tokio = "0.2.11"
serde = "1.0.107"
anyhow = "1.0.28"

View File

@ -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))
};
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))
}
let config = DiplonatConfig {
private_ip: private_ip,
refresh_time: refresh_time,
expiration_time: expiration_time,
consul_node_name: consul_node_name
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 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))
}
info!("Consul node name: {}", config.consul_node_name);
info!("Private IP address: {}", config.private_ip);
info!("Refresh time: {} seconds", config.refresh_time);

View File

@ -1,4 +1,5 @@
use igd::Gateway;
use anyhow::Result;
use crate::*;
@ -7,7 +8,7 @@ pub struct DiplonatContext {
//pub gateway: igd::Gateway
}
pub fn setup() -> Result<DiplonatContext, String> {
pub fn setup() -> Result<DiplonatContext> {
return Ok(DiplonatContext {
config: config::load_env()?,
//gateway: search_gateway(Default::default()).await