From 979b4b4430807b07cc169ee1c369f8b954c20478 Mon Sep 17 00:00:00 2001 From: Kavika Date: Sat, 23 Nov 2024 00:44:08 +1100 Subject: [PATCH] change `QuestionType` `if`/`else` to `match` statement --- backend/server/src/models/answer.rs | 43 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/backend/server/src/models/answer.rs b/backend/server/src/models/answer.rs index 94336dc3..670a7ba8 100644 --- a/backend/server/src/models/answer.rs +++ b/backend/server/src/models/answer.rs @@ -358,27 +358,30 @@ impl AnswerData { multi_option_answers: Option>, ranking_answers: Option>, ) -> Self { - return if question_type == QuestionType::ShortAnswer { - let answer = short_answer_answer.expect("Data should exist for ShortAnswer variant"); - AnswerData::ShortAnswer(answer) - } else if question_type == QuestionType::MultiChoice - || question_type == QuestionType::MultiSelect - || question_type == QuestionType::DropDown - { - let options = - multi_option_answers.expect("Data should exist for MultiOptionData variants"); - - match question_type { - QuestionType::MultiChoice => AnswerData::MultiChoice(options[0]), - QuestionType::MultiSelect => AnswerData::MultiSelect(options), - QuestionType::DropDown => AnswerData::DropDown(options[0]), - _ => AnswerData::ShortAnswer("".to_string()), // Should never be reached, hence return ShortAnswer + return match question_type { + QuestionType::ShortAnswer => { + let answer = + short_answer_answer.expect("Data should exist for ShortAnswer variant"); + AnswerData::ShortAnswer(answer) + } + QuestionType::MultiChoice | QuestionType::MultiSelect | QuestionType::DropDown => { + let options = + multi_option_answers.expect("Data should exist for MultiOptionData variants"); + + match question_type { + QuestionType::MultiChoice => AnswerData::MultiChoice(options[0]), + QuestionType::MultiSelect => AnswerData::MultiSelect(options), + QuestionType::DropDown => AnswerData::DropDown(options[0]), + _ => AnswerData::ShortAnswer("".to_string()), // Should never be reached, hence return ShortAnswer + } + } + QuestionType::Ranking => { + let options = ranking_answers.expect("Data should exist for Ranking variant"); + AnswerData::Ranking(options) + } + _ => { + AnswerData::ShortAnswer("".to_string()) // Should never be reached, hence return ShortAnswer } - } else if question_type == QuestionType::Ranking { - let options = ranking_answers.expect("Data should exist for Ranking variant"); - AnswerData::Ranking(options) - } else { - AnswerData::ShortAnswer("".to_string()) // Should never be reached, hence return ShortAnswer }; }