From c5f5320f8ca5b955c57f5e5aad2543140a84cc09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Tue, 25 Jun 2024 13:22:16 +0200 Subject: [PATCH] remove NetworkParseError: From, construct explicitly --- src/errors.rs | 6 ------ src/parsers/combinators.rs | 33 ++++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 0684811..01110c2 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -105,12 +105,6 @@ impl From for NetworkParseError { } } -impl From for NetworkParseError { - fn from(e: ParseIntError) -> Self { - NetworkParseError::NetworkLengthParseError(e) - } -} - impl From for NetworkParseError { fn from(e: NetworkLengthTooLongError) -> Self { NetworkParseError::NetworkLengthTooLongError(e) diff --git a/src/parsers/combinators.rs b/src/parsers/combinators.rs index 04e0318..5d0d2a2 100644 --- a/src/parsers/combinators.rs +++ b/src/parsers/combinators.rs @@ -13,6 +13,11 @@ use crate::{ IpInet, }; +fn parse_prefix_len(s: &str) -> Result { + s.parse() + .map_err(NetworkParseError::NetworkLengthParseError) +} + /// 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`. @@ -29,7 +34,10 @@ 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])?, + parse_prefix_len(&s[pos + 1..])?, + ), } } @@ -63,7 +71,7 @@ where Some(pos) => { let inet = ::Inet::new( address_parser(&s[0..pos])?, - s[pos + 1..].parse()?, + parse_prefix_len(&s[pos + 1..])?, )?; Ok(inet.network()) }, @@ -103,7 +111,10 @@ where } match s.rfind('/') { None => Ok(host_parser(s)?.into()), - Some(pos) => AnyIpCidr::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?), + Some(pos) => AnyIpCidr::new( + address_parser(&s[0..pos])?, + parse_prefix_len(&s[pos + 1..])?, + ), } } @@ -138,11 +149,12 @@ where } match s.rfind('/') { None => Ok(host_parser(s)?.into()), - 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])?, + parse_prefix_len(&s[pos + 1..])?, + )? + .network() + .into()), } } @@ -177,7 +189,10 @@ 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])?, + parse_prefix_len(&s[pos + 1..])?, + )?), } }