From f5ac36e21f4ae2f720070b5fff550954c7af0b25 Mon Sep 17 00:00:00 2001 From: adrien Date: Wed, 22 Sep 2021 13:12:57 +0200 Subject: [PATCH] WIP: rewrote runtime.rs using match to convey intent better --- src/config/runtime.rs | 62 +++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/config/runtime.rs b/src/config/runtime.rs index 1cc409c..89acdfd 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -62,10 +62,14 @@ impl RuntimeConfig { impl RuntimeConfigConsul { pub(super) fn new(opts: ConfigOptsConsul) -> Result { - let node_name = opts - .node_name - .expect("'DIPLONAT_CONSUL_NODE_NAME' is required"); - let url = opts.url.unwrap_or(super::CONSUL_URL.to_string()); + let node_name = match opts.node_name { + Some(n) => n, + _ => return Err(anyhow!("'DIPLONAT_CONSUL_NODE_NAME' is required")), + }; + let url = match opts.url { + Some(url) => url, + _ => super::CONSUL_URL.to_string(), + }; Ok(Self { node_name, url }) } @@ -75,11 +79,16 @@ impl RuntimeConfigAcme { pub fn new(opts: ConfigOptsAcme) -> Result> { if !opts.enable { return Ok(None) - } + }; - let email = opts - .email - .expect("'DIPLONAT_ACME_EMAIL' is required if ACME is enabled"); + let email = match opts.email { + Some(email) => email, + _ => { + return Err(anyhow!( + "'DIPLONAT_ACME_EMAIL' is required if ACME is enabled" + )) + } + }; Ok(Some(Self { email })) } @@ -91,7 +100,13 @@ impl RuntimeConfigFirewall { return Ok(None) } - let refresh_time = Duration::from_secs(opts.refresh_time.unwrap_or(super::REFRESH_TIME).into()); + let refresh_time = Duration::from_secs( + match opts.refresh_time { + Some(t) => t, + _ => super::REFRESH_TIME, + } + .into(), + ); Ok(Some(Self { refresh_time })) } @@ -103,16 +118,29 @@ impl RuntimeConfigIgd { return Ok(None) } - let private_ip = opts - .private_ip - .expect("'DIPLONAT_IGD_PRIVATE_IP' is required if IGD is enabled"); + let private_ip = match opts.private_ip { + Some(ip) => ip, + _ => { + return Err(anyhow!( + "'DIPLONAT_IGD_PRIVATE_IP' is required if IGD is enabled" + )) + } + }; + let expiration_time = Duration::from_secs( - opts - .expiration_time - .unwrap_or(super::EXPIRATION_TIME) - .into(), + match opts.expiration_time { + Some(t) => t.into(), + _ => super::EXPIRATION_TIME, + } + .into(), + ); + let refresh_time = Duration::from_secs( + match opts.refresh_time { + Some(t) => t, + _ => super::REFRESH_TIME, + } + .into(), ); - let refresh_time = Duration::from_secs(opts.refresh_time.unwrap_or(super::REFRESH_TIME).into()); if refresh_time.as_secs() * 2 > expiration_time.as_secs() { return Err(anyhow!(