From b511ee66c7fbdb3e8fd0766b382e636db8921536 Mon Sep 17 00:00:00 2001 From: Matthew Banister Date: Thu, 22 Oct 2015 17:38:30 -0400 Subject: [PATCH] feat(router): Add chainable routes closes #262 --- Cargo.toml | 5 +++ examples/chainable_router.rs | 22 +++++++++++ src/router/chainable_router.rs | 71 ++++++++++++++++++++++++++++++++++ src/router/mod.rs | 1 + 4 files changed, 99 insertions(+) create mode 100644 examples/chainable_router.rs create mode 100644 src/router/chainable_router.rs diff --git a/Cargo.toml b/Cargo.toml index 34cfa79cfc..cc03d9552a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,5 +84,10 @@ path = "examples/chaining.rs" [[example]] +name = "chainable_router" +path = "examples/chainable_router.rs" + +[[example]] + name = "form_data" path = "examples/form_data/form_data.rs" diff --git a/examples/chainable_router.rs b/examples/chainable_router.rs new file mode 100644 index 0000000000..7a4c65462a --- /dev/null +++ b/examples/chainable_router.rs @@ -0,0 +1,22 @@ +#[macro_use] extern crate nickel; +extern crate hyper; + +use nickel::{Nickel}; +use nickel::router::chainable_router::{ChainablePath, ChainableHandler}; + +fn main() { + let mut server = Nickel::new(); + server + .route("/") + .get(middleware!("get")) + .post(middleware!("post")) + .put(middleware!("put")) + .patch(middleware!("patch")) + .delete(middleware!("delete")) + .route("/chain1") + .get(middleware!("chain1")) + .route("/chain2") + .get(middleware!("chain2")); + + server.listen("127.0.0.1:6767"); +} diff --git a/src/router/chainable_router.rs b/src/router/chainable_router.rs new file mode 100644 index 0000000000..0bb3f6d7c6 --- /dev/null +++ b/src/router/chainable_router.rs @@ -0,0 +1,71 @@ +use hyper::method::Method; +use middleware::Middleware; +use nickel::Nickel; +use router::http_router::{HttpRouter}; + +pub struct ChainableRoute<'a, D: Sync + Send + 'static = ()> { + path: String, + nickel: &'a mut Nickel +} + +pub trait ChainablePath<'a, D> { + fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D>; +} + +impl<'a, D: Sync + Send + 'static> ChainablePath<'a, D> for Nickel { + fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D> { + ChainableRoute { + path: path.to_string(), + nickel: self + } + } +} + +pub trait ChainableHandler<'a, D> { + fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D>; + fn get>(&mut self, handler: H) -> &mut Self; + fn post>(&mut self, handler: H) -> &mut Self; + fn delete>(&mut self, handler: H) -> &mut Self; + fn options>(&mut self, handler: H) -> &mut Self; + fn patch>(&mut self, handler: H) -> &mut Self; + fn put>(&mut self, handler: H) -> &mut Self; +} + +impl<'a, D: Sync + Send + 'static> ChainableHandler<'a, D> for ChainableRoute<'a, D> { + fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D> { + ChainableRoute { + path: path.to_string(), + nickel: self.nickel + } + } + fn get>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Get, &path[..], handler); + self + } + fn post>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Post, &path[..], handler); + self + } + fn delete>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Delete, &path[..], handler); + self + } + fn options>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Options, &path[..], handler); + self + } + fn patch>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Patch, &path[..], handler); + self + } + fn put>(&mut self, handler: H) -> &mut Self { + let path = self.path.clone(); + self.nickel.add_route(Method::Put, &path[..], handler); + self + } +} diff --git a/src/router/mod.rs b/src/router/mod.rs index 63a1cba2da..a72971ed7e 100644 --- a/src/router/mod.rs +++ b/src/router/mod.rs @@ -6,5 +6,6 @@ pub use self::into_matcher::FORMAT_PARAM; pub mod http_router; pub mod router; +pub mod chainable_router; mod matcher; mod into_matcher;