From 5dc5162765c12454f1652d93d03beeb961ec7c6b Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Wed, 7 Aug 2019 20:44:24 +0100 Subject: [PATCH] update and tidy code --- Cargo.toml | 1 + src/async_impl/client.rs | 27 ++++++++++++--------------- src/async_impl/decoder.rs | 6 +++--- src/async_impl/multipart.rs | 4 ++-- src/async_impl/request.rs | 4 ++-- src/async_impl/response.rs | 15 ++++++--------- src/body.rs | 6 +++--- src/client.rs | 4 ++-- src/connect.rs | 9 +++------ src/error.rs | 2 +- src/multipart.rs | 10 +++++----- src/proxy.rs | 5 ++--- src/redirect.rs | 6 +++--- src/wait.rs | 2 +- 14 files changed, 46 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e8d9fe9..e281547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ authors = ["Sean McArthur "] readme = "README.md" license = "MIT/Apache-2.0" categories = ["web-programming::http-client"] +edition = "2018" publish = false diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index b04f795..7c833a8 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -42,7 +42,7 @@ use crate::{Certificate, Identity}; #[cfg(feature = "tls")] use crate::tls::TlsBackend; -static DEFAULT_USER_AGENT: &'static str = +static DEFAULT_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); /// An asynchronous `Client` to make Requests with. @@ -101,7 +101,7 @@ impl ClientBuilder { ClientBuilder { config: Config { gzip: true, - headers: headers, + headers, #[cfg(feature = "default-tls")] hostname_verification: true, #[cfg(feature = "tls")] @@ -338,7 +338,7 @@ impl ClientBuilder { pub fn use_sys_proxy(mut self) -> ClientBuilder { let proxies = get_proxies(); self.config.proxies.push(Proxy::custom(move |url| { - return if proxies.contains_key(url.scheme()) { + if proxies.contains_key(url.scheme()) { Some((*proxies.get(url.scheme()).unwrap()).clone()) } else { None @@ -610,16 +610,16 @@ impl Client { Pending { inner: PendingInner::Request(PendingRequest { - method: method, - url: url, - headers: headers, + method, + url, + headers, body: reusable, urls: Vec::new(), client: self.inner.clone(), - in_flight: in_flight, + in_flight, timeout, }), } @@ -644,14 +644,11 @@ impl Client { for proxy in self.inner.proxies.iter() { if proxy.is_match(dst) { - match proxy.http_basic_auth(dst) { - Some(header) => { - headers.insert( - PROXY_AUTHORIZATION, - header, - ); - }, - None => (), + if let Some(header) = proxy.http_basic_auth(dst) { + headers.insert( + PROXY_AUTHORIZATION, + header, + ); } break; diff --git a/src/async_impl/decoder.rs b/src/async_impl/decoder.rs index 4ff75cc..8d4653d 100644 --- a/src/async_impl/decoder.rs +++ b/src/async_impl/decoder.rs @@ -117,12 +117,12 @@ impl Decoder { content_encoding_gzip = headers .get_all(CONTENT_ENCODING) .iter() - .fold(false, |acc, enc| acc || enc == "gzip"); + .any(|enc| enc == "gzip"); content_encoding_gzip || headers .get_all(TRANSFER_ENCODING) .iter() - .fold(false, |acc, enc| acc || enc == "gzip") + .any(|enc| enc == "gzip") }; if is_gzip { if let Some(content_length) = headers.get(CONTENT_LENGTH) { @@ -264,7 +264,7 @@ impl ReadableChunks { pub(crate) fn new(stream: S) -> Self { ReadableChunks { state: ReadState::NotReady, - stream: stream, + stream, } } } diff --git a/src/async_impl/multipart.rs b/src/async_impl/multipart.rs index 19fb15d..ccb76c4 100644 --- a/src/async_impl/multipart.rs +++ b/src/async_impl/multipart.rs @@ -61,7 +61,7 @@ impl Form { /// # Examples /// /// ``` - /// let form = reqwest::async::multipart::Form::new() + /// let form = reqwest::r#async::multipart::Form::new() /// .text("username", "seanmonstar") /// .text("password", "secret"); /// ``` @@ -98,7 +98,7 @@ impl Form { /// Consume this instance and transform into an instance of hyper::Body for use in a request. pub(crate) fn stream(mut self) -> hyper::Body { - if self.inner.fields.len() == 0 { + if self.inner.fields.is_empty(){ return hyper::Body::empty(); } diff --git a/src/async_impl/request.rs b/src/async_impl/request.rs index 10e6a2a..b87e9f0 100644 --- a/src/async_impl/request.rs +++ b/src/async_impl/request.rs @@ -201,8 +201,8 @@ impl RequestBuilder { /// # use futures::future::Future; /// /// # fn run() -> Result<(), Error> { - /// let client = reqwest::async::Client::new(); - /// let form = reqwest::async::multipart::Form::new() + /// let client = reqwest::r#async::Client::new(); + /// let form = reqwest::r#async::multipart::Form::new() /// .text("key3", "value3") /// .text("key4", "value4"); /// diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index c312538..1d6e6f6 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -184,7 +184,7 @@ impl Response { /// # Example /// /// ``` - /// # use reqwest::async::Response; + /// # use reqwest::r#async::Response; /// fn on_response(res: Response) { /// match res.error_for_status() { /// Ok(_res) => (), @@ -214,7 +214,7 @@ impl Response { /// # Example /// /// ``` - /// # use reqwest::async::Response; + /// # use reqwest::r#async::Response; /// fn on_response(res: &Response) { /// match res.error_for_status_ref() { /// Ok(_res) => (), @@ -263,7 +263,7 @@ impl> From> for Response { status: parts.status, headers: parts.headers, url: Box::new(url), - body: body, + body, version: parts.version, extensions: parts.extensions, } @@ -307,10 +307,7 @@ impl Future for Text { // a block because of borrow checker { let (text, _, _) = self.encoding.decode(&bytes); - match text { - Cow::Owned(s) => return Ok(Async::Ready(s)), - _ => (), - } + if let Cow::Owned(s) = text { return Ok(Async::Ready(s)) } } unsafe { // decoding returned Cow::Borrowed, meaning these bytes @@ -339,8 +336,8 @@ pub trait ResponseBuilderExt { /// # use std::error::Error; /// use url::Url; /// use http::response::Builder; - /// use reqwest::async::ResponseBuilderExt; - /// # fn main() -> Result<(), Box> { + /// use reqwest::r#async::ResponseBuilderExt; + /// # fn main() -> Result<(), Box> { /// let response = Builder::new() /// .status(200) /// .url(Url::parse("http://example.com")?) diff --git a/src/body.rs b/src/body.rs index 511852a..cfe0468 100644 --- a/src/body.rs +++ b/src/body.rs @@ -99,7 +99,7 @@ impl Body { let (tx, rx) = hyper::Body::channel(); let tx = Sender { body: (read, len), - tx: tx, + tx, }; (Some(tx), async_impl::Body::wrap(rx), len) }, @@ -250,7 +250,7 @@ impl Sender { // input stream as soon as the data received is valid JSON. // This behaviour is questionable, but it exists and the // fact is that there is actually no remaining data to read. - if buf.len() == 0 { + if buf.is_empty() { if buf.remaining_mut() == 0 { buf.reserve(8192); } @@ -285,7 +285,7 @@ impl Sender { written += buf.len() as u64; let tx = tx.as_mut().expect("tx only taken on error"); - if let Err(_) = tx.send_data(buf.take().freeze().into()) { + if tx.send_data(buf.take().freeze().into()).is_err() { return Err(crate::error::timedout(None)); } }) diff --git a/src/client.rs b/src/client.rs index da77339..4d9cbe3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -561,7 +561,7 @@ impl ClientHandle { let (mut rt, client) = match built { Ok((rt, c)) => { - if let Err(_) = spawn_tx.send(Ok(())) { + if spawn_tx.send(Ok(())).is_err() { return; } (rt, c) @@ -626,7 +626,7 @@ impl ClientHandle { Ok(ClientHandle { - timeout: timeout, + timeout, inner: inner_handle, }) } diff --git a/src/connect.rs b/src/connect.rs index e6f466a..95e8239 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -394,15 +394,12 @@ fn tunnel(conn: T, host: String, port: u16, auth: Option<::http::header::Head Host: {0}:{1}\r\n\ ", host, port).into_bytes(); - match auth { - Some(value) => { + if let Some(value) = auth { debug!("tunnel to {}:{} using basic auth", host, port); buf.extend_from_slice(b"Proxy-Authorization: "); buf.extend_from_slice(value.as_bytes()); buf.extend_from_slice(b"\r\n"); - }, - None => (), - } + } // headers end buf.extend_from_slice(b"\r\n"); @@ -699,7 +696,7 @@ mod tests { use super::tunnel; use crate::proxy; - static TUNNEL_OK: &'static [u8] = b"\ + static TUNNEL_OK: &[u8] = b"\ HTTP/1.1 200 OK\r\n\ \r\n\ "; diff --git a/src/error.rs b/src/error.rs index d5c84ce..0cb1ee4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -248,7 +248,7 @@ impl fmt::Debug for Error { } } -static BLOCK_IN_FUTURE: &'static str = "blocking Client used inside a Future context"; +static BLOCK_IN_FUTURE: &str = "blocking Client used inside a Future context"; impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/multipart.rs b/src/multipart.rs index 789a9df..8bce09e 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -296,7 +296,7 @@ impl fmt::Debug for Reader { impl Reader { fn new(form: Form) -> Reader { let mut reader = Reader { - form: form, + form, active_reader: None, }; reader.next_reader(); @@ -304,13 +304,13 @@ impl Reader { } fn next_reader(&mut self) { - self.active_reader = if self.form.inner.fields.len() != 0 { + self.active_reader = if !self.form.inner.fields.is_empty() { // We need to move out of the vector here because we are consuming the field's reader let (name, field) = self.form.inner.fields.remove(0); let boundary = Cursor::new(format!("--{}\r\n", self.form.boundary())); let header = Cursor::new({ // Try to use cached headers created by compute_length - let mut h = if self.form.inner.computed_headers.len() > 0 { + let mut h = if !self.form.inner.computed_headers.is_empty() { self.form.inner.computed_headers.remove(0) } else { self.form.inner.percent_encoding.encode_headers(&name, field.metadata()) @@ -324,7 +324,7 @@ impl Reader { .chain(Cursor::new("\r\n")); // According to https://tools.ietf.org/html/rfc2046#section-5.1.1 // the very last field has a special boundary - if self.form.inner.fields.len() != 0 { + if !self.form.inner.fields.is_empty() { Some(Box::new(reader)) } else { Some(Box::new(reader.chain(Cursor::new( @@ -352,7 +352,7 @@ impl Read for Reader { } None => return Ok(total_bytes_read), }; - if last_read_bytes == 0 && buf.len() != 0 { + if last_read_bytes == 0 && !buf.is_empty() { self.next_reader(); } } diff --git a/src/proxy.rs b/src/proxy.rs index dff8ff1..b0c7291 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -397,8 +397,7 @@ impl Custom { uri.scheme(), uri.host(), uri.port().map(|_| ":").unwrap_or(""), - uri.port().map(|p| p.to_string()).unwrap_or(String::new()) - ) + uri.port().map(|p| p.to_string()).unwrap_or_default()) .parse() .expect("should be valid Url"); @@ -517,7 +516,7 @@ fn get_from_environment() -> HashMap { if key.ends_with(PROXY_KEY_ENDS) { let end_indx = key.len() - PROXY_KEY_ENDS.len(); let schema = &key[..end_indx]; - insert_proxy(&mut proxies, String::from(schema), String::from(value)); + insert_proxy(&mut proxies, String::from(schema), value); } } proxies diff --git a/src/redirect.rs b/src/redirect.rs index 4978367..88a8f66 100644 --- a/src/redirect.rs +++ b/src/redirect.rs @@ -149,9 +149,9 @@ impl RedirectPolicy { ) -> Action { self .redirect(RedirectAttempt { - status: status, - next: next, - previous: previous, + status, + next, + previous, }) .inner } diff --git a/src/wait.rs b/src/wait.rs index 794463f..36861a5 100644 --- a/src/wait.rs +++ b/src/wait.rs @@ -20,7 +20,7 @@ pub(crate) fn stream(stream: S, timeout: Option) -> WaitStream where S: Stream { WaitStream { stream: executor::spawn(stream), - timeout: timeout, + timeout, } }