Skip to content

Commit

Permalink
Merge pull request #35 from cramertj/strip-whitespace
Browse files Browse the repository at this point in the history
Strip whitespace
  • Loading branch information
aturon authored Nov 9, 2018
2 parents fdcb177 + ae6e6fa commit 294c311
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
22 changes: 11 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::{
router::{Resource, Router},
Request,
extract::Extract,
RouteMatch,
Response,
RouteMatch,
Response,
body::Body,
Middleware,
};

/// The top-level type for setting up a Tide application.
///
///
/// Apps are equipped with a handle to their own state (`Data`), which is available to all endpoints.
/// This is a "handle" because it must be `Clone`, and endpoints are invoked with a fresh clone.
/// They also hold a top-level router.
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
}

/// Start serving the app at the given address.
///
///
/// Blocks the calling thread indefinitely.
pub fn serve<A: std::net::ToSocketAddrs>(self, addr: A) {
let server: Server<Data> = self.into_server();
Expand All @@ -66,7 +66,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
let addr = addr.to_socket_addrs().unwrap().next().unwrap();

let server = hyper::Server::bind(&addr).serve(move || {
let res: Result<_, std::io::Error> = Ok(server.clone());
let res: Result<_, std::io::Error> = Ok(server.clone());
res
}).compat().map(|_| {
let res: Result<(), ()> = Ok(());
Expand Down Expand Up @@ -94,17 +94,17 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
let router = self.router.clone();
let middleware = self.middleware.clone();

let mut req = req.map(Body::from);
let mut req = req.map(Body::from);
let path = req.uri().path().to_owned();
let method = req.method().to_owned();

FutureObj::new(Box::new(async move {
if let Some((endpoint, params)) = router.route(&path, &method) {
FutureObj::new(Box::new(async move {
if let Some((endpoint, params)) = router.route(&path, &method) {
for m in middleware.iter() {
match await!(m.request(&mut data, req, &params)) {
Ok(new_req) => req = new_req,
Err(resp) => return Ok(resp.map(Into::into)),
}
}
}

let (head, mut resp) = await!(endpoint.call(data.clone(), req, params));
Expand All @@ -122,7 +122,7 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
}

/// An extractor for accessing app data.
///
///
/// Endpoints can use `AppData<T>` to gain a handle to the data (of type `T`) originally injected into their app.
pub struct AppData<T>(pub T);

Expand All @@ -147,4 +147,4 @@ impl<T: Clone + Send + 'static> Extract<T> for AppData<T> {
) -> Self::Fut {
future::ok(AppData(data.clone()))
}
}
}
18 changes: 9 additions & 9 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Types for working directly with the bodies of requests and responses.
//!
//!
//! This module includes types like `Json`, which can be used to automatically (de)serialize bodies
//! using `serde_json`.
Expand All @@ -10,9 +10,9 @@ use http::status::StatusCode;
use crate::{Extract, IntoResponse, RouteMatch, Request, Response};

/// The raw contents of an http request or response.
///
///
/// A body is a stream of `BodyChunk`s, which are essentially `Vec<u8>` values.
/// Both `Body` and `BodyChunk` values can be easily created from standard byte buffer types,
/// Both `Body` and `BodyChunk` values can be easily created from standard byte buffer types,
/// using the `From` trait.
pub struct Body {
inner: BodyInner,
Expand Down Expand Up @@ -54,8 +54,8 @@ impl Body {
}

/// Collect the full contents of the body into a vector.
///
/// This method is asynchronous because, in general, it requires reading an async
///
/// This method is asynchronous because, in general, it requires reading an async
/// stream of `BodyChunk` values.
pub async fn to_vec(&mut self) -> Result<Vec<u8>, Error> {
match &mut self.inner {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Into<hyper::Body> for Body {
}

/// A wrapper for json (de)serialization of bodies.
///
///
/// This type is usable both as an extractor (argument to an endpoint) and as a response
/// (return value from an endpoint).
pub struct Json<T>(pub T);
Expand All @@ -128,9 +128,9 @@ impl<T: Send + serde::de::DeserializeOwned + 'static, S: 'static> Extract<S> for
params: &RouteMatch<'_>,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());
FutureObj::new(Box::new(async move {
FutureObj::new(Box::new(async move {
fn mk_err<T>(_: T) -> Response { StatusCode::BAD_REQUEST.into_response() }
let body = await!(body.to_vec()).map_err(mk_err)?;
let body = await!(body.to_vec()).map_err(mk_err)?;
let json: T = serde_json::from_slice(&body).map_err(mk_err)?;
Ok(Json(json))
}))
Expand All @@ -146,4 +146,4 @@ impl<T: 'static + Send + serde::Serialize> IntoResponse for Json<T> {
.body(Body::from(serde_json::to_vec(&self.0).unwrap()))
.unwrap()
}
}
}

0 comments on commit 294c311

Please sign in to comment.