Skip to content

Commit e3a92f1

Browse files
committed
Restore default params in tide::Result
This means we no long have to define Result<Response> in every single location.
1 parent 4c1f629 commit e3a92f1

File tree

16 files changed

+47
-54
lines changed

16 files changed

+47
-54
lines changed

examples/cookies.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use async_std::task;
2-
use cookie::Cookie;
3-
use tide::{Request, Response, StatusCode};
2+
use tide::http::Cookie;
3+
use tide::{Request, StatusCode};
44

55
/// Tide will use the the `Cookies`'s `Extract` implementation to build this parameter.
66
///
77
async fn retrieve_cookie(cx: Request<()>) -> tide::Result<String> {
88
Ok(format!("hello cookies: {:?}", cx.cookie("hello").unwrap()))
99
}
1010

11-
async fn set_cookie(_req: Request<()>) -> tide::Result<Response> {
11+
async fn set_cookie(_req: Request<()>) -> tide::Result {
1212
let mut res = tide::Response::new(StatusCode::Ok);
1313
res.set_cookie(Cookie::new("hello", "world"));
1414
Ok(res)
1515
}
1616

17-
async fn remove_cookie(_req: Request<()>) -> tide::Result<Response> {
17+
async fn remove_cookie(_req: Request<()>) -> tide::Result {
1818
let mut res = tide::Response::new(StatusCode::Ok);
1919
res.remove_cookie(Cookie::named("hello"));
2020
Ok(res)

examples/graphql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn create_schema() -> Schema {
7272
Schema::new(QueryRoot {}, MutationRoot {})
7373
}
7474

75-
async fn handle_graphql(mut cx: Request<State>) -> tide::Result<Response> {
75+
async fn handle_graphql(mut cx: Request<State>) -> tide::Result {
7676
let query: juniper::http::GraphQLRequest = cx
7777
.body_json()
7878
.await
@@ -92,7 +92,7 @@ async fn handle_graphql(mut cx: Request<State>) -> tide::Result<Response> {
9292
Ok(res)
9393
}
9494

95-
async fn handle_graphiql(_: Request<State>) -> tide::Result<Response> {
95+
async fn handle_graphiql(_: Request<State>) -> tide::Result {
9696
let res = Response::new(StatusCode::Ok)
9797
.body_string(juniper::http::graphiql::graphiql_source("/graphql"))
9898
.set_header("content-type".parse().unwrap(), "text/html;charset=utf-8");

src/cookies/middleware.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::response::CookieEvent;
22
use crate::utils::BoxFuture;
3-
use crate::{Middleware, Next};
4-
use crate::{Request, Response, Result};
3+
use crate::{Middleware, Next, Request};
54

65
use cookie::CookieJar;
76
use http_types::headers;
@@ -37,7 +36,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
3736
&'a self,
3837
mut ctx: Request<State>,
3938
next: Next<'a, State>,
40-
) -> BoxFuture<'a, Result<Response>> {
39+
) -> BoxFuture<'a, crate::Result> {
4140
Box::pin(async move {
4241
let cookie_jar = if let Some(cookie_data) = ctx.local::<CookieData>() {
4342
cookie_data.content.clone()

src/endpoint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use http_types::Result;
55

66
use crate::middleware::Next;
77
use crate::utils::BoxFuture;
8-
use crate::{Middleware, Request, Response};
8+
use crate::{Middleware, Request};
99

1010
/// An HTTP request handler.
1111
///
@@ -51,7 +51,7 @@ use crate::{Middleware, Request, Response};
5151
/// Tide routes will also accept endpoints with `Fn` signatures of this form, but using the `async` keyword has better ergonomics.
5252
pub trait Endpoint<State>: Send + Sync + 'static {
5353
/// Invoke the endpoint within the given context
54-
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Result<Response>>;
54+
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, crate::Result>;
5555
}
5656

5757
pub(crate) type DynEndpoint<State> = dyn Endpoint<State>;
@@ -62,7 +62,7 @@ where
6262
Fut: Future<Output = Result<Res>> + Send + 'static,
6363
Res: IntoResponse,
6464
{
65-
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Result<Response>> {
65+
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
6666
let fut = (self)(req);
6767
Box::pin(async move {
6868
let res = fut.await?;
@@ -111,7 +111,7 @@ impl<E, State: 'static> Endpoint<State> for MiddlewareEndpoint<E, State>
111111
where
112112
E: Endpoint<State>,
113113
{
114-
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Result<Response>> {
114+
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
115115
let next = Next {
116116
endpoint: &self.endpoint,
117117
next_middleware: &self.middleware,

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
//! it's incredibly efficient.
7878
//!
7979
//! ```txt
80-
//! async fn endpoint(req: Request) -> Result<Response>;
80+
//! async fn endpoint(req: Request) -> Result;
8181
//! ```
8282
//!
8383
//! ## Middleware
@@ -88,7 +88,7 @@
8888
//! like a stack. A simplified example of the logger middleware is something like this:
8989
//!
9090
//! ```ignore
91-
//! async fn log(req: Request, next: Next) -> Result<Response> {
91+
//! async fn log(req: Request, next: Next) -> tide::Result {
9292
//! println!("Incoming request from {} on url {}", req.peer_addr(), req.url());
9393
//! let res = next().await?;
9494
//! println!("Outgoing response with status {}", res.status());
@@ -204,7 +204,7 @@ pub use request::Request;
204204
pub mod sse;
205205

206206
#[doc(inline)]
207-
pub use http_types::{Body, Error, Result, Status, StatusCode};
207+
pub use http_types::{Body, Error, Status, StatusCode};
208208

209209
#[doc(inline)]
210210
pub use middleware::{Middleware, Next};
@@ -271,3 +271,6 @@ where
271271
{
272272
Server::with_state(state)
273273
}
274+
275+
/// A specialized Result type for Tide.
276+
pub type Result<T = Response> = std::result::Result<T, Error>;

src/log/middleware.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::log;
2-
use crate::{Middleware, Next, Request, Response};
3-
use futures_core::future::BoxFuture;
2+
use crate::utils::BoxFuture;
3+
use crate::{Middleware, Next, Request};
44

55
/// Log all incoming requests and responses.
66
///
@@ -28,7 +28,7 @@ impl LogMiddleware {
2828
&'a self,
2929
ctx: Request<State>,
3030
next: Next<'a, State>,
31-
) -> crate::Result<Response> {
31+
) -> crate::Result {
3232
let path = ctx.uri().path().to_owned();
3333
let method = ctx.method().to_string();
3434
log::trace!("IN => {} {}", method, path);
@@ -65,7 +65,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for LogMiddleware {
6565
&'a self,
6666
ctx: Request<State>,
6767
next: Next<'a, State>,
68-
) -> BoxFuture<'a, crate::Result<Response>> {
68+
) -> BoxFuture<'a, crate::Result> {
6969
Box::pin(async move { self.log(ctx, next).await })
7070
}
7171
}

src/middleware.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use std::sync::Arc;
44

55
#[doc(inline)]
66
pub use http_service::HttpService;
7-
use http_types::Result;
87

98
use crate::endpoint::DynEndpoint;
109
use crate::utils::BoxFuture;
11-
use crate::{Request, Response};
10+
use crate::Request;
1211

1312
// mod compression;
1413
// mod default_headers;
@@ -23,21 +22,21 @@ pub trait Middleware<State>: 'static + Send + Sync {
2322
&'a self,
2423
cx: Request<State>,
2524
next: Next<'a, State>,
26-
) -> BoxFuture<'a, Result<Response>>;
25+
) -> BoxFuture<'a, crate::Result>;
2726
}
2827

2928
impl<State, F> Middleware<State> for F
3029
where
3130
F: Send
3231
+ Sync
3332
+ 'static
34-
+ for<'a> Fn(Request<State>, Next<'a, State>) -> BoxFuture<'a, crate::Result<Response>>,
33+
+ for<'a> Fn(Request<State>, Next<'a, State>) -> BoxFuture<'a, crate::Result>,
3534
{
3635
fn handle<'a>(
3736
&'a self,
3837
req: Request<State>,
3938
next: Next<'a, State>,
40-
) -> BoxFuture<'a, crate::Result<Response>> {
39+
) -> BoxFuture<'a, crate::Result> {
4140
(self)(req, next)
4241
}
4342
}
@@ -51,7 +50,7 @@ pub struct Next<'a, State> {
5150

5251
impl<'a, State: 'static> Next<'a, State> {
5352
/// Asynchronously execute the remaining middleware chain.
54-
pub fn run(mut self, req: Request<State>) -> BoxFuture<'a, Result<Response>> {
53+
pub fn run(mut self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
5554
if let Some((current, next)) = self.next_middleware.split_first() {
5655
self.next_middleware = next;
5756
current.handle(req, self)

src/redirect/temporary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct TemporaryRedirect {
3232
}
3333

3434
impl<State> Endpoint<State> for TemporaryRedirect {
35-
fn call<'a>(&'a self, _req: Request<State>) -> BoxFuture<'a, crate::Result<Response>> {
35+
fn call<'a>(&'a self, _req: Request<State>) -> BoxFuture<'a, crate::Result> {
3636
let res = Response::redirect_temporary(&self.location);
3737
Box::pin(async move { Ok(res) })
3838
}

src/response/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Response {
6262
/// # use tide::{Response, Request, StatusCode};
6363
/// # fn canonicalize(uri: &url::Url) -> Option<&url::Url> { None }
6464
/// # #[allow(dead_code)]
65-
/// async fn route_handler(request: Request<()>) -> tide::Result<Response> {
65+
/// async fn route_handler(request: Request<()>) -> tide::Result {
6666
/// if let Some(canonical_redirect) = canonicalize(request.uri()) {
6767
/// Ok(Response::redirect_permanent(canonical_redirect))
6868
/// } else {
@@ -85,7 +85,7 @@ impl Response {
8585
/// # use tide::{Response, Request, StatusCode};
8686
/// # fn special_sale_today() -> Option<String> { None }
8787
/// # #[allow(dead_code)]
88-
/// async fn route_handler(request: Request<()>) -> tide::Result<Response> {
88+
/// async fn route_handler(request: Request<()>) -> tide::Result {
8989
/// if let Some(sale_url) = special_sale_today() {
9090
/// Ok(Response::redirect_temporary(sale_url))
9191
/// } else {

src/router.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use http_types::Result;
21
use route_recognizer::{Match, Params, Router as MethodRouter};
32
use std::collections::HashMap;
43

54
use crate::endpoint::DynEndpoint;
65
use crate::utils::BoxFuture;
7-
use crate::{Request, Response};
6+
use crate::{Request, Response, StatusCode};
87

98
/// The routing table used by `Server`
109
///
@@ -87,10 +86,10 @@ impl<State: 'static> Router<State> {
8786
}
8887
}
8988

90-
fn not_found_endpoint<State>(_cx: Request<State>) -> BoxFuture<'static, Result<Response>> {
91-
Box::pin(async move { Ok(Response::new(crate::StatusCode::NotFound.into())) })
89+
fn not_found_endpoint<State>(_cx: Request<State>) -> BoxFuture<'static, crate::Result> {
90+
Box::pin(async move { Ok(Response::new(StatusCode::NotFound)) })
9291
}
9392

94-
fn method_not_allowed<State>(_cx: Request<State>) -> BoxFuture<'static, Result<Response>> {
95-
Box::pin(async move { Ok(Response::new(crate::StatusCode::MethodNotAllowed.into())) })
93+
fn method_not_allowed<State>(_cx: Request<State>) -> BoxFuture<'static, crate::Result> {
94+
Box::pin(async move { Ok(Response::new(StatusCode::MethodNotAllowed)) })
9695
}

0 commit comments

Comments
 (0)