Skip to content

Commit

Permalink
change QuestionType if/else to match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne committed Nov 22, 2024
1 parent 53cfc53 commit 979b4b4
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions backend/server/src/models/answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,27 +358,30 @@ impl AnswerData {
multi_option_answers: Option<Vec<i64>>,
ranking_answers: Option<Vec<i64>>,
) -> 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
};
}

Expand Down

0 comments on commit 979b4b4

Please sign in to comment.