Skip to content

Commit c5f5320

Browse files
committed
remove NetworkParseError: From<ParseIntError>, construct explicitly
1 parent b9147bf commit c5f5320

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ impl From<AddrParseError> for NetworkParseError {
105105
}
106106
}
107107

108-
impl From<ParseIntError> for NetworkParseError {
109-
fn from(e: ParseIntError) -> Self {
110-
NetworkParseError::NetworkLengthParseError(e)
111-
}
112-
}
113-
114108
impl From<NetworkLengthTooLongError> for NetworkParseError {
115109
fn from(e: NetworkLengthTooLongError) -> Self {
116110
NetworkParseError::NetworkLengthTooLongError(e)

src/parsers/combinators.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ use crate::{
1313
IpInet,
1414
};
1515

16+
fn parse_prefix_len(s: &str) -> Result<u8, NetworkParseError> {
17+
s.parse()
18+
.map_err(NetworkParseError::NetworkLengthParseError)
19+
}
20+
1621
/// Parse [`Cidr`] with custom address and network (when no '/' separator was found) parser
1722
///
1823
/// If a '/' is found, parse trailing number as prefix length and leading address with `address_parser`.
@@ -29,7 +34,10 @@ where
2934
{
3035
match s.rfind('/') {
3136
None => host_parser(s),
32-
Some(pos) => C::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?),
37+
Some(pos) => C::new(
38+
address_parser(&s[0..pos])?,
39+
parse_prefix_len(&s[pos + 1..])?,
40+
),
3341
}
3442
}
3543

@@ -63,7 +71,7 @@ where
6371
Some(pos) => {
6472
let inet = <C::Address as Address>::Inet::new(
6573
address_parser(&s[0..pos])?,
66-
s[pos + 1..].parse()?,
74+
parse_prefix_len(&s[pos + 1..])?,
6775
)?;
6876
Ok(inet.network())
6977
},
@@ -103,7 +111,10 @@ where
103111
}
104112
match s.rfind('/') {
105113
None => Ok(host_parser(s)?.into()),
106-
Some(pos) => AnyIpCidr::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?),
114+
Some(pos) => AnyIpCidr::new(
115+
address_parser(&s[0..pos])?,
116+
parse_prefix_len(&s[pos + 1..])?,
117+
),
107118
}
108119
}
109120

@@ -138,11 +149,12 @@ where
138149
}
139150
match s.rfind('/') {
140151
None => Ok(host_parser(s)?.into()),
141-
Some(pos) => Ok(
142-
IpInet::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?)?
143-
.network()
144-
.into(),
145-
),
152+
Some(pos) => Ok(IpInet::new(
153+
address_parser(&s[0..pos])?,
154+
parse_prefix_len(&s[pos + 1..])?,
155+
)?
156+
.network()
157+
.into()),
146158
}
147159
}
148160

@@ -177,7 +189,10 @@ where
177189
{
178190
match s.rfind('/') {
179191
None => host_parser(s),
180-
Some(pos) => Ok(I::new(address_parser(&s[0..pos])?, s[pos + 1..].parse()?)?),
192+
Some(pos) => Ok(I::new(
193+
address_parser(&s[0..pos])?,
194+
parse_prefix_len(&s[pos + 1..])?,
195+
)?),
181196
}
182197
}
183198

0 commit comments

Comments
 (0)