about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-15T19·31+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-15T19·31+0200
commitce18cfa2d67ccf6ec560d59da1753930e86d169c (patch)
tree504f8eb63d615ac7d36de8f2778eade845f6e684
parentd9d1a3313f5cbceb5cb4878752fe0dc117bed3b0 (diff)
refactor: Add a message!-macro to reduce message boilerplate
-rw-r--r--src/db.rs30
-rw-r--r--src/main.rs10
-rw-r--r--src/oidc.rs10
-rw-r--r--src/render.rs20
4 files changed, 22 insertions, 48 deletions
diff --git a/src/db.rs b/src/db.rs
index ad1c148a7072..202f6c1609ca 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -35,10 +35,7 @@ impl Actor for DbExecutor {
 /// Message used to request a list of threads.
 /// TODO: This should support page numbers.
 pub struct ListThreads;
-
-impl Message for ListThreads {
-    type Result = Result<Vec<ThreadIndex>>;
-}
+message!(ListThreads, Result<Vec<ThreadIndex>>);
 
 impl Handler<ListThreads> for DbExecutor {
     type Result = <ListThreads as Message>::Result;
@@ -56,10 +53,7 @@ impl Handler<ListThreads> for DbExecutor {
 /// Message used to fetch a specific thread. Returns the thread and
 /// its posts.
 pub struct GetThread(pub i32);
-
-impl Message for GetThread {
-    type Result = Result<(Thread, Vec<Post>)>;
-}
+message!(GetThread, Result<(Thread, Vec<Post>)>);
 
 impl Handler<GetThread> for DbExecutor {
     type Result = <GetThread as Message>::Result;
@@ -82,10 +76,7 @@ pub struct CreateThread {
     pub new_thread: NewThread,
     pub post: String,
 }
-
-impl Message for CreateThread {
-    type Result = Result<Thread>;
-}
+message!(CreateThread, Result<Thread>);
 
 impl Handler<CreateThread> for DbExecutor {
     type Result = <CreateThread as Message>::Result;
@@ -121,10 +112,7 @@ impl Handler<CreateThread> for DbExecutor {
 
 /// Message used to create a new reply
 pub struct CreatePost(pub NewPost);
-
-impl Message for CreatePost {
-    type Result = Result<Post>;
-}
+message!(CreatePost, Result<Post>);
 
 impl Handler<CreatePost> for DbExecutor {
     type Result = <CreatePost as Message>::Result;
@@ -143,10 +131,7 @@ impl Handler<CreatePost> for DbExecutor {
 /// Message used to search for posts
 #[derive(Deserialize)]
 pub struct SearchPosts { pub query: String }
-
-impl Message for SearchPosts {
-    type Result = Result<Vec<SearchResult>>;
-}
+message!(SearchPosts, Result<Vec<SearchResult>>);
 
 /// Raw PostgreSQL query used to perform full-text search on posts
 /// with a supplied phrase. For now, the query language is hardcoded
@@ -182,10 +167,7 @@ impl Handler<SearchPosts> for DbExecutor {
 /// Message that triggers a refresh of the view used for full-text
 /// searching.
 pub struct RefreshSearchView;
-
-impl Message for RefreshSearchView {
-    type Result = Result<()>;
-}
+message!(RefreshSearchView, Result<()>);
 
 const REFRESH_QUERY: &'static str = "REFRESH MATERIALIZED VIEW search_index";
 
diff --git a/src/main.rs b/src/main.rs
index 9ec60044021d..8d81a670e0a2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -47,6 +47,16 @@ extern crate tokio_timer;
 extern crate url;
 extern crate url_serde;
 
+/// Simple macro used to reduce boilerplate when defining actor
+/// message types.
+macro_rules! message {
+    ( $t:ty, $r:ty ) => {
+        impl Message for $t {
+            type Result = $r;
+        }
+    }
+}
+
 pub mod db;
 pub mod errors;
 pub mod handlers;
diff --git a/src/oidc.rs b/src/oidc.rs
index d9ba4237be00..3ec15854a8b2 100644
--- a/src/oidc.rs
+++ b/src/oidc.rs
@@ -70,10 +70,7 @@ impl Actor for OidcExecutor {
 
 /// Message used to request the login URL:
 pub struct GetLoginUrl; // TODO: Add a nonce parameter stored in session.
-
-impl Message for GetLoginUrl {
-    type Result = String;
-}
+message!(GetLoginUrl, String);
 
 impl Handler<GetLoginUrl> for OidcExecutor {
     type Result = String;
@@ -95,10 +92,7 @@ impl Handler<GetLoginUrl> for OidcExecutor {
 /// Message used to request the token from the returned code and
 /// retrieve userinfo from the appropriate endpoint.
 pub struct RetrieveToken(pub CodeResponse);
-
-impl Message for RetrieveToken {
-    type Result = Result<Author>;
-}
+message!(RetrieveToken, Result<Author>);
 
 #[derive(Debug, Deserialize)]
 struct TokenResponse {
diff --git a/src/render.rs b/src/render.rs
index 0c1e69d3c4c9..c0a2fe88e697 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -49,10 +49,7 @@ impl From<DateTime<Utc>> for FormattedDate {
 pub struct IndexPage {
     pub threads: Vec<ThreadIndex>,
 }
-
-impl Message for IndexPage {
-    type Result = Result<String>;
-}
+message!(IndexPage, Result<String>);
 
 #[derive(Debug, Serialize)]
 struct IndexThread {
@@ -90,10 +87,7 @@ pub struct ThreadPage {
     pub thread: Thread,
     pub posts: Vec<Post>,
 }
-
-impl Message for ThreadPage {
-    type Result = Result<String>;
-}
+message!(ThreadPage, Result<String>);
 
 // "Renderable" structures with data transformations applied.
 #[derive(Debug, Serialize)]
@@ -200,10 +194,7 @@ pub struct NewThreadPage {
     pub title: Option<String>,
     pub post: Option<String>,
 }
-
-impl Message for NewThreadPage {
-    type Result = Result<String>;
-}
+message!(NewThreadPage, Result<String>);
 
 impl Handler<NewThreadPage> for Renderer {
     type Result = Result<String>;
@@ -225,10 +216,7 @@ pub struct SearchResultPage {
     pub query: String,
     pub results: Vec<SearchResult>,
 }
-
-impl Message for SearchResultPage {
-    type Result = Result<String>;
-}
+message!(SearchResultPage, Result<String>);
 
 impl Handler<SearchResultPage> for Renderer {
     type Result = Result<String>;