summaryrefslogtreecommitdiff
path: root/src/app_state.rs
blob: fb4b92e79d192ebd3a071925891eabc8dd79f841 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use anyhow::{bail, Context, Result};
use crossterm::{
    style::Stylize,
    terminal::{Clear, ClearType},
    ExecutableCommand,
};
use std::{
    fs::{self, File},
    io::{Read, StdoutLock, Write},
};

use crate::{exercise::Exercise, info_file::ExerciseInfo, FENISH_LINE};

const STATE_FILE_NAME: &str = ".rustlings-state.txt";
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";

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

pub enum StateFileStatus {
    Read,
    NotRead,
}

pub struct AppState {
    current_exercise_ind: usize,
    exercises: Vec<Exercise>,
    n_done: u16,
    final_message: String,
    file_buf: Vec<u8>,
}

impl AppState {
    fn update_from_file(&mut self) -> StateFileStatus {
        self.file_buf.clear();
        self.n_done = 0;

        if File::open(STATE_FILE_NAME)
            .and_then(|mut file| file.read_to_end(&mut self.file_buf))
            .is_err()
        {
            return StateFileStatus::NotRead;
        }

        // See `Self::write` for more information about the file format.
        let mut lines = self.file_buf.split(|c| *c == b'\n');
        let Some(current_exercise_name) = lines.next() else {
            return StateFileStatus::NotRead;
        };

        if current_exercise_name.is_empty() || lines.next().is_none() {
            return StateFileStatus::NotRead;
        }

        let mut done_exercises = hashbrown::HashSet::with_capacity(self.exercises.len());

        for done_exerise_name in lines {
            if done_exerise_name.is_empty() {
                break;
            }
            done_exercises.insert(done_exerise_name);
        }

        for (ind, exercise) in self.exercises.iter_mut().enumerate() {
            if done_exercises.contains(exercise.name.as_bytes()) {
                exercise.done = true;
                self.n_done += 1;
            }

            if exercise.name.as_bytes() == current_exercise_name {
                self.current_exercise_ind = ind;
            }
        }

        StateFileStatus::Read
    }

    pub fn new(
        exercise_infos: Vec<ExerciseInfo>,
        final_message: String,
    ) -> (Self, StateFileStatus) {
        let exercises = exercise_infos
            .into_iter()
            .map(|mut exercise_info| {
                // Leaking to be able to borrow in the watch mode `Table`.
                // Leaking is not a problem because the `AppState` instance lives until
                // the end of the program.
                let path = exercise_info.path().leak();

                exercise_info.name.shrink_to_fit();
                let name = exercise_info.name.leak();

                let hint = exercise_info.hint.trim().to_owned();

                Exercise {
                    name,
                    path,
                    mode: exercise_info.mode,
                    hint,
                    done: false,
                }
            })
            .collect::<Vec<_>>();

        let mut slf = Self {
            current_exercise_ind: 0,
            exercises,
            n_done: 0,
            final_message,
            file_buf: Vec::with_capacity(2048),
        };

        let state_file_status = slf.update_from_file();

        (slf, state_file_status)
    }

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

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

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

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

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

        self.current_exercise_ind = ind;

        self.write()
    }

    pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
        // O(N) is fine since this method is used only once until the program exits.
        // Building a hashmap would have more overhead.
        self.current_exercise_ind = self
            .exercises
            .iter()
            .position(|exercise| exercise.name == name)
            .with_context(|| format!("No exercise found for '{name}'!"))?;

        self.write()
    }

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

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

        Ok(())
    }

    fn next_pending_exercise_ind(&self) -> Option<usize> {
        if self.current_exercise_ind == self.exercises.len() - 1 {
            // The last exercise is done.
            // Search for exercises not done from the start.
            return self.exercises[..self.current_exercise_ind]
                .iter()
                .position(|exercise| !exercise.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.exercises[self.current_exercise_ind + 1..]
            .iter()
            .position(|exercise| !exercise.done)
        {
            Some(ind) => Some(self.current_exercise_ind + 1 + ind),
            None => self.exercises[..self.current_exercise_ind]
                .iter()
                .position(|exercise| !exercise.done),
        }
    }

    pub fn done_current_exercise(&mut self, writer: &mut StdoutLock) -> Result<ExercisesProgress> {
        let exercise = &mut self.exercises[self.current_exercise_ind];
        if !exercise.done {
            exercise.done = true;
            self.n_done += 1;
        }

        let Some(ind) = self.next_pending_exercise_ind() else {
            writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;

            for (exercise_ind, exercise) in self.exercises().iter().enumerate() {
                writer.write_fmt(format_args!("Running {exercise} ... "))?;
                writer.flush()?;

                if !exercise.run()?.status.success() {
                    writer.write_fmt(format_args!("{}\n\n", "FAILED".red()))?;

                    self.current_exercise_ind = exercise_ind;

                    // No check if the exercise is done before setting it to pending
                    // because no pending exercise was found.
                    self.exercises[exercise_ind].done = false;
                    self.n_done -= 1;

                    self.write()?;

                    return Ok(ExercisesProgress::Pending);
                }

                writer.write_fmt(format_args!("{}\n", "ok".green()))?;
            }

            writer.execute(Clear(ClearType::All))?;
            writer.write_all(FENISH_LINE.as_bytes())?;
            writer.write_all(self.final_message.as_bytes())?;
            writer.write_all(b"\n")?;

            return Ok(ExercisesProgress::AllDone);
        };

        self.set_current_exercise_ind(ind)?;

        Ok(ExercisesProgress::Pending)
    }

    // Write the state file.
    // The file's format is very simple:
    // - The first line is the name of the current exercise. It must end with `\n` even if there
    // are no done exercises.
    // - The second line is an empty line.
    // - All remaining lines are the names of done exercises.
    fn write(&mut self) -> Result<()> {
        self.file_buf.clear();

        self.file_buf
            .extend_from_slice(self.current_exercise().name.as_bytes());
        self.file_buf.push(b'\n');

        for exercise in &self.exercises {
            if exercise.done {
                self.file_buf.push(b'\n');
                self.file_buf.extend_from_slice(exercise.name.as_bytes());
            }
        }

        fs::write(STATE_FILE_NAME, &self.file_buf)
            .with_context(|| format!("Failed to write the state file {STATE_FILE_NAME}"))?;

        Ok(())
    }
}

const RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
All exercises seem to be done.
Recompiling and running all exercises to make sure that all of them are actually done.

";