Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IntoResponse trait #463

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ async fn main() -> Result<(), std::io::Error> {
app.at("/api").nest({
let mut api = tide::new();
api.at("/hello").get(|_| async move { Ok("Hello, world") });
api.at("/goodbye").get(|_| async move { Ok("Goodbye, world") });
api.at("/goodbye")
.get(|_| async move { Ok("Goodbye, world") });
api
});
app.listen("127.0.0.1:8080").await?;
Expand Down
9 changes: 4 additions & 5 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::response::IntoResponse;
use async_std::future::Future;
use async_std::sync::Arc;
use http_types::Result;

use crate::middleware::Next;
use crate::utils::BoxFuture;
use crate::{Middleware, Request};
use crate::{Middleware, Request, Response};

/// An HTTP request handler.
///
/// This trait is automatically implemented for `Fn` types, and so is rarely implemented
/// directly by Tide users.
///
/// In practice, endpoints are functions that take a `Request<State>` as an argument and
/// return a type `T` that implements [`IntoResponse`].
/// return a type `T` that implements `Into<Response>`.
///
/// # Examples
///
Expand Down Expand Up @@ -60,13 +59,13 @@ impl<State, F: Send + Sync + 'static, Fut, Res> Endpoint<State> for F
where
F: Fn(Request<State>) -> Fut,
Fut: Future<Output = Result<Res>> + Send + 'static,
Res: IntoResponse,
Res: Into<Response>,
{
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
let fut = (self)(req);
Box::pin(async move {
let res = fut.await?;
Ok(res.into_response())
Ok(res.into())
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub use http_types::{Body, Error, Status, StatusCode};
#[doc(inline)]
pub use middleware::{Middleware, Next};
#[doc(inline)]
pub use response::{IntoResponse, Response};
pub use response::Response;
#[doc(inline)]
pub use server::{Route, Server};

Expand Down
2 changes: 1 addition & 1 deletion src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::Request;
// pub use compression::{Compression, Decompression};
// pub use default_headers::DefaultHeaders;

/// Middleware that wraps around remaining middleware chain.
/// Middleware that wraps around the remaining middleware chain.
pub trait Middleware<State>: 'static + Send + Sync {
/// Asynchronously handle the request, and return a response.
fn handle<'a>(
Expand Down
11 changes: 10 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use http_types::{
use route_recognizer::Params;
use serde::Deserialize;

use async_std::io::{self, prelude::*};
use async_std::io::{self, prelude::*, BufReader};
use async_std::task::{Context, Poll};

use std::pin::Pin;
use std::{str::FromStr, sync::Arc};

use crate::cookies::CookieData;
use crate::Response;

/// An HTTP request.
///
Expand Down Expand Up @@ -319,6 +320,14 @@ impl<State> Read for Request<State> {
}
}

// NOTE: From cannot be implemented for this conversion because `State` needs to
// be constrained by a type.
impl<State: Send + Sync + 'static> Into<Response> for Request<State> {
fn into(self) -> Response {
Response::new(StatusCode::Ok).body(BufReader::new(self))
}
}

impl<State> IntoIterator for Request<State> {
type Item = (HeaderName, Vec<HeaderValue>);
type IntoIter = http_types::headers::IntoIter;
Expand Down
30 changes: 24 additions & 6 deletions src/response/mod.rs → src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ use http_types::{
use mime::Mime;
use serde::Serialize;

pub use into_response::IntoResponse;

mod into_response;

#[derive(Debug)]
pub(crate) enum CookieEvent {
Added(Cookie<'static>),
Expand Down Expand Up @@ -245,14 +241,12 @@ impl Response {
}
}

#[doc(hidden)]
impl Into<http_service::Response> for Response {
fn into(self) -> http_service::Response {
self.res
}
}

#[doc(hidden)]
impl From<http_service::Response> for Response {
fn from(res: http_service::Response) -> Self {
Self {
Expand All @@ -262,6 +256,30 @@ impl From<http_service::Response> for Response {
}
}

impl From<String> for Response {
fn from(s: String) -> Self {
let mut res = http_types::Response::new(StatusCode::Ok);
res.set_content_type(http_types::mime::PLAIN);
res.set_body(s);
Self {
res,
cookie_events: vec![],
}
}
}

impl<'a> From<&'a str> for Response {
fn from(s: &'a str) -> Self {
let mut res = http_types::Response::new(StatusCode::Ok);
res.set_content_type(http_types::mime::PLAIN);
res.set_body(String::from(s));
Self {
res,
cookie_events: vec![],
}
}
}

impl IntoIterator for Response {
type Item = (HeaderName, Vec<HeaderValue>);
type IntoIter = http_types::headers::IntoIter;
Expand Down
111 changes: 0 additions & 111 deletions src/response/into_response.rs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/querystring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_std::prelude::*;
use futures::executor::block_on;
use http_service_mock::{make_server, TestBackend};
use serde::Deserialize;
use tide::{IntoResponse, Request, Response, Server, StatusCode};
use tide::{Request, Response, Server, StatusCode};

#[derive(Deserialize)]
struct Params {
Expand All @@ -18,7 +18,7 @@ struct OptionalParams {
async fn handler(cx: Request<()>) -> tide::Result {
let p = cx.query::<Params>();
match p {
Ok(params) => Ok(params.msg.into_response()),
Ok(params) => Ok(params.msg.into()),
Err(error) => Ok(err_to_res(error)),
}
}
Expand Down