Skip to content

Commit 04321df

Browse files
authored
Merge pull request #40 from tirr-c/fix-travis
Fix broken CI
2 parents f498f59 + 029db97 commit 04321df

File tree

10 files changed

+84
-73
lines changed

10 files changed

+84
-73
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ rust:
33
- nightly
44

55
before_script: |
6-
rustup component add rustfmt-preview &&
7-
cargo install clippy -f
6+
rustup component add rustfmt-preview clippy-preview
87
script: |
98
cargo fmt -- --check &&
10-
cargo clippy -- -D clippy &&
9+
cargo clippy -- -D clippy::all &&
1110
cargo build --verbose &&
1211
cargo test --verbose
1312
cache: cargo

examples/graphql.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ async fn handle_graphql(
5353
let response = request.execute(&Schema::new(Query, Mutation), &ctx);
5454

5555
// `response` has the lifetime of `request`, so we can't use `IntoResponse` directly.
56-
let body_vec = serde_json::to_vec(&response)
57-
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
56+
let body_vec = serde_json::to_vec(&response).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
5857

5958
http::Response::builder()
60-
.status(if response.is_ok() { StatusCode::OK } else { StatusCode::BAD_REQUEST })
59+
.status(if response.is_ok() {
60+
StatusCode::OK
61+
} else {
62+
StatusCode::BAD_REQUEST
63+
})
6164
.header("Content-Type", "application/json")
6265
.body(body::Body::from(body_vec))
6366
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)

examples/named_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(async_await, futures_api)]
22

3-
use tide::head::{NamedComponent, Named};
3+
use tide::head::{Named, NamedComponent};
44

55
struct Number(i32);
66

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2018"

src/app.rs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use futures::{
44
prelude::*,
55
};
66
use hyper::service::Service;
7-
use std::{sync::Arc, ops::{Deref, DerefMut}};
7+
use std::{
8+
ops::{Deref, DerefMut},
9+
sync::Arc,
10+
};
811

912
use crate::{
10-
router::{Resource, Router},
11-
Request,
12-
extract::Extract,
13-
RouteMatch,
14-
Response,
1513
body::Body,
16-
Middleware,
14+
extract::Extract,
15+
router::{Resource, Router},
16+
Middleware, Request, Response, RouteMatch,
1717
};
1818

1919
/// The top-level type for setting up a Tide application.
@@ -65,13 +65,17 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
6565
// TODO: be more robust
6666
let addr = addr.to_socket_addrs().unwrap().next().unwrap();
6767

68-
let server = hyper::Server::bind(&addr).serve(move || {
69-
let res: Result<_, std::io::Error> = Ok(server.clone());
70-
res
71-
}).compat().map(|_| {
72-
let res: Result<(), ()> = Ok(());
73-
res
74-
}).compat();
68+
let server = hyper::Server::bind(&addr)
69+
.serve(move || {
70+
let res: Result<_, std::io::Error> = Ok(server.clone());
71+
res
72+
})
73+
.compat()
74+
.map(|_| {
75+
let res: Result<(), ()> = Ok(());
76+
res
77+
})
78+
.compat();
7579
hyper::rt::run(server);
7680
}
7781
}
@@ -98,26 +102,32 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
98102
let path = req.uri().path().to_owned();
99103
let method = req.method().to_owned();
100104

101-
FutureObj::new(Box::new(async move {
102-
if let Some((endpoint, params)) = router.route(&path, &method) {
103-
for m in middleware.iter() {
104-
match await!(m.request(&mut data, req, &params)) {
105-
Ok(new_req) => req = new_req,
106-
Err(resp) => return Ok(resp.map(Into::into)),
105+
FutureObj::new(Box::new(
106+
async move {
107+
if let Some((endpoint, params)) = router.route(&path, &method) {
108+
for m in middleware.iter() {
109+
match await!(m.request(&mut data, req, &params)) {
110+
Ok(new_req) => req = new_req,
111+
Err(resp) => return Ok(resp.map(Into::into)),
112+
}
107113
}
108-
}
109114

110-
let (head, mut resp) = await!(endpoint.call(data.clone(), req, params));
115+
let (head, mut resp) = await!(endpoint.call(data.clone(), req, params));
111116

112-
for m in middleware.iter() {
113-
resp = await!(m.response(&mut data, &head, resp))
114-
}
117+
for m in middleware.iter() {
118+
resp = await!(m.response(&mut data, &head, resp))
119+
}
115120

116-
Ok(resp.map(Into::into))
117-
} else {
118-
Ok(http::Response::builder().status(http::status::StatusCode::NOT_FOUND).body(hyper::Body::empty()).unwrap())
119-
}
120-
})).compat()
121+
Ok(resp.map(Into::into))
122+
} else {
123+
Ok(http::Response::builder()
124+
.status(http::status::StatusCode::NOT_FOUND)
125+
.body(hyper::Body::empty())
126+
.unwrap())
127+
}
128+
},
129+
))
130+
.compat()
121131
}
122132
}
123133

@@ -140,11 +150,7 @@ impl<T> DerefMut for AppData<T> {
140150

141151
impl<T: Clone + Send + 'static> Extract<T> for AppData<T> {
142152
type Fut = future::Ready<Result<Self, Response>>;
143-
fn extract(
144-
data: &mut T,
145-
req: &mut Request,
146-
params: &RouteMatch<'_>,
147-
) -> Self::Fut {
153+
fn extract(data: &mut T, req: &mut Request, params: &RouteMatch<'_>) -> Self::Fut {
148154
future::ok(AppData(data.clone()))
149155
}
150156
}

src/body.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
//! This module includes types like `Json`, which can be used to automatically (de)serialize bodies
44
//! using `serde_json`.
55
6-
use futures::{compat::Compat01As03, prelude::*, future::FutureObj, stream::StreamObj};
7-
use pin_utils::pin_mut;
6+
use futures::{compat::Compat01As03, future::FutureObj, prelude::*, stream::StreamObj};
87
use http::status::StatusCode;
8+
use pin_utils::pin_mut;
99

10-
use crate::{Extract, IntoResponse, RouteMatch, Request, Response};
10+
use crate::{Extract, IntoResponse, Request, Response, RouteMatch};
1111

1212
/// The raw contents of an http request or response.
1313
///
@@ -57,7 +57,7 @@ impl Body {
5757
///
5858
/// This method is asynchronous because, in general, it requires reading an async
5959
/// stream of `BodyChunk` values.
60-
pub async fn to_vec(&mut self) -> Result<Vec<u8>, Error> {
60+
pub async fn read_to_vec(&mut self) -> Result<Vec<u8>, Error> {
6161
match &mut self.inner {
6262
BodyInner::Streaming(s) => {
6363
let mut bytes = Vec::new();
@@ -122,18 +122,18 @@ impl<T: Send + serde::de::DeserializeOwned + 'static, S: 'static> Extract<S> for
122122
// Note: cannot use `existential type` here due to ICE
123123
type Fut = FutureObj<'static, Result<Self, Response>>;
124124

125-
fn extract(
126-
data: &mut S,
127-
req: &mut Request,
128-
params: &RouteMatch<'_>,
129-
) -> Self::Fut {
125+
fn extract(data: &mut S, req: &mut Request, params: &RouteMatch<'_>) -> Self::Fut {
130126
let mut body = std::mem::replace(req.body_mut(), Body::empty());
131-
FutureObj::new(Box::new(async move {
132-
fn mk_err<T>(_: T) -> Response { StatusCode::BAD_REQUEST.into_response() }
133-
let body = await!(body.to_vec()).map_err(mk_err)?;
134-
let json: T = serde_json::from_slice(&body).map_err(mk_err)?;
135-
Ok(Json(json))
136-
}))
127+
FutureObj::new(Box::new(
128+
async move {
129+
fn mk_err<T>(_: T) -> Response {
130+
StatusCode::BAD_REQUEST.into_response()
131+
}
132+
let body = await!(body.read_to_vec()).map_err(mk_err)?;
133+
let json: T = serde_json::from_slice(&body).map_err(mk_err)?;
134+
Ok(Json(json))
135+
},
136+
))
137137
}
138138
}
139139

src/endpoint.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ pub trait Endpoint<Data, Kind>: Send + Sync + 'static {
1414
fn call(&self, data: Data, req: Request, params: RouteMatch<'_>) -> Self::Fut;
1515
}
1616

17+
type BoxedEndpointFn<Data> =
18+
dyn Fn(Data, Request, RouteMatch) -> FutureObj<'static, (Head, Response)> + Send + Sync;
19+
1720
pub(crate) struct BoxedEndpoint<Data> {
18-
endpoint: Box<
19-
dyn Fn(Data, Request, RouteMatch) -> FutureObj<'static, (Head, Response)> + Send + Sync,
20-
>,
21+
endpoint: Box<BoxedEndpointFn<Data>>,
2122
}
2223

2324
impl<Data> BoxedEndpoint<Data> {

src/head.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ pub trait NamedComponent: Send + 'static + std::str::FromStr {
7575

7676
/// An extractor for named path components
7777
///
78-
/// Allows routes to access named path components (`{foo}`). Each `Named<T>` extracts a single
79-
/// component. `T` must implement the `NamedComponent` trait - to provide the component name - and the
78+
/// Allows routes to access named path components (`{foo}`). Each `Named<T>` extracts a single
79+
/// component. `T` must implement the `NamedComponent` trait - to provide the component name - and the
8080
/// FromStr trait. Fails with a `BAD_REQUEST` response if the component is not found, fails to
8181
/// parse or if multiple identically named components exist.
8282
pub struct Named<T: NamedComponent>(pub T);
@@ -85,10 +85,13 @@ impl<T: NamedComponent, S: 'static> Extract<S> for Named<T> {
8585
type Fut = future::Ready<Result<Self, Response>>;
8686

8787
fn extract(data: &mut S, req: &mut Request, params: &RouteMatch<'_>) -> Self::Fut {
88-
params.map.get(T::NAME)
88+
params
89+
.map
90+
.get(T::NAME)
8991
.and_then(|component| component.parse().ok())
90-
.map_or(future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
91-
|t| future::ok(Named(t)))
92+
.map_or(
93+
future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
94+
|t| future::ok(Named(t)),
95+
)
9296
}
9397
}
94-

src/router.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ impl<Data> Default for Resource<Data> {
5050
impl<Data> Resource<Data> {
5151
/// Add an endpoint for the given HTTP method
5252
pub fn method<T: Endpoint<Data, U>, U>(&mut self, method: http::Method, ep: T) {
53-
let old_ep = self
54-
.endpoints
55-
.insert(method.clone(), BoxedEndpoint::new(ep));
56-
57-
if old_ep.is_some() {
53+
if self.endpoints.contains_key(&method) {
5854
panic!("A {} endpoint already exists for this path", method)
5955
}
56+
57+
self.endpoints.insert(method, BoxedEndpoint::new(ep));
6058
}
6159

6260
/// Add an endpoint for `GET` requests

src/url_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<R: Default> UrlTable<R> {
102102
}
103103

104104
match table.wildcard_mut().unwrap() {
105-
Wildcard { name: n, .. } if &name != n => {
105+
Wildcard { name: n, .. } if name != n => {
106106
panic!("Route {} segment `{{{}}}` conflicts with existing wildcard segment `{{{}}}`", path, name, n);
107107
}
108108
Wildcard { table: t, .. } => {
@@ -113,7 +113,7 @@ impl<R: Default> UrlTable<R> {
113113
table = table
114114
.next
115115
.entry(segment.to_string())
116-
.or_insert(UrlTable::new());
116+
.or_insert_with(UrlTable::new);
117117
}
118118
}
119119

0 commit comments

Comments
 (0)