Skip to content

Commit

Permalink
Apply rustfmt to entire repo (#277)
Browse files Browse the repository at this point in the history
Apply newly added rustfmt configuration from #276 to the entire repo. This will be the start of a clean slate going forward for consistent formatting.

In addition, this PR is to test semi-automated formatting with the `@teto-bot rustfmt` command, which can be invoked from any PR by the PR author or by an Isahc maintainer. Indeed, the formatting proposed by this PR was all automated.
  • Loading branch information
sagebind authored Dec 17, 2020
1 parent b789398 commit dd3745b
Show file tree
Hide file tree
Showing 45 changed files with 290 additions and 250 deletions.
6 changes: 2 additions & 4 deletions benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use rouille::{Request, Response};
use std::net::SocketAddr;
use std::sync::Arc;
use std::thread;
use std::{net::SocketAddr, sync::Arc, thread};

pub struct TestServer {
addr: SocketAddr,
Expand Down Expand Up @@ -36,7 +34,7 @@ impl TestServer {
});

Self {
addr: addr,
addr,
counter: Some(counter_outer),
handle: Some(handle),
}
Expand Down
5 changes: 1 addition & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::env;
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use std::{env, error::Error, fs, path::PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
Expand Down
3 changes: 1 addition & 2 deletions examples/badssl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! This example contains a number of manual tests against badssl.com
//! demonstrating several dangerous SSL/TLS options.
use isahc::config::SslOption;
use isahc::prelude::*;
use isahc::{config::SslOption, prelude::*};

fn main() {
// accept expired cert
Expand Down
5 changes: 1 addition & 4 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Another simple example that creates a custom HTTP client instance and sends
//! a GET request with it instead of using the default client.
use isahc::{
config::RedirectPolicy,
prelude::*,
};
use isahc::{config::RedirectPolicy, prelude::*};
use std::{
io::{copy, stdout},
time::Duration,
Expand Down
3 changes: 1 addition & 2 deletions examples/cookies.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! A simple example program that sends a GET request and then prints out all
//! the cookies in the cookie jar.
use isahc::cookies::CookieJar;
use isahc::prelude::*;
use isahc::{cookies::CookieJar, prelude::*};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
Expand Down
5 changes: 1 addition & 4 deletions examples/http2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! This example simply demonstrates HTTP/2 support by making a request that
//! enforces usage of HTTP/2.
use isahc::{
config::VersionNegotiation,
prelude::*,
};
use isahc::{config::VersionNegotiation, prelude::*};

fn main() -> Result<(), isahc::Error> {
let response = Request::get("https://nghttp2.org")
Expand Down
3 changes: 1 addition & 2 deletions examples/parallel_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use isahc::prelude::*;
use rayon::prelude::*;
use std::env;
use std::time::Instant;
use std::{env, time::Instant};

fn main() -> Result<(), isahc::Error> {
let count = env::args()
Expand Down
6 changes: 4 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Types for working with HTTP authentication methods.
use crate::config::{internal::SetOpt, proxy::Proxy};
use std::fmt;
use std::ops::{BitOr, BitOrAssign};
use std::{
fmt,
ops::{BitOr, BitOrAssign},
};

/// Credentials consisting of a username and a secret (password) that can be
/// used to establish user identity.
Expand Down
7 changes: 5 additions & 2 deletions src/body/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Body {
#[inline]
pub fn from_bytes_static<B>(bytes: B) -> Self
where
B: AsRef<[u8]> + 'static
B: AsRef<[u8]> + 'static,
{
match_type! {
<bytes as Cursor<Cow<'static, [u8]>>> => Self(Inner::Buffer(bytes)),
Expand Down Expand Up @@ -166,7 +166,10 @@ impl Body {
} else {
AsyncBody::from_reader(pipe_reader)
},
Some(Writer { reader, writer }),
Some(Writer {
reader,
writer,
}),
)
}
}
Expand Down
68 changes: 38 additions & 30 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::{
agent::{self, AgentBuilder},
auth::{Authentication, Credentials},
body::{AsyncBody, Body},
config::internal::{ConfigurableBase, SetOpt},
config::*,
config::{
internal::{ConfigurableBase, SetOpt},
*,
},
default_headers::DefaultHeadersInterceptor,
error::{Error, ErrorKind},
handler::{RequestHandler, ResponseBodyReader},
Expand All @@ -19,7 +21,8 @@ use futures_lite::{
};
use http::{
header::{HeaderMap, HeaderName, HeaderValue},
Request, Response,
Request,
Response,
};
use once_cell::sync::Lazy;
use std::{
Expand Down Expand Up @@ -920,27 +923,28 @@ impl HttpClient {
async_body
});

let response = block_on(async move {
// Instead of simply blocking the current thread until the response
// is received, we can use the current thread to read from the
// request body synchronously while concurrently waiting for the
// response.
if let Some(mut writer) = writer_maybe {
// Note that the `send_async` future is given first; this
// ensures that it is polled first and thus the request is
// initiated before we attempt to write the request body.
let (response, _) = try_zip(
self.send_async_inner(request),
async move {
let response = block_on(
async move {
// Instead of simply blocking the current thread until the response
// is received, we can use the current thread to read from the
// request body synchronously while concurrently waiting for the
// response.
if let Some(mut writer) = writer_maybe {
// Note that the `send_async` future is given first; this
// ensures that it is polled first and thus the request is
// initiated before we attempt to write the request body.
let (response, _) = try_zip(self.send_async_inner(request), async move {
writer.write().await.map_err(Error::from)
},
).await?;
})
.await?;

Ok(response)
} else {
self.send_async_inner(request).await
Ok(response)
} else {
self.send_async_inner(request).await
}
}
}.instrument(span))?;
.instrument(span),
)?;

Ok(response.map(|body| body.into_sync()))
}
Expand Down Expand Up @@ -979,11 +983,17 @@ impl HttpClient {
uri = ?request.uri(),
);

ResponseFuture::new(self.send_async_inner(request.map(Into::into)).instrument(span))
ResponseFuture::new(
self.send_async_inner(request.map(Into::into))
.instrument(span),
)
}

/// Actually send the request. All the public methods go through here.
async fn send_async_inner(&self, mut request: Request<AsyncBody>) -> Result<Response<AsyncBody>, Error> {
async fn send_async_inner(
&self,
mut request: Request<AsyncBody>,
) -> Result<Response<AsyncBody>, Error> {
// Set redirect policy if not specified.
if request.extensions().get::<RedirectPolicy>().is_none() {
if let Some(policy) = self.inner.defaults.get::<RedirectPolicy>().cloned() {
Expand Down Expand Up @@ -1161,7 +1171,9 @@ impl crate::interceptor::Invoke for &HttpClient {

// Check if automatic decompression is enabled; we'll need to know
// this later after the response is sent.
let is_automatic_decompression = request.extensions().get()
let is_automatic_decompression = request
.extensions()
.get()
.or_else(|| self.inner.defaults.get())
.map(|AutomaticDecompression(enabled)| *enabled)
.unwrap_or(false);
Expand Down Expand Up @@ -1222,9 +1234,7 @@ impl fmt::Debug for HttpClient {

/// A future for a request being executed.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ResponseFuture<'c>(
Pin<Box<dyn Future<Output = <Self as Future>::Output> + 'c + Send>>,
);
pub struct ResponseFuture<'c>(Pin<Box<dyn Future<Output = <Self as Future>::Output> + 'c + Send>>);

impl<'c> ResponseFuture<'c> {
fn new<F>(future: F) -> Self
Expand All @@ -1235,9 +1245,7 @@ impl<'c> ResponseFuture<'c> {
}

fn error(error: Error) -> Self {
Self::new(async move {
Err(error)
})
Self::new(async move { Err(error) })
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/config/dial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
use super::SetOpt;
use curl::easy::{Easy2, List};
use http::Uri;
use std::{
convert::TryFrom,
fmt,
net::SocketAddr,
str::FromStr,
};
use std::{convert::TryFrom, fmt, net::SocketAddr, str::FromStr};

/// An error which can be returned when parsing a dial address.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
5 changes: 1 addition & 4 deletions src/config/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
use super::SetOpt;
use curl::easy::Easy2;
use std::{
net::IpAddr,
time::Duration,
};
use std::{net::IpAddr, time::Duration};

/// DNS caching configuration.
///
Expand Down
10 changes: 4 additions & 6 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
use self::internal::SetOpt;
use crate::auth::{Authentication, Credentials};
use curl::easy::Easy2;
use std::{
iter::FromIterator,
net::IpAddr,
time::Duration,
};
use std::{iter::FromIterator, net::IpAddr, time::Duration};

pub(crate) mod dial;
pub(crate) mod dns;
Expand Down Expand Up @@ -694,7 +690,9 @@ impl NetworkInterface {
/// Bind to whatever the networking stack finds suitable. This is the
/// default behavior.
pub fn any() -> Self {
Self { interface: None }
Self {
interface: None,
}
}

/// Bind to the interface with the given name (such as `eth0`). This method
Expand Down
16 changes: 5 additions & 11 deletions src/cookies/cookie.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use chrono::{prelude::*, Duration};
use std::{
error::Error,
fmt,
str,
};
use std::{error::Error, fmt, str};

/// An error which can occur when attempting to parse a cookie string.
#[derive(Debug)]
Expand Down Expand Up @@ -246,12 +242,10 @@ fn parse_cookie_value(mut bytes: &[u8]) -> Result<&str, ParseError> {

// https://tools.ietf.org/html/rfc6265#section-4.1.1
fn is_valid_cookie_value(bytes: &[u8]) -> bool {
bytes
.iter()
.all(|&byte| match byte {
0x21 | 0x23..=0x2B | 0x2D..=0x3A | 0x3C..=0x5B | 0x5D..=0x7E => true,
_ => false,
})
bytes.iter().all(|&byte| match byte {
0x21 | 0x23..=0x2B | 0x2D..=0x3A | 0x3C..=0x5B | 0x5D..=0x7E => true,
_ => false,
})
}

// https://tools.ietf.org/html/rfc2616#section-2.2
Expand Down
26 changes: 17 additions & 9 deletions src/cookies/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ impl CookieInterceptor {
impl Interceptor for CookieInterceptor {
type Err = Error;

fn intercept<'a>(&'a self, mut request: Request<AsyncBody>, ctx: Context<'a>) -> InterceptorFuture<'a, Self::Err> {
fn intercept<'a>(
&'a self,
mut request: Request<AsyncBody>,
ctx: Context<'a>,
) -> InterceptorFuture<'a, Self::Err> {
Box::pin(async move {
// Determine the cookie jar to use for this request. If one is
// attached to this specific request, use it, otherwise use the
// default one.
let jar = request.extensions().get::<CookieJar>()
let jar = request
.extensions()
.get::<CookieJar>()
.cloned()
.or_else(|| self.cookie_jar.clone());

if let Some(jar) = jar.as_ref() {
// Get the outgoing cookie header.
let mut cookie_string = request.headers_mut()
let mut cookie_string = request
.headers_mut()
.remove(http::header::COOKIE)
.map(|value| value.as_bytes().to_vec())
.unwrap_or_default();
Expand Down Expand Up @@ -70,8 +77,7 @@ impl Interceptor for CookieInterceptor {
if let Some(jar) = jar {
// Persist cookies returned from the server, if any.
if response.headers().contains_key(http::header::SET_COOKIE) {
let request_uri = response.effective_uri()
.unwrap_or(&request_uri);
let request_uri = response.effective_uri().unwrap_or(&request_uri);

let cookies = response
.headers()
Expand All @@ -83,10 +89,12 @@ impl Interceptor for CookieInterceptor {
None
})
})
.filter_map(|header| Cookie::parse(header).ok().or_else(|| {
tracing::warn!("could not parse Set-Cookie header");
None
}));
.filter_map(|header| {
Cookie::parse(header).ok().or_else(|| {
tracing::warn!("could not parse Set-Cookie header");
None
})
});

for cookie in cookies {
jar.set(cookie, request_uri);
Expand Down
3 changes: 2 additions & 1 deletion src/cookies/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ impl CookieJar {
pub fn get_for_uri(&self, uri: &Uri) -> impl IntoIterator<Item = Cookie> {
let jar = self.cookies.read().unwrap();

let mut cookies = jar.iter()
let mut cookies = jar
.iter()
.filter(|cookie| cookie.matches(uri))
.map(|c| c.cookie.clone())
.collect::<Vec<_>>();
Expand Down
7 changes: 2 additions & 5 deletions src/cookies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
//! feature is enabled.
mod cookie;
mod jar;
pub(crate) mod interceptor;
mod jar;

#[cfg(feature = "psl")]
mod psl;

pub use self::{
cookie::Cookie,
jar::CookieJar,
};
pub use self::{cookie::Cookie, jar::CookieJar};
Loading

0 comments on commit dd3745b

Please sign in to comment.