Skip to content

Commit

Permalink
Uncouple endpoint construction from routing
Browse files Browse the repository at this point in the history
The result looks more clunky for binding it to a route but at least the
concerns are separate more strictly. This also removes the need for
duplicating the router utilities for methods.
  • Loading branch information
HeroicKatora committed Mar 1, 2019
1 parent cd48e8f commit 3294cc6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
5 changes: 3 additions & 2 deletions examples/seeded_extractor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(async_await, futures_api)]

use tide::Seeded;
use tide::head::{NamedHeader, Header, SegmentName, Named};
use http::header::{HeaderName, HeaderValue};

Expand All @@ -13,7 +14,7 @@ async fn display_number(nr: Named<i32>) -> String {

fn main() {
let mut app = tide::App::new(());
app.at("/").get_with(display_header, NamedHeader(HeaderName::from_static("user-agent")));
app.at("/numbered/{num}").get_with(display_number, SegmentName("num".into()));
app.at("/").get(Seeded(display_header, NamedHeader(HeaderName::from_static("user-agent"))));
app.at("/numbered/{num}").get(Seeded(display_number, SegmentName("num".into())));
app.serve();
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use crate::{
app::{App, AppData, Server},
configuration::ExtractConfiguration,
cookies::Cookies,
endpoint::Endpoint,
endpoint::{Endpoint, Seeded},
extract::{Extract, ExtractSeed},
middleware::Middleware,
request::{Compute, Computed, Request},
Expand Down
8 changes: 1 addition & 7 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use crate::{
configuration::Store,
endpoint::{BoxedEndpoint, Endpoint, Seeded},
endpoint::{BoxedEndpoint, Endpoint},
Middleware,
};
use path_table::{PathTable, RouteMatch};
Expand Down Expand Up @@ -279,12 +279,6 @@ impl<'a, Data> Resource<'a, Data> {
self.method(http::Method::GET, ep)
}

pub fn get_with<T, S, U>(&mut self, ep: T, seed: S) -> &mut EndpointData<Data>
where Seeded<T, S>: Endpoint<Data, U>
{
self.method(http::Method::GET, Seeded(ep, seed))
}

/// Add an endpoint for `HEAD` requests
pub fn head<T: Endpoint<Data, U>, U>(&mut self, ep: T) -> &mut EndpointData<Data> {
self.method(http::Method::HEAD, ep)
Expand Down

0 comments on commit 3294cc6

Please sign in to comment.