diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2016-12-13 14:11:23 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2016-12-13 14:11:23 +0100 |
| commit | ee64cfd9ce1e7f4c604314a083fffaa29b7967b9 (patch) | |
| tree | 1014fabf5eeeb028b727db03d2cb3c94d94fc970 | |
| parent | 6e34677dc1127aeb002464321e980a3dc984ff37 (diff) | |
| download | mullvadvpn-ee64cfd9ce1e7f4c604314a083fffaa29b7967b9.tar.xz mullvadvpn-ee64cfd9ce1e7f4c604314a083fffaa29b7967b9.zip | |
Simplify error handling
| -rw-r--r-- | src/net.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/net.rs b/src/net.rs index 9a8a8431f5..8cbc0c25e6 100644 --- a/src/net.rs +++ b/src/net.rs @@ -3,6 +3,7 @@ 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; @@ -44,18 +45,18 @@ impl RemoteAddr { } fn from_domain_str(s: &str) -> Result<Self, AddrParseError> { - let (address, port_str) = Self::split_at_last_colon(s).map_err(|_| AddrParseError(()))?; - let port = u16::from_str(port_str).map_err(|_| AddrParseError(()))?; + let (address, port_str) = Self::split_at_last_colon(s)?; + let port = u16::from_str(port_str)?; if address.len() == 0 || address.contains(':') { return Err(AddrParseError(())); } Ok(RemoteAddr::Domain(address.to_owned(), port)) } - fn split_at_last_colon(s: &str) -> Result<(&str, &str), ()> { + fn split_at_last_colon(s: &str) -> Result<(&str, &str), AddrParseError> { let mut iter = s.rsplitn(2, ":"); let port = iter.next().unwrap(); - let address = iter.next().ok_or(())?; + let address = iter.next().ok_or(AddrParseError(()))?; Ok((address, port)) } } @@ -91,6 +92,12 @@ impl fmt::Display for 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()) |
