diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-02-08 11:48:27 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-02-08 11:51:34 +0100 |
| commit | fa39b2d2354cc6de2aa9b15379b1c45b31b6f822 (patch) | |
| tree | df7357a9339637ae0512f08290beabe8761ae1e8 | |
| parent | 93c53e7267056572615f2e29b1bf804b1c671380 (diff) | |
| download | mullvadvpn-fa39b2d2354cc6de2aa9b15379b1c45b31b6f822.tar.xz mullvadvpn-fa39b2d2354cc6de2aa9b15379b1c45b31b6f822.zip | |
Convert net module to use error-chain
| -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 |
