Skip to content

Commit 01d4731

Browse files
authored
Rollup merge of #96168 - chris-morgan:AddrParseError-description-improvements, r=joshtriplett
Improve AddrParseError description The existing description was incorrect for socket addresses, and misleading: users would see “invalid IP address syntax” and suppose they were supposed to provide an IP address rather than a socket address. I contemplated making it two variants (IP, socket), but realised we can do still better for the IPv4 and IPv6 types, so here it is as six. I contemplated more precise error descriptions (e.g. “invalid IPv6 socket address syntax: expected a decimal scope ID after %”), but that’s a more invasive change, and probably not worthwhile anyway.
2 parents 53f028d + 0255398 commit 01d4731

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

Diff for: library/std/src/net/parser.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ impl<'a> Parser<'a> {
5959

6060
/// Run a parser, but fail if the entire input wasn't consumed.
6161
/// Doesn't run atomically.
62-
fn parse_with<T, F>(&mut self, inner: F) -> Result<T, AddrParseError>
62+
fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError>
6363
where
6464
F: FnOnce(&mut Parser<'_>) -> Option<T>,
6565
{
6666
let result = inner(self);
67-
if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(()))
67+
if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind))
6868
}
6969

7070
/// Peek the next character from the input
@@ -278,7 +278,7 @@ impl<'a> Parser<'a> {
278278
impl FromStr for IpAddr {
279279
type Err = AddrParseError;
280280
fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
281-
Parser::new(s).parse_with(|p| p.read_ip_addr())
281+
Parser::new(s).parse_with(|p| p.read_ip_addr(), AddrKind::Ip)
282282
}
283283
}
284284

@@ -288,9 +288,9 @@ impl FromStr for Ipv4Addr {
288288
fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
289289
// don't try to parse if too long
290290
if s.len() > 15 {
291-
Err(AddrParseError(()))
291+
Err(AddrParseError(AddrKind::Ipv4))
292292
} else {
293-
Parser::new(s).parse_with(|p| p.read_ipv4_addr())
293+
Parser::new(s).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4)
294294
}
295295
}
296296
}
@@ -299,34 +299,44 @@ impl FromStr for Ipv4Addr {
299299
impl FromStr for Ipv6Addr {
300300
type Err = AddrParseError;
301301
fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
302-
Parser::new(s).parse_with(|p| p.read_ipv6_addr())
302+
Parser::new(s).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6)
303303
}
304304
}
305305

306306
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
307307
impl FromStr for SocketAddrV4 {
308308
type Err = AddrParseError;
309309
fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
310-
Parser::new(s).parse_with(|p| p.read_socket_addr_v4())
310+
Parser::new(s).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4)
311311
}
312312
}
313313

314314
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
315315
impl FromStr for SocketAddrV6 {
316316
type Err = AddrParseError;
317317
fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
318-
Parser::new(s).parse_with(|p| p.read_socket_addr_v6())
318+
Parser::new(s).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6)
319319
}
320320
}
321321

322322
#[stable(feature = "rust1", since = "1.0.0")]
323323
impl FromStr for SocketAddr {
324324
type Err = AddrParseError;
325325
fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
326-
Parser::new(s).parse_with(|p| p.read_socket_addr())
326+
Parser::new(s).parse_with(|p| p.read_socket_addr(), AddrKind::Socket)
327327
}
328328
}
329329

330+
#[derive(Debug, Clone, PartialEq, Eq)]
331+
enum AddrKind {
332+
Ip,
333+
Ipv4,
334+
Ipv6,
335+
Socket,
336+
SocketV4,
337+
SocketV6,
338+
}
339+
330340
/// An error which can be returned when parsing an IP address or a socket address.
331341
///
332342
/// This error is used as the error type for the [`FromStr`] implementation for
@@ -353,7 +363,7 @@ impl FromStr for SocketAddr {
353363
/// ```
354364
#[stable(feature = "rust1", since = "1.0.0")]
355365
#[derive(Debug, Clone, PartialEq, Eq)]
356-
pub struct AddrParseError(());
366+
pub struct AddrParseError(AddrKind);
357367

358368
#[stable(feature = "addr_parse_error_error", since = "1.4.0")]
359369
impl fmt::Display for AddrParseError {
@@ -367,6 +377,13 @@ impl fmt::Display for AddrParseError {
367377
impl Error for AddrParseError {
368378
#[allow(deprecated)]
369379
fn description(&self) -> &str {
370-
"invalid IP address syntax"
380+
match self.0 {
381+
AddrKind::Ip => "invalid IP address syntax",
382+
AddrKind::Ipv4 => "invalid IPv4 address syntax",
383+
AddrKind::Ipv6 => "invalid IPv6 address syntax",
384+
AddrKind::Socket => "invalid socket address syntax",
385+
AddrKind::SocketV4 => "invalid IPv4 socket address syntax",
386+
AddrKind::SocketV6 => "invalid IPv6 socket address syntax",
387+
}
371388
}
372389
}

0 commit comments

Comments
 (0)