From deeecd93e1fc5545e6e0280d1799436638e84a38 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 22 May 2020 18:13:32 +0200 Subject: Rewrite for clarity --- src/consul_actor.rs | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/consul_actor.rs b/src/consul_actor.rs index 241a643..de5e216 100644 --- a/src/consul_actor.rs +++ b/src/consul_actor.rs @@ -35,29 +35,38 @@ fn retry_to_time(retries: u32, max_time: Duration) -> Duration { return Duration::from_secs(cmp::min(max_time.as_secs(), 1.2f64.powf(retries as f64) as u64)) } -fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::PublicExposedPorts { - let mut op = messages::PublicExposedPorts { - tcp_ports: Vec::new(), - udp_ports: Vec::new() - }; +fn to_parameters(catalog: &consul::CatalogNode) -> Vec { + let mut r = Vec::new(); for (_, service_info) in &catalog.Services { for tag in &service_info.Tags { let diplo_conf: error::Result = from_str(tag); match diplo_conf { - Ok(conf) => { - let DiplonatConsul::diplonat(c) = conf; - for parameter in &c { - match parameter { - DiplonatParameter::tcp_port(p) => op.tcp_ports.extend(p), - DiplonatParameter::udp_port(p) => op.udp_ports.extend(p), - } - } - } + Ok(conf) => r.push(conf), Err(e) => debug!("Failed to parse entry {}. {}", tag, e), }; } } + + return r; +} + +fn to_open_ports(params: &Vec) -> messages::PublicExposedPorts { + let mut op = messages::PublicExposedPorts { + tcp_ports: Vec::new(), + udp_ports: Vec::new() + }; + + for conf in params { + let DiplonatConsul::diplonat(c) = conf; + for parameter in c { + match parameter { + DiplonatParameter::tcp_port(p) => op.tcp_ports.extend(p), + DiplonatParameter::udp_port(p) => op.udp_ports.extend(p), + }; + } + } + return op; } @@ -91,7 +100,7 @@ impl ConsulActor { } }; self.retries = 0; - info!("{:#?}", from_catalog_to_open_ports(&catalog)); + info!("{:#?}", to_open_ports(&to_parameters(&catalog))); } } } -- cgit v1.2.3