Skip to content

Commit

Permalink
avoid empty vectors in TrustedAddress::to_addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Nov 21, 2019
1 parent caac50d commit f914eef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
29 changes: 18 additions & 11 deletions jormungandr/src/settings/start/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ custom_error! {pub AddressError
}

impl TrustedAddress {
pub fn to_addresses(&self) -> Result<Vec<Address>, AddressError> {
pub fn to_addresses(&self) -> Result<Option<Vec<Address>>, AddressError> {
use multiaddr::AddrComponent;
use std::iter::FromIterator;
use trust_dns_proto::rr::{record_data::RData, RecordType};
Expand Down Expand Up @@ -254,20 +254,27 @@ impl TrustedAddress {
.collect(),
Some(AddrComponent::IP4(addr)) => vec![AddrComponent::IP4(addr)],
Some(AddrComponent::IP6(addr)) => vec![AddrComponent::IP6(addr)],
_ => Vec::new(),
_ => return Ok(None),
};

if addresses.is_empty() {
return Ok(None);
}

if let Some(AddrComponent::TCP(port)) = components.next() {
Ok(addresses
.into_iter()
.map(|addr| {
let new_components = vec![addr, AddrComponent::TCP(port)];
let new_multiaddr = multiaddr::Multiaddr::from_iter(new_components.into_iter());
Address(poldercast::Address::new(new_multiaddr).unwrap())
})
.collect())
Ok(Some(
addresses
.into_iter()
.map(|addr| {
let new_components = vec![addr, AddrComponent::TCP(port)];
let new_multiaddr =
multiaddr::Multiaddr::from_iter(new_components.into_iter());
Address(poldercast::Address::new(new_multiaddr).unwrap())
})
.collect(),
))
} else {
Ok(Vec::new())
Ok(None)
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions jormungandr/src/settings/start/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ fn generate_network(
command_arguments: &StartArguments,
config: &Option<Config>,
) -> Result<network::Configuration, Error> {
use crate::settings::start::network::TrustedPeer;

let mut p2p = if let Some(cfg) = config {
cfg.p2p.clone()
} else {
Expand Down Expand Up @@ -235,14 +237,16 @@ fn generate_network(
.unwrap_or(vec![])
.into_iter()
.filter_map(|tp| {
tp.address.to_addresses().ok().map(|addrs| {
addrs.into_iter().map(move |address| {
crate::settings::start::network::TrustedPeer {
tp.address
.to_addresses()
.ok()
.and_then(std::convert::identity)
.map(|addrs| {
addrs.into_iter().map(move |addr| TrustedPeer {
id: tp.id.clone(),
address: address.0,
}
address: addr.0,
})
})
})
})
.flatten()
.collect(),
Expand Down

0 comments on commit f914eef

Please sign in to comment.