WIP: rewrote runtime.rs using match to convey intent better

This commit is contained in:
adrien 2021-09-22 13:12:57 +02:00
parent beefcd7ade
commit f5ac36e21f

View file

@ -62,10 +62,14 @@ impl RuntimeConfig {
impl RuntimeConfigConsul { impl RuntimeConfigConsul {
pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> { pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> {
let node_name = opts let node_name = match opts.node_name {
.node_name Some(n) => n,
.expect("'DIPLONAT_CONSUL_NODE_NAME' is required"); _ => return Err(anyhow!("'DIPLONAT_CONSUL_NODE_NAME' is required")),
let url = opts.url.unwrap_or(super::CONSUL_URL.to_string()); };
let url = match opts.url {
Some(url) => url,
_ => super::CONSUL_URL.to_string(),
};
Ok(Self { node_name, url }) Ok(Self { node_name, url })
} }
@ -75,11 +79,16 @@ impl RuntimeConfigAcme {
pub fn new(opts: ConfigOptsAcme) -> Result<Option<Self>> { pub fn new(opts: ConfigOptsAcme) -> Result<Option<Self>> {
if !opts.enable { if !opts.enable {
return Ok(None) return Ok(None)
} };
let email = opts let email = match opts.email {
.email Some(email) => email,
.expect("'DIPLONAT_ACME_EMAIL' is required if ACME is enabled"); _ => {
return Err(anyhow!(
"'DIPLONAT_ACME_EMAIL' is required if ACME is enabled"
))
}
};
Ok(Some(Self { email })) Ok(Some(Self { email }))
} }
@ -91,7 +100,13 @@ impl RuntimeConfigFirewall {
return Ok(None) 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 })) Ok(Some(Self { refresh_time }))
} }
@ -103,16 +118,29 @@ impl RuntimeConfigIgd {
return Ok(None) return Ok(None)
} }
let private_ip = opts let private_ip = match opts.private_ip {
.private_ip Some(ip) => ip,
.expect("'DIPLONAT_IGD_PRIVATE_IP' is required if IGD is enabled"); _ => {
return Err(anyhow!(
"'DIPLONAT_IGD_PRIVATE_IP' is required if IGD is enabled"
))
}
};
let expiration_time = Duration::from_secs( let expiration_time = Duration::from_secs(
opts match opts.expiration_time {
.expiration_time Some(t) => t.into(),
.unwrap_or(super::EXPIRATION_TIME) _ => super::EXPIRATION_TIME,
.into(), }
.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() { if refresh_time.as_secs() * 2 > expiration_time.as_secs() {
return Err(anyhow!( return Err(anyhow!(