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;

/// Enum representing the quality of an answer from 0 to 5.
/// - 0 - complete blackout.
/// - 1 - incorrect response; the correct one remembered
/// - 2 - incorrect response; where the correct one seemed easy to recall
/// - 3 - correct response recalled with serious difficulty
/// - 4 - correct response after a hesitation
/// - 5 - perfect response
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 {
    /// The maximum value for the quality of an answer is 5.
    /// This error is for when an answer above 5 is given.
    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 {
    /// Create a `Quality` object from a number between 0 and 5.
    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)),
        }
    }
}

/// A struct that holds the essential metadata for an item using the supermemo2 algorithm.
pub struct Item {
    /// The number of reviews of this item.
    repetitions: usize,
    /// Easiness factor.
    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 {
    /// Return an `Item` with no reviews and the default E-factor of 2.5.
    pub fn new() -> Self {
        Self {
            repetitions: 0,
            efactor: 2.5,
        }
    }

    /// Create an `Item` that has already been reviewed by specifying the
    /// number of repetitions done and the current E-factor.
    pub fn from(repetitions: usize, efactor: f64) -> Self {
        Self {
            repetitions,
            efactor,
        }
    }

    /// Returns the current interval of the card.
    /// The interval is defined as the time in days since the previous review after which
    /// this item will be due for review.
    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 {
        // EF':=EF+(0.1-(5-q)*(0.08+(5-q)*0.02))
        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;
        }
    }

    /// Returns a new `Item` for an answer to this `Item` with the provided `Quality`.
    pub fn answer(&self, quality: &Quality) -> Self {
        Self {
            repetitions: Self::new_repetitions(self.repetitions, quality),
            efactor: Self::new_efactor(self.efactor, quality),
        }
    }

    /// Updates an `Item` for an answer with the provided `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();
    }
}