aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-11-22 19:38:53 +0100
committerHarald Eilertsen <haraldei@anduin.net>2017-11-22 19:38:53 +0100
commit71391281d49731de6c10046735e188266f4bc514 (patch)
tree159497cf5aad6c8dd26f66f8c072b5dd79f765ed /src
parent2e98f140e810d5271258df6905e744737734c666 (diff)
downloadrocket-blog-71391281d49731de6c10046735e188266f4bc514.tar.gz
rocket-blog-71391281d49731de6c10046735e188266f4bc514.tar.bz2
rocket-blog-71391281d49731de6c10046735e188266f4bc514.zip
Move responder impl's for templates to a macro.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs34
1 files changed, 19 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index a709cd9..c94dab5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,23 +23,27 @@ struct IndexTemplate<'a> {
posts: Vec<models::Post>
}
-impl<'a> Responder<'a> for IndexTemplate<'a> {
- fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
- Response::build()
- .header(ContentType::HTML)
- .sized_body(Cursor::new(format!("{}", &self)))
- .ok()
- }
+macro_rules! implement_responder_for {
+ // Implement a responder for the given template type
+ //
+ // Seems I can't add the lifetime after the matcher,
+ // like this: `$template_type<'a>`
+ // So it will have to be passed in to the argument at
+ // the macro incovation instead.
+ ($template_type:ty) => (
+ impl<'a> Responder<'a> for $template_type {
+ fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
+ Response::build()
+ .header(ContentType::HTML)
+ .sized_body(Cursor::new(format!("{}", &self)))
+ .ok()
+ }
+ }
+ )
}
-impl<'a> Responder<'a> for posts::NewPostTemplate<'a> {
- fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
- Response::build()
- .header(ContentType::HTML)
- .sized_body(Cursor::new(format!("{}", &self)))
- .ok()
- }
-}
+implement_responder_for!(IndexTemplate<'a>);
+implement_responder_for!(posts::NewPostTemplate<'a>);
fn get_posts(conn: rocket_blog::DbConn) -> Vec<models::Post> {
use schema::posts::dsl::*;