Skip to content

Commit 06055d9

Browse files
committed
chore: use macro to reduce code
1 parent 68042aa commit 06055d9

File tree

2 files changed

+58
-50
lines changed

2 files changed

+58
-50
lines changed

src/app_ui/async_task_channel.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,37 @@ pub enum TaskRequest {
2222
DbUpdateQuestion(Request<QuestionModel>),
2323
}
2424

25-
impl TaskRequest {
26-
pub async fn execute(
27-
self,
28-
client: &reqwest::Client,
29-
conn: &DatabaseConnection,
30-
) -> TaskResponse {
31-
match self {
32-
TaskRequest::QuestionDetail(Request {
33-
content: slug,
34-
widget_name,
35-
request_id,
36-
}) => get_question_details(request_id, widget_name, slug, client).await,
37-
TaskRequest::GetAllQuestionsMap(Request {
38-
widget_name,
39-
request_id,
40-
..
41-
}) => get_all_questions(request_id, widget_name, conn).await,
42-
TaskRequest::GetAllTopicTags(Request {
43-
widget_name,
44-
request_id,
45-
..
46-
}) => get_all_topic_tags(request_id, widget_name, conn).await,
47-
TaskRequest::GetQuestionEditorData(Request {
48-
request_id,
49-
content,
50-
widget_name,
51-
}) => get_editor_data(request_id, widget_name, content, client).await,
52-
TaskRequest::CodeRunRequest(Request {
53-
request_id,
54-
content,
55-
widget_name,
56-
}) => run_or_submit_question(request_id, widget_name, content, client).await,
57-
TaskRequest::DbUpdateQuestion(Request {
58-
request_id,
59-
content,
60-
widget_name,
61-
}) => update_status_to_accepted(request_id, widget_name, content, conn).await,
25+
macro_rules! impl_task_request {
26+
($(($var:ident, $f_name: ident)),*) => {
27+
impl TaskRequest {
28+
pub async fn execute(
29+
self,
30+
client: &reqwest::Client,
31+
conn: &DatabaseConnection,
32+
) -> TaskResponse {
33+
match self {
34+
$(
35+
TaskRequest::$var(Request {
36+
content,
37+
widget_name,
38+
request_id,
39+
}) => $f_name(request_id, widget_name, content, client, conn).await,
40+
)*
41+
}
42+
}
6243
}
63-
}
44+
};
6445
}
6546

47+
impl_task_request!(
48+
(QuestionDetail, get_question_details),
49+
(GetAllQuestionsMap, get_all_questions),
50+
(GetAllTopicTags, get_all_topic_tags),
51+
(GetQuestionEditorData, get_editor_data),
52+
(CodeRunRequest, run_or_submit_question),
53+
(DbUpdateQuestion, update_status_to_accepted)
54+
);
55+
6656
#[derive(Debug)]
6757
pub struct Response<T> {
6858
pub(crate) request_id: String,
@@ -81,21 +71,31 @@ pub enum TaskResponse {
8171
Error(Response<String>),
8272
}
8373

84-
impl TaskResponse {
85-
pub fn get_widget_name(&self) -> WidgetName {
86-
match self {
87-
TaskResponse::QuestionDetail(Response { widget_name, .. }) => widget_name,
88-
TaskResponse::GetAllQuestionsMap(Response { widget_name, .. }) => widget_name,
89-
TaskResponse::AllTopicTags(Response { widget_name, .. }) => widget_name,
90-
TaskResponse::Error(Response { widget_name, .. }) => widget_name,
91-
TaskResponse::QuestionEditorData(Response { widget_name, .. }) => widget_name,
92-
TaskResponse::RunResponseData(Response { widget_name, .. }) => widget_name,
93-
TaskResponse::DbUpdateStatus(Response { widget_name, .. }) => widget_name,
74+
macro_rules! impl_task_response {
75+
($($variant:ident),*) => {
76+
impl TaskResponse {
77+
pub fn get_widget_name(&self) -> WidgetName {
78+
match self {
79+
$(
80+
TaskResponse::$variant(Response { widget_name, .. }) => widget_name,
81+
)*
82+
}
83+
.clone()
84+
}
9485
}
95-
.clone()
96-
}
86+
};
9787
}
9888

89+
impl_task_response!(
90+
QuestionDetail,
91+
GetAllQuestionsMap,
92+
AllTopicTags,
93+
QuestionEditorData,
94+
RunResponseData,
95+
DbUpdateStatus,
96+
Error
97+
);
98+
9999
pub type ChannelRequestSender = tokio::sync::mpsc::UnboundedSender<TaskRequest>;
100100
pub type ChannelRequestReceiver = tokio::sync::mpsc::UnboundedReceiver<TaskRequest>;
101101

src/app_ui/helpers/tasks.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub async fn get_question_details(
1313
widget_name: WidgetName,
1414
slug: String,
1515
client: &reqwest::Client,
16+
_conn: &DatabaseConnection,
1617
) -> TaskResponse {
1718
match QuestionGQLQuery::new(slug).post(client).await {
1819
Ok(resp) => {
@@ -36,6 +37,7 @@ pub async fn get_editor_data(
3637
widget_name: WidgetName,
3738
slug: String,
3839
client: &reqwest::Client,
40+
_conn: &DatabaseConnection,
3941
) -> TaskResponse {
4042
match QuestionEditorDataQuery::new(slug).post(client).await {
4143
Ok(data) => TaskResponse::QuestionEditorData(Response {
@@ -54,6 +56,8 @@ pub async fn get_editor_data(
5456
pub async fn get_all_questions(
5557
request_id: String,
5658
widget_name: WidgetName,
59+
_content: (),
60+
_client: &reqwest::Client,
5761
conn: &DatabaseConnection,
5862
) -> TaskResponse {
5963
match TopicTagEntity::get_all_topic_questions_map(conn).await {
@@ -73,6 +77,8 @@ pub async fn get_all_questions(
7377
pub async fn get_all_topic_tags(
7478
request_id: String,
7579
widget_name: WidgetName,
80+
_content: (),
81+
_client: &reqwest::Client,
7682
conn: &DatabaseConnection,
7783
) -> TaskResponse {
7884
match TopicTagEntity::get_all_topics(conn).await {
@@ -94,6 +100,7 @@ pub async fn run_or_submit_question(
94100
widget_name: WidgetName,
95101
mut run_or_submit_code: graphql::RunOrSubmitCode,
96102
client: &reqwest::Client,
103+
_conn: &DatabaseConnection,
97104
) -> TaskResponse {
98105
if let RunOrSubmitCode::Run(RunCode {
99106
test_cases_stdin,
@@ -136,6 +143,7 @@ pub async fn update_status_to_accepted(
136143
request_id: String,
137144
widget_name: WidgetName,
138145
question: QuestionModel,
146+
_client: &reqwest::Client,
139147
db: &DatabaseConnection,
140148
) -> TaskResponse {
141149
let mut am = question.into_active_model();

0 commit comments

Comments
 (0)