From 7d478d997620c0af41a6ffb8b3f29dc6dda2cb98 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 20 Sep 2021 15:00:31 +0200 Subject: Stricter Rust formating rules and build in the CI --- src/config/options.rs | 4 +--- src/config/options_test.rs | 3 +-- src/config/runtime.rs | 17 +++++++++-------- src/consul.rs | 11 +++++------ src/consul_actor.rs | 25 ++++++++++++------------- src/diplonat.rs | 11 +++++------ src/fw.rs | 6 ++++-- src/fw_actor.rs | 12 ++++++------ src/igd_actor.rs | 13 +++++++------ src/messages.rs | 2 +- 10 files changed, 51 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/config/options.rs b/src/config/options.rs index f62d14c..b8dccf7 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -62,9 +62,7 @@ impl ConfigOpts { // Currently only used in tests #[allow(dead_code)] pub fn from_iter(iter: Iter) -> Result - where - Iter: IntoIterator, - { + where Iter: IntoIterator { let base: ConfigOptsBase = envy::prefixed("DIPLONAT_").from_iter(iter.clone())?; let consul: ConfigOptsConsul = envy::prefixed("DIPLONAT_CONSUL_").from_iter(iter.clone())?; let acme: ConfigOptsAcme = envy::prefixed("DIPLONAT_ACME_").from_iter(iter.clone())?; diff --git a/src/config/options_test.rs b/src/config/options_test.rs index c0c7367..790a14e 100644 --- a/src/config/options_test.rs +++ b/src/config/options_test.rs @@ -1,5 +1,4 @@ -use std::collections::HashMap; -use std::time::Duration; +use std::{collections::HashMap, time::Duration}; use crate::config::*; diff --git a/src/config/runtime.rs b/src/config/runtime.rs index 0d52b15..eeb34f6 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -6,8 +6,8 @@ use crate::config::{ConfigOpts, ConfigOptsAcme, ConfigOptsBase, ConfigOptsConsul // This code is inspired by the Trunk crate (https://github.com/thedodd/trunk) -// In this file, we take ConfigOpts and transform them into ready-to-use RuntimeConfig. -// We apply default values and business logic. +// In this file, we take ConfigOpts and transform them into ready-to-use +// RuntimeConfig. We apply default values and business logic. #[derive(Debug)] pub struct RuntimeConfigAcme { @@ -59,12 +59,11 @@ impl RuntimeConfig { impl RuntimeConfigAcme { pub fn new(opts: ConfigOptsAcme) -> Result> { if !opts.enable { - return Ok(None); + return Ok(None) } let email = opts.email.expect( - "'DIPLONAT_ACME_EMAIL' environment variable is required \ - if 'DIPLONAT_ACME_ENABLE' == 'true'", + "'DIPLONAT_ACME_EMAIL' environment variable is required if 'DIPLONAT_ACME_ENABLE' == 'true'", ); Ok(Some(Self { email })) @@ -105,9 +104,11 @@ impl RuntimeConfigIgd { if refresh_time.as_secs() * 2 > expiration_time.as_secs() { return Err(anyhow!( - "IGD expiration time (currently: {}s) must be at least twice bigger than refresh time (currently: {}s)", - expiration_time.as_secs(), - refresh_time.as_secs())); + "IGD expiration time (currently: {}s) must be at least twice bigger than refresh time \ + (currently: {}s)", + expiration_time.as_secs(), + refresh_time.as_secs() + )) } Ok(Self { diff --git a/src/consul.rs b/src/consul.rs index 1123996..4e6fe5d 100644 --- a/src/consul.rs +++ b/src/consul.rs @@ -1,6 +1,7 @@ +use std::collections::HashMap; + use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; #[derive(Serialize, Deserialize, Debug)] pub struct ServiceEntry { @@ -24,12 +25,10 @@ impl Consul { client: reqwest::Client::new(), url: url.to_string(), idx: None, - }; + } } - pub fn watch_node_reset(&mut self) -> () { - self.idx = None; - } + pub fn watch_node_reset(&mut self) -> () { self.idx = None; } pub async fn watch_node(&mut self, host: &str) -> Result { let url = match self.idx { @@ -44,6 +43,6 @@ impl Consul { }; let resp: CatalogNode = http.json().await?; - return Ok(resp); + return Ok(resp) } } diff --git a/src/consul_actor.rs b/src/consul_actor.rs index d66f7fd..b111f09 100644 --- a/src/consul_actor.rs +++ b/src/consul_actor.rs @@ -1,14 +1,12 @@ -use crate::consul; -use crate::messages; +use std::{cmp, collections::HashSet, time::Duration}; + use anyhow::Result; use log::*; use serde::{Deserialize, Serialize}; use serde_lexpr::{error, from_str}; -use std::cmp; -use std::collections::HashSet; -use std::time::Duration; -use tokio::sync::watch; -use tokio::time::delay_for; +use tokio::{sync::watch, time::delay_for}; + +use crate::{consul, messages}; #[derive(Serialize, Deserialize, Debug)] pub enum DiplonatParameter { @@ -32,11 +30,12 @@ pub struct ConsulActor { fn retry_to_time(retries: u32, max_time: Duration) -> Duration { // 1.2^x seems to be a good value to exponentially increase time at a good pace - // eg. 1.2^32 = 341 seconds ~= 5 minutes - ie. after 32 retries we wait 5 minutes + // eg. 1.2^32 = 341 seconds ~= 5 minutes - ie. after 32 retries we wait 5 + // minutes return Duration::from_secs(cmp::min( max_time.as_secs(), 1.2f64.powf(retries as f64) as u64, - )); + )) } fn to_parameters(catalog: &consul::CatalogNode) -> Vec { @@ -52,7 +51,7 @@ fn to_parameters(catalog: &consul::CatalogNode) -> Vec { } } - return r; + return r } fn to_open_ports(params: &Vec) -> messages::PublicExposedPorts { @@ -71,7 +70,7 @@ fn to_open_ports(params: &Vec) -> messages::PublicExposedPorts { } } - return op; + return op } impl ConsulActor { @@ -87,7 +86,7 @@ impl ConsulActor { tx_open_ports: tx, node: node.to_string(), retries: 0, - }; + } } pub async fn listen(&mut self) -> Result<()> { @@ -104,7 +103,7 @@ impl ConsulActor { e ); delay_for(will_retry_in).await; - continue; + continue } }; self.retries = 0; diff --git a/src/diplonat.rs b/src/diplonat.rs index a17bdc0..496f0ab 100644 --- a/src/diplonat.rs +++ b/src/diplonat.rs @@ -1,10 +1,9 @@ use anyhow::Result; use tokio::try_join; -use crate::config::ConfigOpts; -use crate::consul_actor::ConsulActor; -use crate::fw_actor::FirewallActor; -use crate::igd_actor::IgdActor; +use crate::{ + config::ConfigOpts, consul_actor::ConsulActor, fw_actor::FirewallActor, igd_actor::IgdActor, +}; pub struct Diplonat { consul: ConsulActor, @@ -35,7 +34,7 @@ impl Diplonat { firewall: fw, }; - return Ok(ctx); + return Ok(ctx) } pub async fn listen(&mut self) -> Result<()> { @@ -45,6 +44,6 @@ impl Diplonat { self.firewall.listen() )?; - return Ok(()); + return Ok(()) } } diff --git a/src/fw.rs b/src/fw.rs index 0b07a12..e18a301 100644 --- a/src/fw.rs +++ b/src/fw.rs @@ -1,9 +1,11 @@ -use crate::messages; +use std::collections::HashSet; + use anyhow::{Context, Result}; use iptables; use log::*; use regex::Regex; -use std::collections::HashSet; + +use crate::messages; pub fn setup(ipt: &iptables::IPTables) -> Result<()> { // ensure we start from a clean state without any rule already set diff --git a/src/fw_actor.rs b/src/fw_actor.rs index 5147a9c..0fa6e64 100644 --- a/src/fw_actor.rs +++ b/src/fw_actor.rs @@ -1,4 +1,7 @@ +use std::collections::HashSet; + use anyhow::Result; +use iptables; use log::*; use tokio::{ select, @@ -6,10 +9,7 @@ use tokio::{ time::{self, Duration}, }; -use crate::fw; -use crate::messages; -use iptables; -use std::collections::HashSet; +use crate::{fw, messages}; pub struct FirewallActor { pub ipt: iptables::IPTables, @@ -32,7 +32,7 @@ impl FirewallActor { fw::setup(&ctx.ipt)?; - return Ok(ctx); + return Ok(ctx) } pub async fn listen(&mut self) -> Result<()> { @@ -81,6 +81,6 @@ impl FirewallActor { fw::open_ports(&self.ipt, ports_to_open)?; - return Ok(()); + return Ok(()) } } diff --git a/src/igd_actor.rs b/src/igd_actor.rs index 4ff5f53..bb9099e 100644 --- a/src/igd_actor.rs +++ b/src/igd_actor.rs @@ -1,15 +1,16 @@ -use crate::messages; +use std::net::SocketAddrV4; + use anyhow::{Context, Result}; -use igd::aio::*; -use igd::PortMappingProtocol; +use igd::{aio::*, PortMappingProtocol}; use log::*; -use std::net::SocketAddrV4; use tokio::{ select, sync::watch, time::{self, Duration}, }; +use crate::messages; + pub struct IgdActor { last_ports: messages::PublicExposedPorts, rx_ports: watch::Receiver, @@ -40,7 +41,7 @@ impl IgdActor { last_ports: messages::PublicExposedPorts::new(), }; - return Ok(ctx); + return Ok(ctx) } pub async fn listen(&mut self) -> Result<()> { @@ -92,6 +93,6 @@ impl IgdActor { } } - return Ok(()); + return Ok(()) } } diff --git a/src/messages.rs b/src/messages.rs index 63f16b0..b622be1 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -11,6 +11,6 @@ impl PublicExposedPorts { return Self { tcp_ports: HashSet::new(), udp_ports: HashSet::new(), - }; + } } } -- cgit v1.2.3