diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/net.rs | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/net.rs b/src/net.rs index 2f5d98e29a..3ee5aaca76 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1,14 +1,25 @@ -use std::error::Error; use std::fmt; use std::io; use std::iter; use std::net::SocketAddr; -use std::num::ParseIntError; use std::option; use std::slice; use std::str::FromStr; use std::vec; + +error_chain! { + errors { + /// Error indicating parsing the address failed + AddrParseError + } + foreign_links { + AddrParsePortError(::std::num::ParseIntError) + #[doc = "The port part of the string did not correspond to a valid network port"]; + } +} + + /// Representation of a TCP or UDP endpoint. The IP level address is represented by either an IP /// directly or a hostname/domain. The IP level address together with a port becomes a socket /// address. @@ -44,19 +55,19 @@ impl RemoteAddr { } } - fn from_domain_str(s: &str) -> Result<Self, AddrParseError> { + fn from_domain_str(s: &str) -> Result<Self> { let (address, port_str) = Self::split_at_last_colon(s)?; let port = u16::from_str(port_str)?; if address.is_empty() || address.contains(':') { - return Err(AddrParseError(())); + return Err(Error::from(ErrorKind::AddrParseError)); } Ok(RemoteAddr::Domain(address.to_owned(), port)) } - fn split_at_last_colon(s: &str) -> Result<(&str, &str), AddrParseError> { + fn split_at_last_colon(s: &str) -> Result<(&str, &str)> { let mut iter = s.rsplitn(2, ':'); let port = iter.next().unwrap(); - let address = iter.next().ok_or(AddrParseError(()))?; + let address = iter.next().ok_or_else(|| Error::from(ErrorKind::AddrParseError))?; Ok((address, port)) } } @@ -68,8 +79,8 @@ impl From<SocketAddr> for RemoteAddr { } impl FromStr for RemoteAddr { - type Err = AddrParseError; - fn from_str(s: &str) -> Result<Self, Self::Err> { + type Err = Error; + fn from_str(s: &str) -> Result<Self> { if let Ok(addr) = SocketAddr::from_str(s) { Ok(RemoteAddr::from(addr)) } else { @@ -78,7 +89,6 @@ impl FromStr for RemoteAddr { } } - impl fmt::Display for RemoteAddr { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -88,29 +98,6 @@ impl fmt::Display for RemoteAddr { } } -/// Representation of the errors that can happen when parsing a string into a `RemoteAddr`. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct AddrParseError(()); - -impl From<ParseIntError> for AddrParseError { - fn from(_: ParseIntError) -> Self { - AddrParseError(()) - } -} - -impl fmt::Display for AddrParseError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.write_str(self.description()) - } -} - -impl Error for AddrParseError { - fn description(&self) -> &str { - "Invalid remote address format" - } -} - - /// A trait for objects which can be converted to one or more `RemoteAddr` values. pub trait ToRemoteAddrs { /// Returned iterator over remote addresses which this type may correspond |
