vocab/vocab_store/
guess.rs1use std::ops::Deref;
2
3use crate::Translation;
4
5pub enum Guess {
6 Local(Translation),
7 Foreign(Translation),
8}
9
10impl Guess {
11 pub fn render(&self) -> &str {
12 match self {
13 Guess::Local(translation) => translation.foreign.as_str(),
14 Guess::Foreign(translation) => translation.local.as_str(),
15 }
16 }
17
18 pub fn render_translation(&self) -> &str {
19 match self {
20 Guess::Local(translation) => translation.local.as_str(),
21 Guess::Foreign(translation) => translation.foreign.as_str(),
22 }
23 }
24
25 pub fn guess(&mut self, guess: &str) -> bool {
26 match self {
27 Guess::Local(ref mut translation) => translation.guess_local(guess),
28 Guess::Foreign(ref mut translation) => translation.guess_foreign(guess),
29 }
30 }
31}
32
33impl Deref for Guess {
34 type Target = Translation;
35
36 fn deref(&self) -> &Self::Target {
37 match self {
38 Guess::Local(translation) => translation,
39 Guess::Foreign(translation) => translation,
40 }
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::{Guess, Translation};
47
48 #[test]
49 fn test_render_local() {
50 let translation = Translation::new("yes", "はい");
51 let guess = Guess::Local(translation);
52 assert_eq!(guess.render(), "はい");
53 }
54
55 #[test]
56 fn test_render_foreign() {
57 let translation = Translation::new("yes", "はい");
58 let guess = Guess::Foreign(translation);
59 assert_eq!(guess.render(), "yes");
60 }
61
62 #[test]
63 fn test_render_translation_local() {
64 let translation = Translation::new("yes", "はい");
65 let guess = Guess::Local(translation);
66 assert_eq!(guess.render_translation(), "yes");
67 }
68
69 #[test]
70 fn test_render_translation_foreign() {
71 let translation = Translation::new("yes", "はい");
72 let guess = Guess::Foreign(translation);
73 assert_eq!(guess.render_translation(), "はい");
74 }
75
76 #[test]
77 fn test_guess_local() {
78 let translation = Translation::new("yes", "はい");
79 let mut guess = Guess::Local(translation);
80 assert!(guess.guess("yes"));
81 assert_eq!(guess.guesses_local_total, 1);
82 assert_eq!(guess.guesses_foreign_total, 0);
83 }
84
85 #[test]
86 fn test_guess_foreign() {
87 let translation = Translation::new("yes", "はい");
88 let mut guess = Guess::Foreign(translation);
89 assert!(guess.guess("はい"));
90 assert_eq!(guess.guesses_foreign_total, 1);
91 assert_eq!(guess.guesses_local_total, 0);
92 }
93}