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
pub mod task_facade;

use crate::level::TaskLevel;
use std::{
    collections::BTreeSet,
    time::{Duration, SystemTime},
};

pub struct Task<Level>
where
    Level: TaskLevel,
{
    level: Level,
    last_repetition_time: SystemTime,
    description: String,
    correct_answers: BTreeSet<String>,
    explanation: Option<String>,
}

impl<Level> Task<Level>
where
    Level: TaskLevel,
{
    pub fn new(
        description: String,
        correct_answers: BTreeSet<String>,
        explanation: Option<String>,
    ) -> Self {
        Self {
            level: Level::default(),
            last_repetition_time: SystemTime::now(),
            description,
            correct_answers,
            explanation,
        }
    }

    pub const fn get_desctiption(&self) -> &String {
        &self.description
    }

    pub fn until_next_repetition(&self) -> Duration {
        (self.last_repetition_time + self.level.duration())
            .duration_since(SystemTime::now())
            .unwrap_or_default()
    }

    pub fn complete(
        &mut self,
        respondent: impl FnOnce(&String) -> String,
    ) -> Option<&Option<String>> {
        match self
            .correct_answers
            .contains(&respondent(&self.description))
        {
            true => {
                self.level.success();
                self.last_repetition_time = SystemTime::now();
                None
            }
            false => {
                self.level.failure();
                self.last_repetition_time = SystemTime::now();
                Some(&self.explanation)
            }
        }
    }
}