mc_exam_randomizer/shuffler/
question.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
4pub struct Question {
5 pub text: String,
6 pub order: u32,
7 pub choices: Option<Choices>,
8 pub group: u32,
9}
10
11impl Question {
12 pub fn new(text: &str, order: u32, choices: Option<Choices>) -> Self {
13 Question {
14 text: String::from(text),
15 order,
16 choices,
17 group: 1,
18 }
19 }
20 pub fn from(text: &str, order: u32) -> Self {
21 Question {
22 text: String::from(text),
23 order,
24 choices: None,
25 group: 1,
26 }
27 }
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct Choices(
32 pub Vec<Choice>,
33 pub CorrectChoice,
34 pub Option<ChoiceOrdering>,
35);
36
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub struct Choice {
39 pub text: String,
40}
41impl Choice {
42 pub fn new(text: &str) -> Choice {
43 Choice {
44 text: String::from(text),
45 }
46 }
47}
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
49pub struct CorrectChoice(pub u32);
50
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub struct ChoiceOrdering(pub Vec<u32>);
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 fn question() -> Question {
58 Question::new(
59 "What is the meaning of life?",
60 1,
61 Some(Choices(
62 vec![
63 Choice::new("41"),
64 Choice::new("42"),
65 Choice::new("43"),
66 Choice::new("44"),
67 Choice::new("45"),
68 ],
69 CorrectChoice(1),
70 None,
71 )),
72 )
73 }
74
75 #[test]
76 fn question_text() {
77 let q = question();
78 assert_eq!(q.text, "What is the meaning of life?");
79 }
80
81 #[test]
82 fn question_oredr() {
83 let q = question();
84 assert_eq!(q.order, 1);
85 }
86 #[test]
87 fn question_choices() {
88 let q = question();
89 let opts = Some(Choices(
90 vec![
91 Choice::new("41"),
92 Choice::new("42"),
93 Choice::new("43"),
94 Choice::new("44"),
95 Choice::new("45"),
96 ],
97 CorrectChoice(1),
98 None,
99 ));
100 assert_eq!(q.choices, opts);
101 }
102
103 #[test]
104 fn question_from() {
105 let q = Question::from("question from", 2);
106 let q2 = Question {
107 text: String::from("question from"),
108 choices: None,
109 order: 2,
110 group: 1,
111 };
112 assert_eq!(q, q2);
113 }
114}