diff --git a/src/cidr/any.rs b/src/cidr/any.rs index 6cdf90b..1426585 100644 --- a/src/cidr/any.rs +++ b/src/cidr/any.rs @@ -244,7 +244,10 @@ impl FromStr for AnyIpCidr { fn from_str(s: &str) -> Result { // 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, + ) } } diff --git a/src/parsers/combinators.rs b/src/parsers/combinators.rs index 8641009..c54caa8 100644 --- a/src/parsers/combinators.rs +++ b/src/parsers/combinators.rs @@ -1,12 +1,26 @@ -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(s: &str, address_parser: AP, host_parser: NP) -> Result +pub fn parse_cidr_full( + s: &str, + address_parser: AP, + host_parser: NP, +) -> Result where C: Cidr, AP: FnOnce(&str) -> Result, @@ -14,10 +28,7 @@ where { 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()?), } } @@ -30,15 +41,17 @@ where C: Cidr, AP: Fn(&str) -> Result, { - 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(s: &str, address_parser: AP, host_parser: NP) -> Result +pub fn parse_cidr_full_ignore_hostbits( + s: &str, + address_parser: AP, + host_parser: NP, +) -> Result where C: Cidr, AP: FnOnce(&str) -> Result, @@ -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(s: &str, address_parser: AP) -> Result +pub fn parse_cidr_ignore_hostbits( + s: &str, + address_parser: AP, +) -> Result where C: Cidr, AP: Fn(&str) -> Result, { - 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(s: &str, address_parser: AP, host_parser: NP) -> Result +pub fn parse_any_cidr_full_ignore_hostbits( + s: &str, + address_parser: AP, + host_parser: NP, +) -> Result where AP: FnOnce(&str) -> Result, NP: FnOnce(&str) -> Result, { 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(s: &str, address_parser: AP) -> Result +pub fn parse_any_cidr_ignore_hostbits( + s: &str, + address_parser: AP, +) -> Result where AP: Fn(&str) -> Result, { @@ -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(s: &str, address_parser: AP, host_parser: NP) -> Result +pub fn parse_any_cidr_full( + s: &str, + address_parser: AP, + host_parser: NP, +) -> Result where AP: FnOnce(&str) -> Result, NP: FnOnce(&str) -> Result, { 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()?), } } @@ -142,7 +165,11 @@ 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(s: &str, address_parser: AP, host_parser: NP) -> Result +pub fn parse_inet_full( + s: &str, + address_parser: AP, + host_parser: NP, +) -> Result where I: Inet, AP: FnOnce(&str) -> Result, @@ -150,10 +177,7 @@ where { 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()?)?), } } @@ -166,7 +190,5 @@ where I: Inet, AP: Fn(&str) -> Result, { - 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)?))) } diff --git a/src/parsers/inetaddr.rs b/src/parsers/inetaddr.rs index c050d6f..acee9fe 100644 --- a/src/parsers/inetaddr.rs +++ b/src/parsers/inetaddr.rs @@ -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` /// @@ -71,7 +75,7 @@ pub fn parse_loose_ipv4(s: &str) -> Result { } else { Err(e) } - } + }, } } @@ -85,18 +89,25 @@ pub fn parse_loose_ip(s: &str) -> Result { } 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] diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index 86b7f28..1b6e227 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -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,