From d5b7f4a0243b8b9713c7b6487ddfe1cd0e858ecd Mon Sep 17 00:00:00 2001 From: Mikhail Epifanov Date: Fri, 23 May 2025 14:04:53 +0200 Subject: [PATCH] use and a flat_map for the addresses --- src/address.rs | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/address.rs b/src/address.rs index 9d7594e..50fb096 100644 --- a/src/address.rs +++ b/src/address.rs @@ -136,30 +136,27 @@ pub fn parse_excluded_networks( exclude_addresses: &Option>, resolver: &Resolver, ) -> Vec { - let mut excluded_cidrs: Vec = Vec::new(); - if let Some(exclude_addresses) = exclude_addresses { - for addr in exclude_addresses { - if let Ok(cidr) = IpCidr::from_str(addr) { - excluded_cidrs.push(cidr); - } else if let Ok(ip) = addr.parse::() { - let cidr = match ip { - IpAddr::V4(_) => IpCidr::from_str(&format!("{}/32", ip)).unwrap(), - IpAddr::V6(_) => IpCidr::from_str(&format!("{}/128", ip)).unwrap(), - }; - excluded_cidrs.push(cidr); - } else { - let resolved_ips = resolve_ips_from_host(addr, resolver); - for ip in resolved_ips { - let cidr = match ip { - IpAddr::V4(_) => IpCidr::from_str(&format!("{}/32", ip)).unwrap(), - IpAddr::V6(_) => IpCidr::from_str(&format!("{}/128", ip)).unwrap(), - }; - excluded_cidrs.push(cidr); - } - } - } + exclude_addresses + .iter() + .flatten() + .flat_map(|addr| parse_single_excluded_address(addr, resolver)) + .collect() +} + +/// Parses a single address into an IpCidr, handling CIDR notation, IP addresses, and hostnames. +fn parse_single_excluded_address(addr: &str, resolver: &Resolver) -> Vec { + if let Ok(cidr) = IpCidr::from_str(addr) { + return vec![cidr]; } - excluded_cidrs + + if let Ok(ip) = IpAddr::from_str(addr) { + return vec![IpCidr::new_host(ip)]; + } + + resolve_ips_from_host(addr, resolver) + .into_iter() + .map(IpCidr::new_host) + .collect() } /// Derive a DNS resolver.