1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::cmp::Ordering;
use std::error::Error as StdError;
use std::fmt;
pub enum Quality {
Zero,
One,
Two,
Three,
Four,
Five,
}
impl Ord for Quality {
fn cmp(&self, other: &Self) -> Ordering {
let a = match self {
Quality::Zero => 0,
Quality::One => 1,
Quality::Two => 2,
Quality::Three => 3,
Quality::Four => 4,
Quality::Five => 5,
};
let b = match other {
Quality::Zero => 0,
Quality::One => 1,
Quality::Two => 2,
Quality::Three => 3,
Quality::Four => 4,
Quality::Five => 5,
};
a.cmp(&b)
}
}
impl Eq for Quality {}
impl PartialOrd for Quality {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Quality {
fn eq(&self, other: &Self) -> bool {
self == other
}
}
impl From<&Quality> for f64 {
fn from(q: &Quality) -> Self {
match q {
Quality::Zero => 0.0,
Quality::One => 1.0,
Quality::Two => 2.0,
Quality::Three => 3.0,
Quality::Four => 4.0,
Quality::Five => 5.0,
}
}
}
#[derive(Debug)]
pub enum Error {
QualityAboveFiveError(usize),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::QualityAboveFiveError(q) => {
write!(f, "Quality cannot be greater than 5, {} was given", q)
}
}
}
}
impl StdError for Error {}
impl Quality {
pub fn from(q: usize) -> Result<Self, Error> {
match q {
0 => Ok(Self::Zero),
1 => Ok(Self::One),
2 => Ok(Self::Two),
3 => Ok(Self::Three),
4 => Ok(Self::Four),
5 => Ok(Self::Five),
_ => Err(Error::QualityAboveFiveError(q)),
}
}
}
pub struct Item {
repetitions: usize,
efactor: f64,
}
impl fmt::Display for Item {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Repetitions: {}, E-factor: {}",
self.repetitions, self.efactor
)
}
}
impl Item {
pub fn new() -> Self {
Self {
repetitions: 0,
efactor: 2.5,
}
}
pub fn from(repetitions: usize, efactor: f64) -> Self {
Self {
repetitions,
efactor,
}
}
pub fn interval(&self) -> usize {
match self.repetitions {
0 => 0,
1 => 1,
2 => 6,
_ => (6.0 * self.efactor.powi(self.repetitions as i32 - 2)).ceil() as usize,
}
}
fn new_efactor(mut efactor: f64, quality: &Quality) -> f64 {
let q = f64::from(quality);
if efactor < 1.3 {
efactor = 1.3;
}
efactor + (0.1 - (5.0 - q) * (0.08 + (5.0 - q) * 0.02))
}
fn new_repetitions(repetitions: usize, quality: &Quality) -> usize {
if quality < &Quality::Three {
return 1;
} else {
return repetitions + 1;
}
}
pub fn answer(&self, quality: &Quality) -> Self {
Self {
repetitions: Self::new_repetitions(self.repetitions, quality),
efactor: Self::new_efactor(self.efactor, quality),
}
}
pub fn answer_mut(&mut self, quality: &Quality) {
self.repetitions = Self::new_repetitions(self.repetitions, quality);
self.efactor = Self::new_efactor(self.efactor, quality);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn quality_above_5() {
Quality::from(6).unwrap();
}
}