Skip to content

Commit

Permalink
cargo +nightly fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
stbuehler committed Jun 24, 2024
1 parent 0715a7f commit 00aee1b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
5 changes: 4 additions & 1 deletion src/cidr/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ impl FromStr for AnyIpCidr {

fn from_str(s: &str) -> Result<Self, NetworkParseError> {
// TODO: use strict FromStr::from_str address parsing with version bump
crate::parsers::parse_any_cidr(s, crate::local_addr_parser::ParseableAddress::address_from_str)
crate::parsers::parse_any_cidr(
s,
crate::local_addr_parser::ParseableAddress::address_from_str,
)
}
}

Expand Down
90 changes: 56 additions & 34 deletions src/parsers/combinators.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
use std::net::{AddrParseError, IpAddr};
use std::net::{
AddrParseError,
IpAddr,
};

use crate::{errors::NetworkParseError, Address, AnyIpCidr, Cidr, Inet, IpInet};
use crate::{
errors::NetworkParseError,
Address,
AnyIpCidr,
Cidr,
Inet,
IpInet,
};

/// Parse [`Cidr`] with custom address and network (when no '/' separator was found) parser
///
/// If a '/' is found, parse trailing number as prefix length and leading address with `address_parser`.
/// Otherwise parse with `host_parser`.
pub fn parse_cidr_full<C, AP, NP>(s: &str, address_parser: AP, host_parser: NP) -> Result<C, NetworkParseError>
pub fn parse_cidr_full<C, AP, NP>(
s: &str,
address_parser: AP,
host_parser: NP,
) -> Result<C, NetworkParseError>
where
C: Cidr,
AP: FnOnce(&str) -> Result<C::Address, AddrParseError>,
NP: FnOnce(&str) -> Result<C, NetworkParseError>,
{
match s.rfind('/') {
None => host_parser(s),
Some(pos) => C::new(
address_parser(&s[0..pos])?,
s[pos + 1..].parse()?,
),
Some(pos) => C::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?),
}
}

Expand All @@ -30,15 +41,17 @@ where
C: Cidr,
AP: Fn(&str) -> Result<C::Address, AddrParseError>,
{
parse_cidr_full(s, &address_parser, |s| {
Ok(C::new_host(address_parser(s)?))
})
parse_cidr_full(s, &address_parser, |s| Ok(C::new_host(address_parser(s)?)))
}

/// Parse [`Cidr`] with custom address and network (when no '/' separator was found) parser
///
/// Similar to [`parse_cidr_full`] but ignores host bits in addresses.
pub fn parse_cidr_full_ignore_hostbits<C, AP, NP>(s: &str, address_parser: AP, host_parser: NP) -> Result<C, NetworkParseError>
pub fn parse_cidr_full_ignore_hostbits<C, AP, NP>(
s: &str,
address_parser: AP,
host_parser: NP,
) -> Result<C, NetworkParseError>
where
C: Cidr,
AP: FnOnce(&str) -> Result<C::Address, AddrParseError>,
Expand All @@ -59,37 +72,46 @@ where
/// Parse [`Cidr`] with custom address parser
///
/// Similar to [`parse_cidr`] but ignores host bits in addresses.
pub fn parse_cidr_ignore_hostbits<C, AP>(s: &str, address_parser: AP) -> Result<C, NetworkParseError>
pub fn parse_cidr_ignore_hostbits<C, AP>(
s: &str,
address_parser: AP,
) -> Result<C, NetworkParseError>
where
C: Cidr,
AP: Fn(&str) -> Result<C::Address, AddrParseError>,
{
parse_cidr_full_ignore_hostbits(s, &address_parser, |s| {
Ok(C::new_host(address_parser(s)?))
})
parse_cidr_full_ignore_hostbits(s, &address_parser, |s| Ok(C::new_host(address_parser(s)?)))
}

/// Parse [`AnyIpCidr`] with custom address and network (when no '/' separator was found) parser
///
/// Similar to [`parse_any_cidr_full`] but ignores host bits in addresses.
pub fn parse_any_cidr_full_ignore_hostbits<AP, NP>(s: &str, address_parser: AP, host_parser: NP) -> Result<AnyIpCidr, NetworkParseError>
pub fn parse_any_cidr_full_ignore_hostbits<AP, NP>(
s: &str,
address_parser: AP,
host_parser: NP,
) -> Result<AnyIpCidr, NetworkParseError>
where
AP: FnOnce(&str) -> Result<IpAddr, AddrParseError>,
NP: FnOnce(&str) -> Result<AnyIpCidr, NetworkParseError>,
{
match s.rfind('/') {
None => host_parser(s),
Some(pos) => Ok(IpInet::new(
address_parser(&s[0..pos])?,
s[pos + 1..].parse()?,
)?.network().into()),
Some(pos) => Ok(
IpInet::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?)?
.network()
.into(),
),
}
}

/// Parse [`AnyIpCidr`] with custom address parser
///
/// Similar to [`parse_any_cidr`] but ignores host bits in addresses.
pub fn parse_any_cidr_ignore_hostbits<AP>(s: &str, address_parser: AP) -> Result<AnyIpCidr, NetworkParseError>
pub fn parse_any_cidr_ignore_hostbits<AP>(
s: &str,
address_parser: AP,
) -> Result<AnyIpCidr, NetworkParseError>
where
AP: Fn(&str) -> Result<IpAddr, AddrParseError>,
{
Expand All @@ -106,17 +128,18 @@ where
///
/// If a '/' is found, parse trailing number as prefix length and leading address with `address_parser`.
/// Otherwise parse with `host_parser`.
pub fn parse_any_cidr_full<AP, NP>(s: &str, address_parser: AP, host_parser: NP) -> Result<AnyIpCidr, NetworkParseError>
pub fn parse_any_cidr_full<AP, NP>(
s: &str,
address_parser: AP,
host_parser: NP,
) -> Result<AnyIpCidr, NetworkParseError>
where
AP: FnOnce(&str) -> Result<IpAddr, AddrParseError>,
NP: FnOnce(&str) -> Result<AnyIpCidr, NetworkParseError>,
{
match s.rfind('/') {
None => host_parser(s),
Some(pos) => AnyIpCidr::new(
address_parser(&s[0..pos])?,
s[pos + 1..].parse()?,
),
Some(pos) => AnyIpCidr::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?),
}
}

Expand All @@ -142,18 +165,19 @@ where
///
/// If a '/' is found, parse trailing number as prefix length and leading address with `address_parser`.
/// Otherwise parse with `host_parser`.
pub fn parse_inet_full<I, AP, NP>(s: &str, address_parser: AP, host_parser: NP) -> Result<I, NetworkParseError>
pub fn parse_inet_full<I, AP, NP>(
s: &str,
address_parser: AP,
host_parser: NP,
) -> Result<I, NetworkParseError>
where
I: Inet,
AP: FnOnce(&str) -> Result<I::Address, AddrParseError>,
NP: FnOnce(&str) -> Result<I, NetworkParseError>,
{
match s.rfind('/') {
None => host_parser(s),
Some(pos) => Ok(I::new(
address_parser(&s[0..pos])?,
s[pos + 1..].parse()?,
)?),
Some(pos) => Ok(I::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?)?),
}
}

Expand All @@ -166,7 +190,5 @@ where
I: Inet,
AP: Fn(&str) -> Result<I::Address, AddrParseError>,
{
parse_inet_full(s, &address_parser, |s| {
Ok(I::new_host(address_parser(s)?))
})
parse_inet_full(s, &address_parser, |s| Ok(I::new_host(address_parser(s)?)))
}
21 changes: 16 additions & 5 deletions src/parsers/inetaddr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::net::{AddrParseError, IpAddr, Ipv4Addr};
use std::net::{
AddrParseError,
IpAddr,
Ipv4Addr,
};

/// Parse "loose" IPv4 address similar to POSIX `inet_addr`
///
Expand Down Expand Up @@ -71,7 +75,7 @@ pub fn parse_loose_ipv4(s: &str) -> Result<Ipv4Addr, AddrParseError> {
} else {
Err(e)
}
}
},
}
}

Expand All @@ -85,18 +89,25 @@ pub fn parse_loose_ip(s: &str) -> Result<IpAddr, AddrParseError> {
} else {
Err(e)
}
}
},
}
}

#[cfg(test)]
mod tests {
use std::net::{AddrParseError, IpAddr, Ipv4Addr};
use std::net::{
AddrParseError,
IpAddr,
Ipv4Addr,
};

fn run(s: &str, expect: Ipv4Addr) {
assert_eq!(super::inet_addr(s), Some(expect));
assert_eq!(super::parse_loose_ipv4(s), Ok::<_, AddrParseError>(expect));
assert_eq!(super::parse_loose_ip(s), Ok::<_, AddrParseError>(IpAddr::V4(expect)));
assert_eq!(
super::parse_loose_ip(s),
Ok::<_, AddrParseError>(IpAddr::V4(expect))
);
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ mod inetaddr;

pub use self::{
combinators::{
parse_any_cidr_full_ignore_hostbits,
parse_any_cidr,
parse_any_cidr_full,
parse_any_cidr_full_ignore_hostbits,
parse_any_cidr_ignore_hostbits,
parse_any_cidr,
parse_cidr_full_ignore_hostbits,
parse_cidr,
parse_cidr_full,
parse_cidr_full_ignore_hostbits,
parse_cidr_ignore_hostbits,
parse_cidr,
parse_inet_full,
parse_inet,
parse_inet_full,
},
inetaddr::{
inet_addr,
Expand Down

0 comments on commit 00aee1b

Please sign in to comment.