summaryrefslogtreecommitdiff
path: root/src/app_state.rs
blob: 4a0912e4ed921460bb61b396de34a9eebd6c9e69 (plain)
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
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;

use crate::exercise::Exercise;

const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct StateFile {
    current_exercise_ind: usize,
    progress: Vec<bool>,
}

impl StateFile {
    fn read(exercises: &[Exercise]) -> Option<Self> {
        let file_content = fs::read(".rustlings-state.json").ok()?;

        let slf: Self = serde_json::de::from_slice(&file_content).ok()?;

        if slf.progress.len() != exercises.len() || slf.current_exercise_ind >= exercises.len() {
            return None;
        }

        Some(slf)
    }

    fn read_or_default(exercises: &[Exercise]) -> Self {
        Self::read(exercises).unwrap_or_else(|| Self {
            current_exercise_ind: 0,
            progress: vec![false; exercises.len()],
        })
    }

    fn write(&self) -> Result<()> {
        let mut buf = Vec::with_capacity(1024);
        serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state")?;
        fs::write(".rustlings-state.json", buf)
            .context("Failed to write the state file `.rustlings-state.json`")?;

        Ok(())
    }
}

pub struct AppState {
    state_file: StateFile,
    exercises: &'static [Exercise],
    n_done: u16,
    current_exercise: &'static Exercise,
}

#[must_use]
pub enum ExercisesProgress {
    AllDone,
    Pending,
}

impl AppState {
    pub fn new(exercises: Vec<Exercise>) -> Self {
        // Leaking for sending the exercises to the debounce event handler.
        // Leaking is not a problem since the exercises' slice is used until the end of the program.
        let exercises = exercises.leak();

        let state_file = StateFile::read_or_default(exercises);
        let n_done = state_file
            .progress
            .iter()
            .fold(0, |acc, done| acc + u16::from(*done));
        let current_exercise = &exercises[state_file.current_exercise_ind];

        Self {
            state_file,
            exercises,
            n_done,
            current_exercise,
        }
    }

    #[inline]
    pub fn current_exercise_ind(&self) -> usize {
        self.state_file.current_exercise_ind
    }

    #[inline]
    pub fn progress(&self) -> &[bool] {
        &self.state_file.progress
    }

    #[inline]
    pub fn exercises(&self) -> &'static [Exercise] {
        self.exercises
    }

    #[inline]
    pub fn n_done(&self) -> u16 {
        self.n_done
    }

    #[inline]
    pub fn current_exercise(&self) -> &'static Exercise {
        self.current_exercise
    }

    pub fn set_current_exercise_ind(&mut self, ind: usize) -> Result<()> {
        if ind >= self.exercises.len() {
            bail!(BAD_INDEX_ERR);
        }

        self.state_file.current_exercise_ind = ind;
        self.current_exercise = &self.exercises[ind];

        self.state_file.write()
    }

    pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
        let (ind, exercise) = self
            .exercises
            .iter()
            .enumerate()
            .find(|(_, exercise)| exercise.name == name)
            .with_context(|| format!("No exercise found for '{name}'!"))?;

        self.state_file.current_exercise_ind = ind;
        self.current_exercise = exercise;

        self.state_file.write()
    }

    pub fn set_pending(&mut self, ind: usize) -> Result<()> {
        let done = self
            .state_file
            .progress
            .get_mut(ind)
            .context(BAD_INDEX_ERR)?;

        if *done {
            *done = false;
            self.n_done -= 1;
            self.state_file.write()?;
        }

        Ok(())
    }

    fn next_exercise_ind(&self) -> Option<usize> {
        let current_ind = self.state_file.current_exercise_ind;

        if current_ind == self.state_file.progress.len() - 1 {
            // The last exercise is done.
            // Search for exercises not done from the start.
            return self.state_file.progress[..current_ind]
                .iter()
                .position(|done| !done);
        }

        // The done exercise isn't the last one.
        // Search for a pending exercise after the current one and then from the start.
        match self.state_file.progress[current_ind + 1..]
            .iter()
            .position(|done| !done)
        {
            Some(ind) => Some(current_ind + 1 + ind),
            None => self.state_file.progress[..current_ind]
                .iter()
                .position(|done| !done),
        }
    }

    pub fn done_current_exercise(&mut self) -> Result<ExercisesProgress> {
        let done = &mut self.state_file.progress[self.state_file.current_exercise_ind];
        if !*done {
            *done = true;
            self.n_done += 1;
        }

        let Some(ind) = self.next_exercise_ind() else {
            return Ok(ExercisesProgress::AllDone);
        };

        self.set_current_exercise_ind(ind)?;

        Ok(ExercisesProgress::Pending)
    }
}