Skip to content

Commit

Permalink
remove NetworkParseError: From<ParseIntError>, construct explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
stbuehler committed Sep 30, 2024
1 parent b9147bf commit c5f5320
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
6 changes: 0 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ impl From<AddrParseError> for NetworkParseError {
}
}

impl From<ParseIntError> for NetworkParseError {
fn from(e: ParseIntError) -> Self {
NetworkParseError::NetworkLengthParseError(e)
}
}

impl From<NetworkLengthTooLongError> for NetworkParseError {
fn from(e: NetworkLengthTooLongError) -> Self {
NetworkParseError::NetworkLengthTooLongError(e)
Expand Down
33 changes: 24 additions & 9 deletions src/parsers/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ use crate::{
IpInet,
};

fn parse_prefix_len(s: &str) -> Result<u8, NetworkParseError> {
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`.
Expand All @@ -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..])?,
),
}
}

Expand Down Expand Up @@ -63,7 +71,7 @@ where
Some(pos) => {
let inet = <C::Address as Address>::Inet::new(
address_parser(&s[0..pos])?,
s[pos + 1..].parse()?,
parse_prefix_len(&s[pos + 1..])?,
)?;
Ok(inet.network())
},
Expand Down Expand Up @@ -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..])?,
),
}
}

Expand Down Expand Up @@ -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()),
}
}

Expand Down Expand Up @@ -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..])?,
)?),
}
}

Expand Down

0 comments on commit c5f5320

Please sign in to comment.