From 5ba6b3cd4d7fcd8061275ecd4e15ae297ac853d2 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Tue, 18 Dec 2018 13:14:29 +0100 Subject: [PATCH] edition 2018, clippy, fmt --- Cargo.toml | 1 + src/agent.rs | 56 ++++++++++++++-------------------------- src/body.rs | 4 +-- src/header.rs | 10 +++---- src/lib.rs | 39 +++++++++++----------------- src/pool.rs | 10 +++---- src/response.rs | 42 ++++++++++++++++-------------- src/stream.rs | 13 +++++----- src/test/agent_test.rs | 2 +- src/test/auth.rs | 2 +- src/test/body_read.rs | 2 +- src/test/body_send.rs | 2 +- src/test/mod.rs | 6 ++--- src/test/query_string.rs | 2 +- src/test/range.rs | 4 +-- src/test/simple.rs | 2 +- src/unit.rs | 16 ++++++------ 17 files changed, 97 insertions(+), 116 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e3cdb68..8ee953e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ repository = "https://github.com/algesten/ureq" readme = "README.md" keywords = ["web", "request", "http", "rest", "client"] categories = ["web-programming::http-client"] +edition = "2018" [features] default = ["tls"] diff --git a/src/agent.rs b/src/agent.rs index d4b220d..b33c8f8 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -1,10 +1,10 @@ +use crate::error::Error; +use crate::pool::ConnectionPool; +use crate::response::{self, Response}; use cookie::{Cookie, CookieJar}; -use error::Error; -use pool::ConnectionPool; -use response::{self, Response}; use std::sync::Mutex; -use header::{add_header, get_all_headers, get_header, has_header, Header}; +use crate::header::{add_header, get_all_headers, get_header, has_header, Header}; // to get to share private fields include!("request.rs"); @@ -112,12 +112,8 @@ impl Agent { /// println!("Oh no error!"); /// } /// ``` - pub fn set(&mut self, header: &str, value: &str) -> &mut Agent - { - add_header( - &mut self.headers, - Header::new(header, value), - ); + pub fn set(&mut self, header: &str, value: &str) -> &mut Agent { + add_header(&mut self.headers, Header::new(header, value)); self } @@ -133,8 +129,7 @@ impl Agent { /// .call(); /// println!("{:?}", r); /// ``` - pub fn auth(&mut self, user: &str, pass: &str) -> &mut Agent - { + pub fn auth(&mut self, user: &str, pass: &str) -> &mut Agent { let pass = basic_auth(user, pass); self.auth_kind("Basic", &pass) } @@ -152,8 +147,7 @@ impl Agent { /// .get("/my_page") /// .call(); /// ``` - pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Agent - { + pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Agent { let value = format!("{} {}", kind, pass); self.set("Authorization", &value); self @@ -169,8 +163,7 @@ impl Agent { /// .call(); /// println!("{:?}", r); /// ``` - pub fn request(&self, method: &str, path: &str) -> Request - { + pub fn request(&self, method: &str, path: &str) -> Request { Request::new(&self, method.into(), path.into()) } @@ -190,7 +183,7 @@ impl Agent { state .as_ref() .and_then(|state| state.jar.get(name)) - .map(|c| c.clone()) + .cloned() } /// Set a cookie in this agent. @@ -212,56 +205,47 @@ impl Agent { } /// Make a GET request from this agent. - pub fn get(&self, path: &str) -> Request - { + pub fn get(&self, path: &str) -> Request { self.request("GET", path) } /// Make a HEAD request from this agent. - pub fn head(&self, path: &str) -> Request - { + pub fn head(&self, path: &str) -> Request { self.request("HEAD", path) } /// Make a POST request from this agent. - pub fn post(&self, path: &str) -> Request - { + pub fn post(&self, path: &str) -> Request { self.request("POST", path) } /// Make a PUT request from this agent. - pub fn put(&self, path: &str) -> Request - { + pub fn put(&self, path: &str) -> Request { self.request("PUT", path) } /// Make a DELETE request from this agent. - pub fn delete(&self, path: &str) -> Request - { + pub fn delete(&self, path: &str) -> Request { self.request("DELETE", path) } /// Make a TRACE request from this agent. - pub fn trace(&self, path: &str) -> Request - { + pub fn trace(&self, path: &str) -> Request { self.request("TRACE", path) } /// Make a OPTIONS request from this agent. - pub fn options(&self, path: &str) -> Request - { + pub fn options(&self, path: &str) -> Request { self.request("OPTIONS", path) } /// Make a CONNECT request from this agent. - pub fn connect(&self, path: &str) -> Request - { + pub fn connect(&self, path: &str) -> Request { self.request("CONNECT", path) } /// Make a PATCH request from this agent. - pub fn patch(&self, path: &str) -> Request - { + pub fn patch(&self, path: &str) -> Request { self.request("PATCH", path) } @@ -272,7 +256,7 @@ impl Agent { } fn basic_auth(user: &str, pass: &str) -> String { - let safe = match user.find(":") { + let safe = match user.find(':') { Some(idx) => &user[..idx], None => user, }; diff --git a/src/body.rs b/src/body.rs index 9f6d634..043282e 100644 --- a/src/body.rs +++ b/src/body.rs @@ -1,13 +1,13 @@ +use crate::stream::Stream; use chunked_transfer; use std::io::{copy, empty, Cursor, Read, Result as IoResult}; -use stream::Stream; #[cfg(feature = "charset")] use encoding::label::encoding_from_whatwg_label; #[cfg(feature = "charset")] use encoding::EncoderTrap; #[cfg(feature = "charset")] -use response::DEFAULT_CHARACTER_SET; +use crate::response::DEFAULT_CHARACTER_SET; #[cfg(feature = "json")] use super::SerdeValue; diff --git a/src/header.rs b/src/header.rs index ee5b8ee..9ebdf88 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,5 +1,5 @@ +use crate::error::Error; use ascii::{AsAsciiStr, AsciiString}; -use error::Error; use std::str::FromStr; #[derive(Clone)] @@ -60,11 +60,11 @@ impl Header { } } -pub fn get_header<'a, 'b>(headers: &'b Vec
, name: &'a str) -> Option<&'b str> { +pub fn get_header<'a, 'b>(headers: &'b [Header], name: &'a str) -> Option<&'b str> { headers.iter().find(|h| h.is_name(name)).map(|h| h.value()) } -pub fn get_all_headers<'a, 'b>(headers: &'b Vec
, name: &'a str) -> Vec<&'b str> { +pub fn get_all_headers<'a, 'b>(headers: &'b [Header], name: &'a str) -> Vec<&'b str> { headers .iter() .filter(|h| h.is_name(name)) @@ -72,7 +72,7 @@ pub fn get_all_headers<'a, 'b>(headers: &'b Vec
, name: &'a str) -> Vec<& .collect() } -pub fn has_header(headers: &Vec
, name: &str) -> bool { +pub fn has_header(headers: &[Header], name: &str) -> bool { get_header(headers, name).is_some() } @@ -89,7 +89,7 @@ impl FromStr for Header { fn from_str(s: &str) -> Result { // let line = AsciiString::from_str(s).map_err(|_| Error::BadHeader)?; - let index = s.find(":").ok_or_else(|| Error::BadHeader)?; + let index = s.find(':').ok_or_else(|| Error::BadHeader)?; // no value? if index >= s.len() { diff --git a/src/lib.rs b/src/lib.rs index 4f03f16..52d5531 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![warn(clippy::all)] //! ureq is a minimal request library. //! //! The goals of this library are: @@ -120,10 +121,10 @@ mod serde_macros; #[cfg(test)] mod test; -pub use agent::{Agent, Request}; -pub use error::Error; -pub use header::Header; -pub use response::Response; +pub use crate::agent::{Agent, Request}; +pub use crate::error::Error; +pub use crate::header::Header; +pub use crate::response::Response; // re-export pub use cookie::Cookie; @@ -140,62 +141,52 @@ pub fn agent() -> Agent { /// ``` /// ureq::request("GET", "https://www.google.com").call(); /// ``` -pub fn request(method: &str, path: &str) -> Request -{ +pub fn request(method: &str, path: &str) -> Request { Agent::new().request(method, path) } /// Make a GET request. -pub fn get(path: &str) -> Request -{ +pub fn get(path: &str) -> Request { request("GET", path) } /// Make a HEAD request. -pub fn head(path: &str) -> Request -{ +pub fn head(path: &str) -> Request { request("HEAD", path) } /// Make a POST request. -pub fn post(path: &str) -> Request -{ +pub fn post(path: &str) -> Request { request("POST", path) } /// Make a PUT request. -pub fn put(path: &str) -> Request -{ +pub fn put(path: &str) -> Request { request("PUT", path) } /// Make a DELETE request. -pub fn delete(path: &str) -> Request -{ +pub fn delete(path: &str) -> Request { request("DELETE", path) } /// Make a TRACE request. -pub fn trace(path: &str) -> Request -{ +pub fn trace(path: &str) -> Request { request("TRACE", path) } /// Make an OPTIONS request. -pub fn options(path: &str) -> Request -{ +pub fn options(path: &str) -> Request { request("OPTIONS", path) } /// Make an CONNECT request. -pub fn connect(path: &str) -> Request -{ +pub fn connect(path: &str) -> Request { request("CONNECT", path) } /// Make an PATCH request. -pub fn patch(path: &str) -> Request -{ +pub fn patch(path: &str) -> Request { request("PATCH", path) } diff --git a/src/pool.rs b/src/pool.rs index 5b2999f..18e50f6 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -1,10 +1,10 @@ -use agent::Unit; +use crate::agent::Unit; +use crate::stream::Stream; use std::collections::HashMap; use std::io::{Read, Result as IoResult}; -use stream::Stream; use url::Url; -pub const DEFAULT_HOST: &'static str = "localhost"; +pub const DEFAULT_HOST: &str = "localhost"; /// Holder of recycled connections. /// @@ -106,7 +106,7 @@ impl PoolReturnRead { fn do_read(&mut self, buf: &mut [u8]) -> IoResult { match self.reader.as_mut() { - None => return Ok(0), + None => Ok(0), Some(reader) => reader.read(buf), } } @@ -128,4 +128,4 @@ impl Drop for PoolReturnRead { fn drop(&mut self) { self.return_connection(); } -} \ No newline at end of file +} diff --git a/src/response.rs b/src/response.rs index 52173d7..5cc4f50 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,11 +1,11 @@ -use agent::Unit; +use crate::agent::Unit; +use crate::header::Header; +use crate::pool::PoolReturnRead; +use crate::stream::Stream; use ascii::AsciiString; use chunked_transfer::Decoder as ChunkDecoder; -use header::Header; -use pool::PoolReturnRead; use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult}; use std::str::FromStr; -use stream::Stream; #[cfg(feature = "json")] use serde_json; @@ -15,10 +15,10 @@ use encoding::label::encoding_from_whatwg_label; #[cfg(feature = "charset")] use encoding::DecoderTrap; -use error::Error; +use crate::error::Error; -pub const DEFAULT_CONTENT_TYPE: &'static str = "text/plain"; -pub const DEFAULT_CHARACTER_SET: &'static str = "utf-8"; +pub const DEFAULT_CONTENT_TYPE: &str = "text/plain"; +pub const DEFAULT_CHARACTER_SET: &str = "utf-8"; /// Response instances are created as results of firing off requests. /// @@ -200,7 +200,7 @@ impl Response { self.header("content-type") .map(|header| { header - .find(";") + .find(';') .map(|index| &header[0..index]) .unwrap_or(header) }) @@ -258,8 +258,9 @@ impl Response { let is_head = (&self.unit).as_ref().map(|u| u.is_head).unwrap_or(false); - let is_chunked = self.header("transfer-encoding") - .map(|enc| enc.len() > 0) // whatever it says, do chunked + let is_chunked = self + .header("transfer-encoding") + .map(|enc| !enc.is_empty()) // whatever it says, do chunked .unwrap_or(false); let use_chunked = !is_http10 && !is_head && is_chunked; @@ -276,7 +277,10 @@ impl Response { let stream = Box::new(self.stream.expect("No reader in response?!")); let stream_ptr = Box::into_raw(stream); - let mut yolo = YoloRead { stream: stream_ptr, dealloc: false }; + let mut yolo = YoloRead { + stream: stream_ptr, + dealloc: false, + }; let unit = self.unit; match (use_chunked, limit_bytes) { @@ -293,7 +297,7 @@ impl Response { (false, None) => { yolo.dealloc = true; // dealloc when read drops. Box::new(yolo) - }, + } } } @@ -398,7 +402,7 @@ impl Response { let mut headers: Vec
= Vec::new(); loop { let line = read_next_line(&mut reader).map_err(|_| Error::BadHeader)?; - if line.len() == 0 { + if line.is_empty() { break; } if let Ok(header) = line.as_str().parse::
() { @@ -444,7 +448,7 @@ fn parse_status_line(line: &str) -> Result<((usize, usize), u16), Error> { let status = status.parse::().map_err(|_| Error::BadStatus)?; let status_text = split.next().ok_or_else(|| Error::BadStatus)?; - if status_text.len() == 0 { + if status_text.is_empty() { return Err(Error::BadStatus); } @@ -503,7 +507,7 @@ fn read_next_line(reader: &mut R) -> IoResult { let byte = reader.bytes().next(); let byte = match byte { - Some(b) => try!(b), + Some(b) => r#try!(b), None => return Err(IoError::new(ErrorKind::ConnectionAborted, "Unexpected EOF")), }; @@ -577,7 +581,7 @@ impl LimitedRead { impl Read for LimitedRead { fn read(&mut self, buf: &mut [u8]) -> IoResult { let left = self.limit - self.position; - if left <= 0 { + if left == 0 { return Ok(0); } let from = if left < buf.len() { @@ -603,9 +607,9 @@ impl Read for LimitedRead { pub fn charset_from_content_type(header: Option<&str>) -> &str { header .and_then(|header| { - header.find(";").and_then(|semi| { + header.find(';').and_then(|semi| { (&header[semi + 1..]) - .find("=") + .find('=') .map(|equal| (&header[semi + equal + 2..]).trim()) }) }) @@ -696,7 +700,7 @@ mod tests { #[test] fn parse_borked_header() { - let s = format!("HTTP/1.1 BORKED\r\n"); + let s = "HTTP/1.1 BORKED\r\n".to_string(); let resp: Response = s.parse::().unwrap_err().into(); assert_eq!(resp.http_version(), "HTTP/1.1"); assert_eq!(resp.status(), 500); diff --git a/src/stream.rs b/src/stream.rs index b316ed5..7c654dd 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -1,5 +1,5 @@ -use agent::Unit; -use error::Error; +use crate::agent::Unit; +use crate::error::Error; use std::io::{Cursor, Read, Result as IoResult, Write}; use std::net::SocketAddr; use std::net::TcpStream; @@ -95,7 +95,7 @@ pub fn connect_http(unit: &Unit) -> Result { let hostname = unit.url.host_str().unwrap(); let port = unit.url.port().unwrap_or(80); - connect_host(unit, hostname, port).map(|tcp| Stream::Http(tcp)) + connect_host(unit, hostname, port).map(Stream::Http) } #[cfg(feature = "tls")] @@ -119,7 +119,7 @@ pub fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result Result 0 { @@ -152,7 +153,7 @@ pub fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result Result { - use test; + use crate::test; test::resolve_handler(unit) } diff --git a/src/test/agent_test.rs b/src/test/agent_test.rs index 0a03407..405f7c3 100644 --- a/src/test/agent_test.rs +++ b/src/test/agent_test.rs @@ -1,4 +1,4 @@ -use test; +use crate::test; use super::super::*; diff --git a/src/test/auth.rs b/src/test/auth.rs index 9d45a29..76fee2b 100644 --- a/src/test/auth.rs +++ b/src/test/auth.rs @@ -1,4 +1,4 @@ -use test; +use crate::test; use super::super::*; diff --git a/src/test/body_read.rs b/src/test/body_read.rs index fae8a8c..2a40fe0 100644 --- a/src/test/body_read.rs +++ b/src/test/body_read.rs @@ -1,5 +1,5 @@ +use crate::test; use std::io::Read; -use test; use super::super::*; diff --git a/src/test/body_send.rs b/src/test/body_send.rs index 1c858ac..1cea6e8 100644 --- a/src/test/body_send.rs +++ b/src/test/body_send.rs @@ -1,4 +1,4 @@ -use test; +use crate::test; use super::super::*; diff --git a/src/test/mod.rs b/src/test/mod.rs index 1d36aa7..c1709a6 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1,9 +1,9 @@ -use agent::Unit; -use error::Error; +use crate::agent::Unit; +use crate::error::Error; +use crate::stream::Stream; use std::collections::HashMap; use std::io::{Cursor, Write}; use std::sync::{Arc, Mutex}; -use stream::Stream; mod agent_test; mod auth; diff --git a/src/test/query_string.rs b/src/test/query_string.rs index a0f9dbb..1dc9791 100644 --- a/src/test/query_string.rs +++ b/src/test/query_string.rs @@ -1,4 +1,4 @@ -use test; +use crate::test; use super::super::*; diff --git a/src/test/range.rs b/src/test/range.rs index 98170bf..fdffc42 100644 --- a/src/test/range.rs +++ b/src/test/range.rs @@ -39,8 +39,8 @@ fn agent_pool() { let state = lock.as_mut().unwrap(); let pool = state.pool(); assert_eq!(pool.len(), 1); - let foo = format!("{:?}", pool.get("s3.amazonaws.com", 443)); - assert_eq!(foo, "Some(Stream[https])"); // not a great way of testing. + let f = format!("{:?}", pool.get("s3.amazonaws.com", 443)); + assert_eq!(f, "Some(Stream[https])"); // not a great way of testing. } // req 2 should be done with a reused connection diff --git a/src/test/simple.rs b/src/test/simple.rs index 2c23d81..000f0f3 100644 --- a/src/test/simple.rs +++ b/src/test/simple.rs @@ -1,5 +1,5 @@ +use crate::test; use std::io::Read; -use test; use super::super::*; diff --git a/src/unit.rs b/src/unit.rs index eda60d1..d3ad240 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -1,11 +1,11 @@ use base64; -use body::{send_body, Payload, SizedReader}; +use crate::body::{send_body, Payload, SizedReader}; use std::io::{Result as IoResult, Write}; -use stream::{connect_http, connect_https, connect_test, Stream}; +use crate::stream::{connect_http, connect_https, connect_test, Stream}; use url::Url; // -use pool::DEFAULT_HOST; +use crate::pool::DEFAULT_HOST; /// It's a "unit of work". Maybe a bad name for it? /// @@ -31,7 +31,7 @@ impl Unit { let is_chunked = req.header("transfer-encoding") // if the user has set an encoding header, obey that. - .map(|enc| enc.len() > 0) + .map(|enc| !enc.is_empty()) // otherwise, no chunking. .unwrap_or(false); @@ -44,7 +44,7 @@ impl Unit { let query_string = combine_query(&url, &req.query, mix_queries); let cookie_headers: Vec<_> = { - let mut state = req.agent.lock().unwrap(); + let state = req.agent.lock().unwrap(); match state.as_ref().map(|state| &state.jar) { None => vec![], Some(jar) => match_cookies(jar, &hostname, url.path(), is_secure), @@ -210,7 +210,7 @@ fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bo /// Combine the query of the url and the query options set on the request object. fn combine_query(url: &Url, query: &QString, mix_queries: bool) -> String { - match (url.query(), query.len() > 0 && mix_queries) { + match (url.query(), !query.is_empty() && mix_queries) { (Some(urlq), true) => format!("?{}&{}", urlq, query), (Some(urlq), false) => format!("?{}", urlq), (None, true) => format!("?{}", query), @@ -273,7 +273,7 @@ fn send_prelude(unit: &Unit, method: &str, stream: &mut Stream) -> IoResult<()> write!(prelude, "\r\n")?; // write all to the wire - stream.write_all(&mut prelude[..])?; + stream.write_all(&prelude[..])?; Ok(()) } @@ -299,7 +299,7 @@ fn save_cookies(unit: &Unit, resp: &Response) { }; match Cookie::parse_encoded(&to_parse[..]) { Err(_) => (), // ignore unparseable cookies - Ok(mut cookie) => { + Ok(cookie) => { let cookie = cookie.into_owned(); add_jar.add(cookie) }