From b4d74000f1f70d4d942e92f75e7a7c4eb2c42341 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Tue, 9 May 2017 13:30:18 -0500 Subject: [PATCH] Replace usage of try! with ? Fixes #330 ? or do not. There is no try. --- src/host.rs | 22 ++++++++--------- src/lib.rs | 38 ++++++++++++++-------------- src/parser.rs | 55 ++++++++++++++++++++--------------------- src/percent_encoding.rs | 2 +- src/quirks.rs | 2 +- 5 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/host.rs b/src/host.rs index 6eae31ddb..50488772e 100644 --- a/src/host.rs +++ b/src/host.rs @@ -50,7 +50,7 @@ impl ::serde::Serialize for HostInternal { impl ::serde::Deserialize for HostInternal { fn deserialize(deserializer: &mut D) -> Result where D: ::serde::Deserializer { use std::net::IpAddr; - Ok(match try!(::serde::Deserialize::deserialize(deserializer)) { + Ok(match ::serde::Deserialize::deserialize(deserializer)? { None => HostInternal::None, Some(None) => HostInternal::Domain, Some(Some(IpAddr::V4(addr))) => HostInternal::Ipv4(addr), @@ -105,7 +105,7 @@ impl ::serde::Serialize for Host { impl ::serde::Deserialize for Host { fn deserialize(deserializer: &mut D) -> Result where D: ::serde::Deserializer { use std::net::IpAddr; - Ok(match try!(::serde::Deserialize::deserialize(deserializer)) { + Ok(match ::serde::Deserialize::deserialize(deserializer)? { Ok(s) => Host::Domain(s), Err(IpAddr::V4(addr)) => Host::Ipv4(addr), Err(IpAddr::V6(addr)) => Host::Ipv6(addr), @@ -146,13 +146,13 @@ impl Host { return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6) } let domain = percent_decode(input.as_bytes()).decode_utf8_lossy(); - let domain = try!(idna::domain_to_ascii(&domain)); + let domain = idna::domain_to_ascii(&domain)?; if domain.find(|c| matches!(c, '\0' | '\t' | '\n' | '\r' | ' ' | '#' | '%' | '/' | ':' | '?' | '@' | '[' | '\\' | ']' )).is_some() { return Err(ParseError::InvalidDomainCharacter) } - if let Some(address) = try!(parse_ipv4addr(&domain)) { + if let Some(address) = parse_ipv4addr(&domain)? { Ok(Host::Ipv4(address)) } else { Ok(Host::Domain(domain.into())) @@ -166,8 +166,8 @@ impl> fmt::Display for Host { Host::Domain(ref domain) => domain.as_ref().fmt(f), Host::Ipv4(ref addr) => addr.fmt(f), Host::Ipv6(ref addr) => { - try!(f.write_str("[")); - try!(write_ipv6(addr, f)); + f.write_str("[")?; + write_ipv6(addr, f)?; f.write_str("]") } } @@ -200,7 +200,7 @@ impl> ToSocketAddrs for HostAndPort { match self.host { Host::Domain(ref domain) => Ok(SocketAddrs { // FIXME: use std::net::lookup_host when it’s stable. - state: SocketAddrsState::Domain(try!((domain.as_ref(), port).to_socket_addrs())) + state: SocketAddrsState::Domain((domain.as_ref(), port).to_socket_addrs()?) }), Host::Ipv4(address) => Ok(SocketAddrs { state: SocketAddrsState::One(SocketAddr::V4(SocketAddrV4::new(address, port))) @@ -243,9 +243,9 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result { let mut i = 0; while i < 8 { if i == compress_start { - try!(f.write_str(":")); + f.write_str(":")?; if i == 0 { - try!(f.write_str(":")); + f.write_str(":")?; } if compress_end < 8 { i = compress_end; @@ -253,9 +253,9 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result { break; } } - try!(write!(f, "{:x}", segments[i as usize])); + write!(f, "{:x}", segments[i as usize])?; if i < 7 { - try!(f.write_str(":")); + f.write_str(":")?; } i += 1; } diff --git a/src/lib.rs b/src/lib.rs index e64af8f82..0740d8495 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -781,7 +781,7 @@ impl Url { /// # use std::io; /// /// fn connect(url: &Url) -> io::Result { - /// TcpStream::connect(try!(url.with_default_port(default_port))) + /// TcpStream::connect(url.with_default_port(default_port)?) /// } /// /// fn default_port(url: &Url) -> Result { @@ -797,13 +797,13 @@ impl Url { pub fn with_default_port(&self, f: F) -> io::Result> where F: FnOnce(&Url) -> Result { Ok(HostAndPort { - host: try!(self.host() - .ok_or(()) - .or_else(|()| io_error("URL has no host"))), - port: try!(self.port_or_known_default() - .ok_or(()) - .or_else(|()| f(self)) - .or_else(|()| io_error("URL has no port number"))) + host: self.host() + .ok_or(()) + .or_else(|()| io_error("URL has no host"))?, + port: self.port_or_known_default() + .ok_or(()) + .or_else(|()| f(self)) + .or_else(|()| io_error("URL has no port number"))? }) } @@ -1192,7 +1192,7 @@ impl Url { if host == "" && SchemeType::from(self.scheme()).is_special() { return Err(ParseError::EmptyHost); } - self.set_host_internal(try!(Host::parse(host)), None) + self.set_host_internal(Host::parse(host)?, None) } else if self.has_host() { if SchemeType::from(self.scheme()).is_special() { return Err(ParseError::EmptyHost) @@ -1407,7 +1407,7 @@ impl Url { /// ``` pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> { let mut parser = Parser::for_setter(String::new()); - let remaining = try!(parser.parse_scheme(parser::Input::new(scheme))); + let remaining = parser.parse_scheme(parser::Input::new(scheme))?; if !remaining.is_empty() || (!self.has_host() && SchemeType::from(&parser.serialization).is_special()) { return Err(()) @@ -1458,7 +1458,7 @@ impl Url { pub fn from_file_path>(path: P) -> Result { let mut serialization = "file://".to_owned(); let path_start = serialization.len() as u32; - try!(path_to_file_url_segments(path.as_ref(), &mut serialization)); + path_to_file_url_segments(path.as_ref(), &mut serialization)?; Ok(Url { serialization: serialization, scheme_end: "file".len() as u32, @@ -1491,7 +1491,7 @@ impl Url { /// Note that `std::path` does not consider trailing slashes significant /// and usually does not include them (e.g. in `Path::parent()`). pub fn from_directory_path>(path: P) -> Result { - let mut url = try!(Url::from_file_path(path)); + let mut url = Url::from_file_path(path)?; if !url.serialization.ends_with('/') { url.serialization.push('/') } @@ -1532,7 +1532,7 @@ impl Url { use serde::{Deserialize, Error}; let (serialization, scheme_end, username_end, host_start, host_end, host, port, path_start, - query_start, fragment_start) = try!(Deserialize::deserialize(deserializer)); + query_start, fragment_start) = Deserialize::deserialize(deserializer)?; let url = Url { serialization: serialization, scheme_end: scheme_end, @@ -1546,7 +1546,7 @@ impl Url { fragment_start: fragment_start }; if cfg!(debug_assertions) { - try!(url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason))) + url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason))? } Ok(url) } @@ -1598,7 +1598,7 @@ impl ToSocketAddrs for Url { type Iter = SocketAddrs; fn to_socket_addrs(&self) -> io::Result { - try!(self.with_default_port(|_| Err(()))).to_socket_addrs() + self.with_default_port(|_| Err(()))?.to_socket_addrs() } } @@ -1707,7 +1707,7 @@ impl rustc_serialize::Encodable for Url { #[cfg(feature="rustc-serialize")] impl rustc_serialize::Decodable for Url { fn decode(decoder: &mut D) -> Result { - Url::parse(&*try!(decoder.read_str())).map_err(|error| { + Url::parse(&*decoder.read_str()?).map_err(|error| { decoder.error(&format!("URL parsing error: {}", error)) }) } @@ -1729,7 +1729,7 @@ impl serde::Serialize for Url { #[cfg(feature="serde")] impl serde::Deserialize for Url { fn deserialize(deserializer: &mut D) -> Result where D: serde::Deserializer { - let string_representation: String = try!(serde::Deserialize::deserialize(deserializer)); + let string_representation: String = serde::Deserialize::deserialize(deserializer)?; Url::parse(&string_representation).map_err(|err| { serde::Error::invalid_value(err.description()) }) @@ -1789,7 +1789,7 @@ fn path_to_file_url_segments_windows(path: &Path, serialization: &mut String) -> for component in components { if component == Component::RootDir { continue } // FIXME: somehow work with non-unicode? - let component = try!(component.as_os_str().to_str().ok_or(())); + let component = component.as_os_str().to_str().ok_or(())?; serialization.push('/'); serialization.extend(percent_encode(component.as_bytes(), PATH_SEGMENT_ENCODE_SET)); } @@ -1822,7 +1822,7 @@ fn file_url_segments_to_pathbuf(segments: str::Split) -> Result) -> Result { - let first = try!(segments.next().ok_or(())); + let first = segments.next().ok_or(())?; let mut string = match first.len() { 2 => { diff --git a/src/parser.rs b/src/parser.rs index 92f279797..e01fad9d6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -307,7 +307,7 @@ impl<'a> Parser<'a> { } fn parse_with_scheme(mut self, input: Input) -> ParseResult { - let scheme_end = try!(to_u32(self.serialization.len())); + let scheme_end = to_u32(self.serialization.len())?; let scheme_type = SchemeType::from(&self.serialization); self.serialization.push(':'); match scheme_type { @@ -350,7 +350,7 @@ impl<'a> Parser<'a> { return self.after_double_slash(input, scheme_type, scheme_end) } // Anarchist URL (no authority) - let path_start = try!(to_u32(self.serialization.len())); + let path_start = to_u32(self.serialization.len())?; let username_end = path_start; let host_start = path_start; let host_end = path_start; @@ -413,7 +413,7 @@ impl<'a> Parser<'a> { }; self.serialization.push_str(before_query); let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(base_url.scheme_end, input)); + self.parse_query_and_fragment(base_url.scheme_end, input)?; Ok(Url { serialization: self.serialization, query_start: query_start, @@ -425,7 +425,7 @@ impl<'a> Parser<'a> { let scheme_end = "file".len() as u32; let path_start = "file://".len() as u32; let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(scheme_end, input)); + self.parse_query_and_fragment(scheme_end, input)?; Ok(Url { serialization: self.serialization, scheme_end: scheme_end, @@ -474,8 +474,8 @@ impl<'a> Parser<'a> { let scheme_end = "file".len() as u32; let host_start = "file://".len() as u32; let (path_start, host, remaining) = - try!(self.parse_file_host(input_after_next_char)); - let host_end = try!(to_u32(self.serialization.len())); + self.parse_file_host(input_after_next_char)?; + let host_end = to_u32(self.serialization.len())?; let mut has_host = !matches!(host, HostInternal::None); let remaining = if path_start { self.parse_path_start(SchemeType::File, &mut has_host, remaining) @@ -486,7 +486,7 @@ impl<'a> Parser<'a> { }; // FIXME: deal with has_host let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(scheme_end, remaining)); + self.parse_query_and_fragment(scheme_end, remaining)?; Ok(Url { serialization: self.serialization, scheme_end: scheme_end, @@ -514,7 +514,7 @@ impl<'a> Parser<'a> { let remaining = self.parse_path( SchemeType::File, &mut false, path_start, input_after_first_char); let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(scheme_end, remaining)); + self.parse_query_and_fragment(scheme_end, remaining)?; let path_start = path_start as u32; Ok(Url { serialization: self.serialization, @@ -554,7 +554,7 @@ impl<'a> Parser<'a> { let remaining = self.parse_path( SchemeType::File, &mut false, path_start, input); let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(scheme_end, remaining)); + self.parse_query_and_fragment(scheme_end, remaining)?; let path_start = path_start as u32; Ok(Url { serialization: self.serialization, @@ -601,7 +601,7 @@ impl<'a> Parser<'a> { }; self.serialization.push_str(before_query); let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(base_url.scheme_end, input)); + self.parse_query_and_fragment(base_url.scheme_end, input)?; Ok(Url { serialization: self.serialization, query_start: query_start, @@ -654,13 +654,13 @@ impl<'a> Parser<'a> { self.serialization.push('/'); self.serialization.push('/'); // authority state - let (username_end, remaining) = try!(self.parse_userinfo(input, scheme_type)); + let (username_end, remaining) = self.parse_userinfo(input, scheme_type)?; // host state - let host_start = try!(to_u32(self.serialization.len())); + let host_start = to_u32(self.serialization.len())?; let (host_end, host, port, remaining) = - try!(self.parse_host_and_port(remaining, scheme_end, scheme_type)); + self.parse_host_and_port(remaining, scheme_end, scheme_type)?; // path state - let path_start = try!(to_u32(self.serialization.len())); + let path_start = to_u32(self.serialization.len())?; let remaining = self.parse_path_start( scheme_type, &mut true, remaining); self.with_query_and_fragment(scheme_end, username_end, host_start, @@ -692,8 +692,8 @@ impl<'a> Parser<'a> { char_count += 1; } let (mut userinfo_char_count, remaining) = match last_at { - None => return Ok((try!(to_u32(self.serialization.len())), input)), - Some((0, remaining)) => return Ok((try!(to_u32(self.serialization.len())), remaining)), + None => return Ok((to_u32(self.serialization.len())?, input)), + Some((0, remaining)) => return Ok((to_u32(self.serialization.len())?, remaining)), Some(x) => x }; @@ -703,7 +703,7 @@ impl<'a> Parser<'a> { userinfo_char_count -= 1; if c == ':' && username_end.is_none() { // Start parsing password - username_end = Some(try!(to_u32(self.serialization.len()))); + username_end = Some(to_u32(self.serialization.len())?); self.serialization.push(':'); } else { self.check_url_code_point(c, &input); @@ -712,7 +712,7 @@ impl<'a> Parser<'a> { } let username_end = match username_end { Some(i) => i, - None => try!(to_u32(self.serialization.len())), + None => to_u32(self.serialization.len())?, }; self.serialization.push('@'); Ok((username_end, remaining)) @@ -721,13 +721,12 @@ impl<'a> Parser<'a> { fn parse_host_and_port<'i>(&mut self, input: Input<'i>, scheme_end: u32, scheme_type: SchemeType) -> ParseResult<(u32, HostInternal, Option, Input<'i>)> { - let (host, remaining) = try!( - Parser::parse_host(input, scheme_type)); + let (host, remaining) = Parser::parse_host(input, scheme_type)?; write!(&mut self.serialization, "{}", host).unwrap(); - let host_end = try!(to_u32(self.serialization.len())); + let host_end = to_u32(self.serialization.len())?; let (port, remaining) = if let Some(remaining) = remaining.split_prefix(':') { let scheme = || default_port(&self.serialization[..scheme_end as usize]); - try!(Parser::parse_port(remaining, scheme, self.context)) + Parser::parse_port(remaining, scheme, self.context)? } else { (None, remaining) }; @@ -781,7 +780,7 @@ impl<'a> Parser<'a> { if scheme_type.is_special() && host_str.is_empty() { return Err(ParseError::EmptyHost) } - let host = try!(Host::parse(host_str)); + let host = Host::parse(host_str)?; Ok((host, input)) } @@ -820,7 +819,7 @@ impl<'a> Parser<'a> { let host = if host_str.is_empty() { HostInternal::None } else { - match try!(Host::parse(host_str)) { + match Host::parse(host_str)? { Host::Domain(ref d) if d == "localhost" => HostInternal::None, host => { write!(&mut self.serialization, "{}", host).unwrap(); @@ -999,7 +998,7 @@ impl<'a> Parser<'a> { port: Option, path_start: u32, remaining: Input) -> ParseResult { let (query_start, fragment_start) = - try!(self.parse_query_and_fragment(scheme_end, remaining)); + self.parse_query_and_fragment(scheme_end, remaining)?; Ok(Url { serialization: self.serialization, scheme_end: scheme_end, @@ -1021,7 +1020,7 @@ impl<'a> Parser<'a> { match input.next() { Some('#') => {} Some('?') => { - query_start = Some(try!(to_u32(self.serialization.len()))); + query_start = Some(to_u32(self.serialization.len())?); self.serialization.push('?'); let remaining = self.parse_query(scheme_end, input); if let Some(remaining) = remaining { @@ -1034,7 +1033,7 @@ impl<'a> Parser<'a> { _ => panic!("Programming error. parse_query_and_fragment() called without ? or # {:?}") } - let fragment_start = try!(to_u32(self.serialization.len())); + let fragment_start = to_u32(self.serialization.len())?; self.serialization.push('#'); self.parse_fragment(input); Ok((query_start, Some(fragment_start))) @@ -1077,7 +1076,7 @@ impl<'a> Parser<'a> { self.parse_fragment(input); Ok(Url { serialization: self.serialization, - fragment_start: Some(try!(to_u32(before_fragment.len()))), + fragment_start: Some(to_u32(before_fragment.len())?), ..*base_url }) } diff --git a/src/percent_encoding.rs b/src/percent_encoding.rs index a4fb6177a..8a621ffe4 100644 --- a/src/percent_encoding.rs +++ b/src/percent_encoding.rs @@ -210,7 +210,7 @@ impl<'a, E: EncodeSet> Iterator for PercentEncode<'a, E> { impl<'a, E: EncodeSet> fmt::Display for PercentEncode<'a, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { for c in (*self).clone() { - try!(formatter.write_str(c)) + formatter.write_str(c)? } Ok(()) } diff --git a/src/quirks.rs b/src/quirks.rs index 9a7537f47..1bc88303c 100644 --- a/src/quirks.rs +++ b/src/quirks.rs @@ -40,7 +40,7 @@ pub fn href(url: &Url) -> &str { /// Setter for https://url.spec.whatwg.org/#dom-url-href pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> { - *url = try!(Url::parse(value)); + *url = Url::parse(value)?; Ok(()) }