refactor: Add a message!-macro to reduce message boilerplate

This commit is contained in:
Vincent Ambo 2018-04-15 21:31:14 +02:00
parent d9d1a3313f
commit ce18cfa2d6
4 changed files with 22 additions and 48 deletions

View file

@ -35,10 +35,7 @@ impl Actor for DbExecutor {
/// Message used to request a list of threads. /// Message used to request a list of threads.
/// TODO: This should support page numbers. /// TODO: This should support page numbers.
pub struct ListThreads; pub struct ListThreads;
message!(ListThreads, Result<Vec<ThreadIndex>>);
impl Message for ListThreads {
type Result = Result<Vec<ThreadIndex>>;
}
impl Handler<ListThreads> for DbExecutor { impl Handler<ListThreads> for DbExecutor {
type Result = <ListThreads as Message>::Result; 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 /// Message used to fetch a specific thread. Returns the thread and
/// its posts. /// its posts.
pub struct GetThread(pub i32); pub struct GetThread(pub i32);
message!(GetThread, Result<(Thread, Vec<Post>)>);
impl Message for GetThread {
type Result = Result<(Thread, Vec<Post>)>;
}
impl Handler<GetThread> for DbExecutor { impl Handler<GetThread> for DbExecutor {
type Result = <GetThread as Message>::Result; type Result = <GetThread as Message>::Result;
@ -82,10 +76,7 @@ pub struct CreateThread {
pub new_thread: NewThread, pub new_thread: NewThread,
pub post: String, pub post: String,
} }
message!(CreateThread, Result<Thread>);
impl Message for CreateThread {
type Result = Result<Thread>;
}
impl Handler<CreateThread> for DbExecutor { impl Handler<CreateThread> for DbExecutor {
type Result = <CreateThread as Message>::Result; type Result = <CreateThread as Message>::Result;
@ -121,10 +112,7 @@ impl Handler<CreateThread> for DbExecutor {
/// Message used to create a new reply /// Message used to create a new reply
pub struct CreatePost(pub NewPost); pub struct CreatePost(pub NewPost);
message!(CreatePost, Result<Post>);
impl Message for CreatePost {
type Result = Result<Post>;
}
impl Handler<CreatePost> for DbExecutor { impl Handler<CreatePost> for DbExecutor {
type Result = <CreatePost as Message>::Result; type Result = <CreatePost as Message>::Result;
@ -143,10 +131,7 @@ impl Handler<CreatePost> for DbExecutor {
/// Message used to search for posts /// Message used to search for posts
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct SearchPosts { pub query: String } pub struct SearchPosts { pub query: String }
message!(SearchPosts, Result<Vec<SearchResult>>);
impl Message for SearchPosts {
type Result = Result<Vec<SearchResult>>;
}
/// Raw PostgreSQL query used to perform full-text search on posts /// Raw PostgreSQL query used to perform full-text search on posts
/// with a supplied phrase. For now, the query language is hardcoded /// 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 /// Message that triggers a refresh of the view used for full-text
/// searching. /// searching.
pub struct RefreshSearchView; pub struct RefreshSearchView;
message!(RefreshSearchView, Result<()>);
impl Message for RefreshSearchView {
type Result = Result<()>;
}
const REFRESH_QUERY: &'static str = "REFRESH MATERIALIZED VIEW search_index"; const REFRESH_QUERY: &'static str = "REFRESH MATERIALIZED VIEW search_index";

View file

@ -47,6 +47,16 @@ extern crate tokio_timer;
extern crate url; extern crate url;
extern crate url_serde; 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 db;
pub mod errors; pub mod errors;
pub mod handlers; pub mod handlers;

View file

@ -70,10 +70,7 @@ impl Actor for OidcExecutor {
/// Message used to request the login URL: /// Message used to request the login URL:
pub struct GetLoginUrl; // TODO: Add a nonce parameter stored in session. pub struct GetLoginUrl; // TODO: Add a nonce parameter stored in session.
message!(GetLoginUrl, String);
impl Message for GetLoginUrl {
type Result = String;
}
impl Handler<GetLoginUrl> for OidcExecutor { impl Handler<GetLoginUrl> for OidcExecutor {
type Result = String; type Result = String;
@ -95,10 +92,7 @@ impl Handler<GetLoginUrl> for OidcExecutor {
/// Message used to request the token from the returned code and /// Message used to request the token from the returned code and
/// retrieve userinfo from the appropriate endpoint. /// retrieve userinfo from the appropriate endpoint.
pub struct RetrieveToken(pub CodeResponse); pub struct RetrieveToken(pub CodeResponse);
message!(RetrieveToken, Result<Author>);
impl Message for RetrieveToken {
type Result = Result<Author>;
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct TokenResponse { struct TokenResponse {

View file

@ -49,10 +49,7 @@ impl From<DateTime<Utc>> for FormattedDate {
pub struct IndexPage { pub struct IndexPage {
pub threads: Vec<ThreadIndex>, pub threads: Vec<ThreadIndex>,
} }
message!(IndexPage, Result<String>);
impl Message for IndexPage {
type Result = Result<String>;
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct IndexThread { struct IndexThread {
@ -90,10 +87,7 @@ pub struct ThreadPage {
pub thread: Thread, pub thread: Thread,
pub posts: Vec<Post>, pub posts: Vec<Post>,
} }
message!(ThreadPage, Result<String>);
impl Message for ThreadPage {
type Result = Result<String>;
}
// "Renderable" structures with data transformations applied. // "Renderable" structures with data transformations applied.
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -200,10 +194,7 @@ pub struct NewThreadPage {
pub title: Option<String>, pub title: Option<String>,
pub post: Option<String>, pub post: Option<String>,
} }
message!(NewThreadPage, Result<String>);
impl Message for NewThreadPage {
type Result = Result<String>;
}
impl Handler<NewThreadPage> for Renderer { impl Handler<NewThreadPage> for Renderer {
type Result = Result<String>; type Result = Result<String>;
@ -225,10 +216,7 @@ pub struct SearchResultPage {
pub query: String, pub query: String,
pub results: Vec<SearchResult>, pub results: Vec<SearchResult>,
} }
message!(SearchResultPage, Result<String>);
impl Message for SearchResultPage {
type Result = Result<String>;
}
impl Handler<SearchResultPage> for Renderer { impl Handler<SearchResultPage> for Renderer {
type Result = Result<String>; type Result = Result<String>;