aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-12-23 12:47:56 +0100
committerHarald Eilertsen <haraldei@anduin.net>2017-12-23 12:47:56 +0100
commitd4f1eb033376fab5074c71558ac796c20026ec19 (patch)
tree1c00304f68b42b33188c0428a1989e61081762e5
parentb5a32ebbc86d6b17f4cb00673eb38096fe82defb (diff)
downloadrocket-blog-d4f1eb033376fab5074c71558ac796c20026ec19.tar.gz
rocket-blog-d4f1eb033376fab5074c71558ac796c20026ec19.tar.bz2
rocket-blog-d4f1eb033376fab5074c71558ac796c20026ec19.zip
Move implement_responder_for macro into crate.
This is so we can use it in other modules too. Besides it makes things slightly cleaner.
-rw-r--r--src/lib.rs25
-rw-r--r--src/main.rs26
-rw-r--r--src/posts.rs2
-rw-r--r--templates/post_teaser.html5
4 files changed, 32 insertions, 26 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c950e7a..0549a3f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -67,3 +67,28 @@ impl Deref for DbConn {
&self.0
}
}
+
+#[macro_export]
+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) => (
+ use rocket::response::{Response, Responder};
+ use rocket::http::{ContentType, Status};
+ use std::io::Cursor;
+ use rocket::request::Request;
+
+ 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()
+ }
+ }
+ )
+}
diff --git a/src/main.rs b/src/main.rs
index f89a39f..0cf1acf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,7 @@
extern crate diesel;
extern crate rocket;
-extern crate rocket_blog;
+#[macro_use] extern crate rocket_blog;
extern crate rocket_contrib;
#[macro_use] extern crate bart_derive;
#[macro_use] extern crate serde_derive;
@@ -11,10 +11,6 @@ extern crate rocket_contrib;
use self::diesel::prelude::*;
use self::rocket_blog::{schema, models};
use rocket_contrib::Json;
-use rocket::response::{Response, Responder};
-use rocket::request::Request;
-use rocket::http::{ContentType, Status};
-use std::io::Cursor;
mod posts;
@@ -25,27 +21,7 @@ struct IndexTemplate<'a> {
posts: Vec<models::Post>
}
-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()
- }
- }
- )
-}
-
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::*;
diff --git a/src/posts.rs b/src/posts.rs
index a98ea5c..350ab53 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -9,6 +9,8 @@ pub struct NewPostTemplate<'a> {
post: ::models::NewPost
}
+implement_responder_for!(NewPostTemplate<'a>);
+
#[get("/new", format = "text/html")]
fn new<'a>(_conn: ::rocket_blog::DbConn) -> NewPostTemplate<'a> {
NewPostTemplate { title: "Bloggen", post: Default::default() }
diff --git a/templates/post_teaser.html b/templates/post_teaser.html
index 5198400..19a30ec 100644
--- a/templates/post_teaser.html
+++ b/templates/post_teaser.html
@@ -1,6 +1,9 @@
<article class="post-teaser">
<header>
- <h1>{{ .title }}</h1>
+ <h1><a href="/posts/{{ .id }}">{{ .title }}</a></h1>
+ <div class="post-actions">
+ Edit | Publish | Delete
+ </div>
</header>
<section class="teaser">
{{ .body }}