1
0
Fork 0
mirror of https://github.com/notohh/rustlings.git synced 2024-12-21 16:08:09 -05:00
rustlings/src/app_state.rs

489 lines
16 KiB
Rust
Raw Normal View History

2024-07-28 11:39:46 -04:00
use anyhow::{bail, Context, Error, Result};
2024-04-13 23:13:27 -04:00
use std::{
fs::{self, File},
io::{Read, StdoutLock, Write},
2024-08-01 09:23:54 -04:00
path::Path,
2024-04-18 06:41:17 -04:00
process::{Command, Stdio},
2024-07-28 11:39:46 -04:00
thread,
2024-04-13 23:13:27 -04:00
};
2024-04-24 19:56:01 -04:00
use crate::{
2024-04-29 19:41:08 -04:00
clear_terminal,
2024-08-01 09:23:54 -04:00
cmd::CmdRunner,
2024-04-24 19:56:01 -04:00
embedded::EMBEDDED_FILES,
2024-07-28 14:30:23 -04:00
exercise::{Exercise, RunnableExercise},
2024-04-24 19:56:01 -04:00
info_file::ExerciseInfo,
};
2024-04-13 23:13:27 -04:00
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
2024-04-13 19:15:43 -04:00
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
2024-04-12 14:06:56 -04:00
#[must_use]
pub enum ExercisesProgress {
2024-05-12 20:32:25 -04:00
// All exercises are done.
2024-04-12 14:06:56 -04:00
AllDone,
2024-05-12 20:32:25 -04:00
// The current exercise failed and is still pending.
CurrentPending,
// A new exercise is now pending.
NewPending,
2024-04-12 14:06:56 -04:00
}
2024-04-14 10:03:49 -04:00
pub enum StateFileStatus {
Read,
NotRead,
}
pub struct AppState {
2024-04-13 19:15:43 -04:00
current_exercise_ind: usize,
exercises: Vec<Exercise>,
2024-04-29 11:01:47 -04:00
// Caches the number of done exercises to avoid iterating over all exercises every time.
n_done: u16,
2024-04-13 19:15:43 -04:00
final_message: String,
2024-04-29 11:01:47 -04:00
// Preallocated buffer for reading and writing the state file.
2024-04-13 23:13:27 -04:00
file_buf: Vec<u8>,
2024-04-18 06:41:17 -04:00
official_exercises: bool,
2024-08-01 09:23:54 -04:00
cmd_runner: CmdRunner,
}
impl AppState {
2024-04-29 11:01:47 -04:00
// Update the app state from the state file.
2024-04-14 10:03:49 -04:00
fn update_from_file(&mut self) -> StateFileStatus {
2024-04-13 23:13:27 -04:00
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))
2024-04-14 10:03:49 -04:00
.is_err()
2024-04-13 23:13:27 -04:00
{
2024-04-14 10:03:49 -04:00
return StateFileStatus::NotRead;
}
2024-04-13 23:13:27 -04:00
2024-04-14 10:03:49 -04:00
// See `Self::write` for more information about the file format.
2024-04-21 17:39:44 -04:00
let mut lines = self.file_buf.split(|c| *c == b'\n').skip(2);
2024-04-14 10:03:49 -04:00
let Some(current_exercise_name) = lines.next() else {
return StateFileStatus::NotRead;
};
if current_exercise_name.is_empty() || lines.next().is_none() {
return StateFileStatus::NotRead;
}
2024-04-13 23:13:27 -04:00
2024-04-14 10:03:49 -04:00
let mut done_exercises = hashbrown::HashSet::with_capacity(self.exercises.len());
2024-04-13 23:13:27 -04:00
2024-04-14 10:03:49 -04:00
for done_exerise_name in lines {
if done_exerise_name.is_empty() {
break;
2024-04-13 23:13:27 -04:00
}
2024-04-14 10:03:49 -04:00
done_exercises.insert(done_exerise_name);
}
2024-04-13 23:13:27 -04:00
2024-04-14 10:03:49 -04:00
for (ind, exercise) in self.exercises.iter_mut().enumerate() {
if done_exercises.contains(exercise.name.as_bytes()) {
exercise.done = true;
self.n_done += 1;
}
2024-04-13 23:13:27 -04:00
2024-04-14 10:03:49 -04:00
if exercise.name.as_bytes() == current_exercise_name {
self.current_exercise_ind = ind;
2024-04-13 23:13:27 -04:00
}
}
2024-04-14 10:03:49 -04:00
StateFileStatus::Read
2024-04-13 23:13:27 -04:00
}
2024-04-14 10:03:49 -04:00
pub fn new(
exercise_infos: Vec<ExerciseInfo>,
final_message: String,
2024-04-27 11:31:51 -04:00
) -> Result<(Self, StateFileStatus)> {
2024-08-01 09:23:54 -04:00
let cmd_runner = CmdRunner::build()?;
2024-04-27 11:31:51 -04:00
2024-04-14 10:03:49 -04:00
let exercises = exercise_infos
2024-04-13 19:15:43 -04:00
.into_iter()
2024-05-13 15:36:20 -04:00
.map(|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();
let name = exercise_info.name.leak();
let dir = exercise_info.dir.map(|dir| &*dir.leak());
let hint = exercise_info.hint.trim().to_owned();
Exercise {
dir,
name,
path,
test: exercise_info.test,
strict_clippy: exercise_info.strict_clippy,
hint,
// Updated in `Self::update_from_file`.
done: false,
}
})
2024-04-13 19:15:43 -04:00
.collect::<Vec<_>>();
2024-04-13 23:13:27 -04:00
let mut slf = Self {
current_exercise_ind: 0,
exercises,
2024-04-13 23:13:27 -04:00
n_done: 0,
2024-04-14 10:03:49 -04:00
final_message,
2024-04-13 23:13:27 -04:00
file_buf: Vec::with_capacity(2048),
2024-04-18 06:41:17 -04:00
official_exercises: !Path::new("info.toml").exists(),
2024-08-01 09:23:54 -04:00
cmd_runner,
2024-04-13 23:13:27 -04:00
};
2024-04-14 10:03:49 -04:00
let state_file_status = slf.update_from_file();
2024-04-13 23:13:27 -04:00
2024-04-27 11:31:51 -04:00
Ok((slf, state_file_status))
}
#[inline]
pub fn current_exercise_ind(&self) -> usize {
2024-04-13 19:15:43 -04:00
self.current_exercise_ind
}
#[inline]
2024-04-13 19:15:43 -04:00
pub fn exercises(&self) -> &[Exercise] {
&self.exercises
}
#[inline]
pub fn n_done(&self) -> u16 {
self.n_done
}
#[inline]
2024-04-13 19:15:43 -04:00
pub fn current_exercise(&self) -> &Exercise {
&self.exercises[self.current_exercise_ind]
}
#[inline]
2024-08-01 09:23:54 -04:00
pub fn cmd_runner(&self) -> &CmdRunner {
&self.cmd_runner
}
2024-04-29 11:01:47 -04:00
// Write the state file.
// The file's format is very simple:
// - The first line is a comment.
// - The second line is an empty line.
// - The third line is the name of the current exercise. It must end with `\n` even if there
// are no done exercises.
// - The fourth 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(b"DON'T EDIT THIS FILE!\n\n");
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(())
}
2024-05-12 11:40:53 -04:00
pub fn set_current_exercise_ind(&mut self, exercise_ind: usize) -> Result<()> {
2024-05-13 11:06:11 -04:00
if exercise_ind == self.current_exercise_ind {
return Ok(());
}
2024-05-12 11:40:53 -04:00
if exercise_ind >= self.exercises.len() {
bail!(BAD_INDEX_ERR);
}
2024-05-12 11:40:53 -04:00
self.current_exercise_ind = exercise_ind;
2024-04-13 23:13:27 -04:00
self.write()
}
pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
2024-04-13 19:15:43 -04:00
// 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()
2024-04-13 19:15:43 -04:00
.position(|exercise| exercise.name == name)
.with_context(|| format!("No exercise found for '{name}'!"))?;
2024-04-13 23:13:27 -04:00
self.write()
}
2024-05-12 11:40:53 -04:00
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
let exercise = self
.exercises
.get_mut(exercise_ind)
.context(BAD_INDEX_ERR)?;
2024-04-13 19:15:43 -04:00
if exercise.done {
exercise.done = false;
self.n_done -= 1;
2024-04-13 23:13:27 -04:00
self.write()?;
}
Ok(())
}
2024-04-29 11:01:47 -04:00
// Official exercises: Dump the original file from the binary.
// Third-party exercises: Reset the exercise file with `git stash`.
2024-05-12 11:40:53 -04:00
fn reset(&self, exercise_ind: usize, path: &str) -> Result<()> {
2024-04-18 06:41:17 -04:00
if self.official_exercises {
return EMBEDDED_FILES
2024-05-12 11:40:53 -04:00
.write_exercise_to_disk(exercise_ind, path)
2024-04-18 06:41:17 -04:00
.with_context(|| format!("Failed to reset the exercise {path}"));
}
let output = Command::new("git")
.arg("stash")
.arg("push")
.arg("--")
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.output()
.with_context(|| format!("Failed to run `git stash push -- {path}`"))?;
if !output.status.success() {
bail!(
"`git stash push -- {path}` didn't run successfully: {}",
String::from_utf8_lossy(&output.stderr),
);
}
Ok(())
}
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
self.set_pending(self.current_exercise_ind)?;
let exercise = self.current_exercise();
2024-05-12 11:40:53 -04:00
self.reset(self.current_exercise_ind, exercise.path)?;
2024-04-18 06:41:17 -04:00
Ok(exercise.path)
2024-04-18 06:41:17 -04:00
}
pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> {
if exercise_ind >= self.exercises.len() {
bail!(BAD_INDEX_ERR);
}
self.set_pending(exercise_ind)?;
let exercise = &self.exercises[exercise_ind];
2024-05-12 11:40:53 -04:00
self.reset(exercise_ind, exercise.path)?;
2024-04-18 06:41:17 -04:00
Ok(exercise.path)
2024-04-18 06:41:17 -04:00
}
2024-04-29 11:01:47 -04:00
// Return the index of the next pending exercise or `None` if all exercises are done.
fn next_pending_exercise_ind(&self) -> Option<usize> {
2024-04-13 19:15:43 -04:00
if self.current_exercise_ind == self.exercises.len() - 1 {
// The last exercise is done.
// Search for exercises not done from the start.
2024-04-13 19:15:43 -04:00
return self.exercises[..self.current_exercise_ind]
.iter()
2024-04-13 19:15:43 -04:00
.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.
2024-04-13 19:15:43 -04:00
match self.exercises[self.current_exercise_ind + 1..]
.iter()
2024-04-13 19:15:43 -04:00
.position(|exercise| !exercise.done)
{
2024-04-13 19:15:43 -04:00
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
None => self.exercises[..self.current_exercise_ind]
.iter()
2024-04-13 19:15:43 -04:00
.position(|exercise| !exercise.done),
}
}
2024-05-13 15:40:40 -04:00
/// Official exercises: Dump the solution file form the binary and return its path.
/// Third-party exercises: Check if a solution file exists and return its path in that case.
2024-04-23 20:52:30 -04:00
pub fn current_solution_path(&self) -> Result<Option<String>> {
2024-08-01 09:23:54 -04:00
if cfg!(debug_assertions) {
2024-04-23 20:52:30 -04:00
return Ok(None);
}
2024-04-23 20:52:30 -04:00
let current_exercise = self.current_exercise();
if self.official_exercises {
2024-05-12 11:40:53 -04:00
EMBEDDED_FILES
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
.map(Some)
2024-04-23 20:52:30 -04:00
} else {
let solution_path = if let Some(dir) = current_exercise.dir {
format!("solutions/{dir}/{}.rs", current_exercise.name)
} else {
format!("solutions/{}.rs", current_exercise.name)
};
if Path::new(&solution_path).exists() {
return Ok(Some(solution_path));
}
Ok(None)
}
}
2024-05-13 15:40:40 -04:00
/// Mark the current exercise as done and move on to the next pending exercise if one exists.
/// If all exercises are marked as done, run all of them to make sure that they are actually
/// done. If an exercise which is marked as done fails, mark it as pending and continue on it.
2024-04-23 20:52:30 -04:00
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;
2024-04-23 18:47:46 -04:00
}
2024-04-29 11:01:47 -04:00
if let Some(ind) = self.next_pending_exercise_ind() {
self.set_current_exercise_ind(ind)?;
2024-05-12 20:32:25 -04:00
return Ok(ExercisesProgress::NewPending);
2024-04-29 11:01:47 -04:00
}
2024-04-12 13:30:29 -04:00
2024-04-29 11:01:47 -04:00
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
2024-07-28 11:39:46 -04:00
let n_exercises = self.exercises.len();
let pending_exercise_ind = thread::scope(|s| {
let handles = self
.exercises
.iter_mut()
.map(|exercise| {
s.spawn(|| {
2024-08-01 09:23:54 -04:00
let success = exercise.run_exercise(None, &self.cmd_runner)?;
2024-07-28 11:39:46 -04:00
exercise.done = success;
Ok::<_, Error>(success)
})
})
.collect::<Vec<_>>();
for (exercise_ind, handle) in handles.into_iter().enumerate() {
write!(writer, "\rProgress: {exercise_ind}/{n_exercises}")?;
writer.flush()?;
let success = handle.join().unwrap()?;
if !success {
writer.write_all(b"\n\n")?;
return Ok(Some(exercise_ind));
}
2024-04-14 10:04:05 -04:00
}
2024-07-28 11:39:46 -04:00
Ok::<_, Error>(None)
})?;
if let Some(pending_exercise_ind) = pending_exercise_ind {
self.current_exercise_ind = pending_exercise_ind;
self.n_done = self
.exercises
.iter()
.filter(|exercise| exercise.done)
.count() as u16;
self.write()?;
return Ok(ExercisesProgress::NewPending);
2024-04-29 11:01:47 -04:00
}
2024-04-13 23:13:27 -04:00
2024-07-07 07:55:39 -04:00
// Write that the last exercise is done.
self.write()?;
2024-04-29 19:41:08 -04:00
clear_terminal(writer)?;
2024-04-29 11:01:47 -04:00
writer.write_all(FENISH_LINE.as_bytes())?;
2024-04-13 23:13:27 -04:00
2024-04-29 11:01:47 -04:00
let final_message = self.final_message.trim();
if !final_message.is_empty() {
writer.write_all(final_message.as_bytes())?;
writer.write_all(b"\n")?;
2024-04-13 23:13:27 -04:00
}
2024-04-29 11:01:47 -04:00
Ok(ExercisesProgress::AllDone)
2024-04-13 23:13:27 -04:00
}
}
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.
";
2024-04-18 05:21:39 -04:00
const FENISH_LINE: &str = "+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
\\/\x1b[31m
\x1b[0m
";
2024-04-29 20:14:20 -04:00
#[cfg(test)]
mod tests {
use super::*;
fn dummy_exercise() -> Exercise {
Exercise {
dir: None,
name: "0",
path: "exercises/0.rs",
test: false,
strict_clippy: false,
hint: String::new(),
done: false,
}
}
#[test]
fn next_pending_exercise() {
let mut app_state = AppState {
current_exercise_ind: 0,
exercises: vec![dummy_exercise(), dummy_exercise(), dummy_exercise()],
n_done: 0,
final_message: String::new(),
file_buf: Vec::new(),
official_exercises: true,
2024-08-01 09:23:54 -04:00
cmd_runner: CmdRunner::build().unwrap(),
2024-04-29 20:14:20 -04:00
};
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
for (exercise, done) in app_state.exercises.iter_mut().zip(done) {
exercise.done = done;
}
for (ind, expected) in expected.into_iter().enumerate() {
app_state.current_exercise_ind = ind;
assert_eq!(
app_state.next_pending_exercise_ind(),
expected,
"done={done:?}, ind={ind}",
);
}
};
assert([true, true, true], [None, None, None]);
assert([false, false, false], [Some(1), Some(2), Some(0)]);
assert([false, true, true], [None, Some(0), Some(0)]);
assert([true, false, true], [Some(1), None, Some(1)]);
assert([true, true, false], [Some(2), Some(2), None]);
assert([true, false, false], [Some(1), Some(2), Some(1)]);
assert([false, true, false], [Some(2), Some(2), Some(0)]);
assert([false, false, true], [Some(1), Some(0), Some(0)]);
}
}