Skip to content

Commit 4bdd76e

Browse files
committed
fix various minor comilation errors
1 parent 87e1516 commit 4bdd76e

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) enum QuestionKind {
4040
Translate { from: Language, to: Language },
4141
}
4242

43-
#[derive(Serialize, Deserialize, PartialEq, Eq)]
43+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
4444
pub(crate) enum QuestionResult {
4545
/// User knew it.
4646
Yes,

src/quiz/mod.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ crate fn quiz(options: &MathemaOptions, language_str: &str, duration_min: i64) -
4444

4545
let mut presentation = presentation::basic();
4646

47-
let parentheticals = Regex::new(r"([^)]*)|\[[^]\]").unwrap();
47+
let parentheticals = Regex::new(r"\(.*\)").unwrap();
4848

49-
for (uuid, question_kind) in cards {
49+
let cards_len = cards.len();
50+
for ((uuid, question_kind), cards_remaining) in cards.into_iter().zip((1..=cards_len).rev()) {
5051
let quiz_duration = Utc::now().signed_duration_since(start_time);
5152
if quiz_duration > max_duration {
52-
match presentation.quiz_expired(Utc::now().signed_duration_since(original_start_time))? {
53+
match presentation.quiz_expired(
54+
Utc::now().signed_duration_since(original_start_time),
55+
cards_remaining,
56+
)? {
5357
None => break,
5458
Some(minutes) => {
5559
start_time = Utc::now();
@@ -59,8 +63,9 @@ crate fn quiz(options: &MathemaOptions, language_str: &str, duration_min: i64) -
5963
}
6064

6165
let card = repo.card(uuid);
62-
let mut expected_responses: Vec<_> = card.lines_with_kind(question_kind.response_line_kind())
63-
.collect();
66+
let mut expected_responses: Vec<_> = card.lines_with_kind(
67+
question_kind.response_line_kind(),
68+
).collect();
6469

6570
let prompt = Prompt {
6671
start_time,
@@ -73,16 +78,16 @@ crate fn quiz(options: &MathemaOptions, language_str: &str, duration_min: i64) -
7378

7479
let mut counter = 1;
7580
while let Some(user_response) = presentation.read_response(prompt, counter)? {
76-
counter += 1;
77-
7881
expected_responses.retain(|r| {
7982
let r = parentheticals.replace_all(r, "");
80-
r != user_response
83+
r.trim() != user_response
8184
});
8285

83-
if expected_responses.is_empty() {
86+
if counter >= expected_responses.len() {
8487
break;
8588
}
89+
90+
counter += 1;
8691
}
8792

8893
let result = if expected_responses.is_empty() {
@@ -91,10 +96,13 @@ crate fn quiz(options: &MathemaOptions, language_str: &str, duration_min: i64) -
9196
presentation.read_result(prompt, &expected_responses)?
9297
};
9398
let record = repo.database_mut().card_record_mut(uuid);
94-
record.push_question_record(question_kind, QuestionRecord {
95-
date: Utc::now(),
96-
result: result,
97-
});
99+
record.push_question_record(
100+
question_kind,
101+
QuestionRecord {
102+
date: Utc::now(),
103+
result: result,
104+
},
105+
);
98106

99107
presentation.cleanup();
100108
}

src/quiz/presentation/basic.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ impl Presentation for Basic {
2222
}
2323

2424
fn read_response(&mut self, prompt: Prompt<'_>, index: usize) -> Fallible<Option<String>> {
25-
print!("Response {}/{}: ", index, prompt.num_responses);
25+
println!("Response {}/{}: ", index, prompt.num_responses);
2626
let mut buffer = String::new();
2727
self.stdin.read_line(&mut buffer)?;
2828
let response_language = prompt.question_kind.response_language();
29-
let response = response_language.transliterate(&buffer);
30-
if response != buffer {
29+
let response = response_language.transliterate(buffer.trim());
30+
if response != buffer.trim() {
3131
println!(" (transliterated to `{}`)", response);
3232
}
3333
if response.is_empty() {
@@ -48,28 +48,42 @@ impl Presentation for Basic {
4848
}
4949

5050
loop {
51-
print!("Did you know it (yes/almost/no)? ");
51+
println!("Did you know it (yes/almost/no)? ");
5252
let mut buffer = String::new();
5353
self.stdin.read_line(&mut buffer)?;
54-
let buffer = buffer.to_lowercase();
54+
let buffer = buffer.trim().to_lowercase();
5555
match &buffer[..] {
5656
"yes" | "y" => return Ok(QuestionResult::Yes),
5757
"almost" | "a" => return Ok(QuestionResult::Almost),
5858
"no" | "n" => return Ok(QuestionResult::No),
59-
_ => { }
59+
_ => {}
6060
}
6161
}
6262
}
6363

6464
fn cleanup(&mut self) {
65+
println!();
66+
println!();
67+
println!();
6568
}
6669

67-
fn quiz_expired(&mut self, quiz_duration: Duration) -> Fallible<Option<i64>> {
70+
fn quiz_expired(
71+
&mut self,
72+
quiz_duration: Duration,
73+
remaining_cards: usize,
74+
) -> Fallible<Option<i64>> {
6875
println!("--------------------------------------------------");
69-
println!("{} minutes have expired since you started the quiz.", quiz_duration.num_minutes());
76+
println!(
77+
"{} minutes have expired since you started the quiz.",
78+
quiz_duration.num_minutes()
79+
);
80+
println!(
81+
"There are still {} cards left to go.",
82+
remaining_cards,
83+
);
7084
loop {
7185
println!("If you want to stop, press enter.");
72-
print!("Otherwise, type in how many more minutes: ");
86+
println!("Otherwise, type in how many more minutes: ");
7387
let mut buffer = String::new();
7488
self.stdin.read_line(&mut buffer)?;
7589
if buffer.is_empty() {
@@ -79,7 +93,7 @@ impl Presentation for Basic {
7993
Ok(v) if v >= 0 => {
8094
return Ok(Some(v));
8195
}
82-
_ => { }
96+
_ => {}
8397
}
8498
}
8599
}

src/quiz/presentation/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ crate trait Presentation {
1212

1313
/// Prompt user that `quiz_duration` time has been spent. Ask if
1414
/// they want to spend more time.
15-
fn quiz_expired(&mut self, quiz_duration: Duration) -> Fallible<Option<i64>>;
15+
fn quiz_expired(
16+
&mut self,
17+
quiz_duration: Duration,
18+
remaining_cards: usize,
19+
) -> Fallible<Option<i64>>;
1620
}
1721

1822
#[derive(Copy, Clone, Debug)]

0 commit comments

Comments
 (0)