rustlings/src/app_state.rs

377 lines
12 KiB
Rust
Raw Normal View History

use anyhow::{bail, Context, Result};
use crossterm::{
style::Stylize,
terminal::{Clear, ClearType},
ExecutableCommand,
};
2024-04-14 06:13:27 +03:00
use std::{
fs::{self, File},
io::{Read, StdoutLock, Write},
2024-04-18 13:41:17 +03:00
path::Path,
process::{Command, Stdio},
2024-04-14 06:13:27 +03:00
};
use crate::{embedded::EMBEDDED_FILES, exercise::Exercise, info_file::ExerciseInfo};
2024-04-14 06:13:27 +03:00
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
2024-04-14 02:15:43 +03:00
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
2024-04-12 21:06:56 +03:00
#[must_use]
pub enum ExercisesProgress {
AllDone,
Pending,
}
2024-04-14 17:03:49 +03:00
pub enum StateFileStatus {
Read,
NotRead,
}
pub struct AppState {
2024-04-14 02:15:43 +03:00
current_exercise_ind: usize,
exercises: Vec<Exercise>,
n_done: u16,
2024-04-14 02:15:43 +03:00
final_message: String,
2024-04-14 06:13:27 +03:00
file_buf: Vec<u8>,
2024-04-18 13:41:17 +03:00
official_exercises: bool,
}
impl AppState {
2024-04-14 17:03:49 +03:00
fn update_from_file(&mut self) -> StateFileStatus {
2024-04-14 06:13:27 +03: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 17:03:49 +03:00
.is_err()
2024-04-14 06:13:27 +03:00
{
2024-04-14 17:03:49 +03:00
return StateFileStatus::NotRead;
}
2024-04-14 06:13:27 +03:00
2024-04-14 17:03:49 +03:00
// See `Self::write` for more information about the file format.
2024-04-22 00:39:44 +03:00
let mut lines = self.file_buf.split(|c| *c == b'\n').skip(2);
2024-04-14 17:03:49 +03: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-14 06:13:27 +03:00
2024-04-14 17:03:49 +03:00
let mut done_exercises = hashbrown::HashSet::with_capacity(self.exercises.len());
2024-04-14 06:13:27 +03:00
2024-04-14 17:03:49 +03:00
for done_exerise_name in lines {
if done_exerise_name.is_empty() {
break;
2024-04-14 06:13:27 +03:00
}
2024-04-14 17:03:49 +03:00
done_exercises.insert(done_exerise_name);
}
2024-04-14 06:13:27 +03:00
2024-04-14 17:03:49 +03: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-14 06:13:27 +03:00
2024-04-14 17:03:49 +03:00
if exercise.name.as_bytes() == current_exercise_name {
self.current_exercise_ind = ind;
2024-04-14 06:13:27 +03:00
}
}
2024-04-14 17:03:49 +03:00
StateFileStatus::Read
2024-04-14 06:13:27 +03:00
}
2024-04-14 17:03:49 +03:00
pub fn new(
exercise_infos: Vec<ExerciseInfo>,
final_message: String,
) -> (Self, StateFileStatus) {
let exercises = exercise_infos
2024-04-14 02:15:43 +03:00
.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.
2024-04-14 03:41:19 +03:00
let path = exercise_info.path().leak();
2024-04-14 02:15:43 +03:00
exercise_info.name.shrink_to_fit();
let name = exercise_info.name.leak();
let dir = exercise_info.dir.map(|mut dir| {
dir.shrink_to_fit();
&*dir.leak()
});
2024-04-14 02:15:43 +03:00
let hint = exercise_info.hint.trim().to_owned();
Exercise {
dir,
2024-04-14 02:15:43 +03:00
name,
path,
mode: exercise_info.mode,
hint,
done: false,
}
})
.collect::<Vec<_>>();
2024-04-14 06:13:27 +03:00
let mut slf = Self {
current_exercise_ind: 0,
exercises,
2024-04-14 06:13:27 +03:00
n_done: 0,
2024-04-14 17:03:49 +03:00
final_message,
2024-04-14 06:13:27 +03:00
file_buf: Vec::with_capacity(2048),
2024-04-18 13:41:17 +03:00
official_exercises: !Path::new("info.toml").exists(),
2024-04-14 06:13:27 +03:00
};
2024-04-14 17:03:49 +03:00
let state_file_status = slf.update_from_file();
2024-04-14 06:13:27 +03:00
2024-04-14 17:03:49 +03:00
(slf, state_file_status)
}
#[inline]
pub fn current_exercise_ind(&self) -> usize {
2024-04-14 02:15:43 +03:00
self.current_exercise_ind
}
#[inline]
2024-04-14 02:15:43 +03:00
pub fn exercises(&self) -> &[Exercise] {
&self.exercises
}
#[inline]
pub fn n_done(&self) -> u16 {
self.n_done
}
#[inline]
2024-04-14 02:15:43 +03:00
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);
}
2024-04-14 02:15:43 +03:00
self.current_exercise_ind = ind;
2024-04-14 06:13:27 +03:00
self.write()
}
pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
2024-04-14 02:15:43 +03: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-14 02:15:43 +03:00
.position(|exercise| exercise.name == name)
.with_context(|| format!("No exercise found for '{name}'!"))?;
2024-04-14 06:13:27 +03:00
self.write()
}
pub fn set_pending(&mut self, ind: usize) -> Result<()> {
2024-04-14 02:15:43 +03:00
let exercise = self.exercises.get_mut(ind).context(BAD_INDEX_ERR)?;
if exercise.done {
exercise.done = false;
self.n_done -= 1;
2024-04-14 06:13:27 +03:00
self.write()?;
}
Ok(())
}
fn reset(&self, ind: usize, dir_name: Option<&str>, path: &str) -> Result<()> {
2024-04-18 13:41:17 +03:00
if self.official_exercises {
return EMBEDDED_FILES
.write_exercise_to_disk(
ind,
dir_name.context(
"Official exercises must be nested in the `exercises` directory",
)?,
path,
)
2024-04-18 13:41:17 +03: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();
self.reset(self.current_exercise_ind, exercise.dir, exercise.path)?;
2024-04-18 13:41:17 +03:00
Ok(exercise.path)
2024-04-18 13:41:17 +03: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];
self.reset(exercise_ind, exercise.dir, exercise.path)?;
2024-04-18 13:41:17 +03:00
Ok(exercise.path)
2024-04-18 13:41:17 +03:00
}
fn next_pending_exercise_ind(&self) -> Option<usize> {
2024-04-14 02:15:43 +03: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-14 02:15:43 +03:00
return self.exercises[..self.current_exercise_ind]
.iter()
2024-04-14 02:15:43 +03: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-14 02:15:43 +03:00
match self.exercises[self.current_exercise_ind + 1..]
.iter()
2024-04-14 02:15:43 +03:00
.position(|exercise| !exercise.done)
{
2024-04-14 02:15:43 +03:00
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
None => self.exercises[..self.current_exercise_ind]
.iter()
2024-04-14 02:15:43 +03:00
.position(|exercise| !exercise.done),
}
}
pub fn done_current_exercise(&mut self, writer: &mut StdoutLock) -> Result<ExercisesProgress> {
2024-04-14 02:15:43 +03:00
let exercise = &mut self.exercises[self.current_exercise_ind];
if !exercise.done {
exercise.done = true;
self.n_done += 1;
}
2024-04-24 01:47:46 +03:00
if self.official_exercises {
EMBEDDED_FILES.write_solution_to_disk(
self.current_exercise_ind,
exercise
.dir
.context("Official exercises must be nested in the `exercises` directory")?,
exercise.name,
)?;
}
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() {
2024-04-12 20:30:29 +03:00
writer.write_fmt(format_args!("{}\n\n", "FAILED".red()))?;
2024-04-14 02:15:43 +03:00
self.current_exercise_ind = exercise_ind;
// No check if the exercise is done before setting it to pending
// because no pending exercise was found.
2024-04-14 02:15:43 +03:00
self.exercises[exercise_ind].done = false;
self.n_done -= 1;
2024-04-14 06:13:27 +03:00
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())?;
2024-04-14 17:04:05 +03:00
let final_message = self.final_message.trim();
if !final_message.is_empty() {
2024-04-14 18:28:01 +03:00
writer.write_all(final_message.as_bytes())?;
2024-04-14 17:04:05 +03:00
writer.write_all(b"\n")?;
}
return Ok(ExercisesProgress::AllDone);
};
self.set_current_exercise_ind(ind)?;
Ok(ExercisesProgress::Pending)
}
2024-04-14 06:13:27 +03:00
// Write the state file.
// The file's format is very simple:
2024-04-22 00:39:44 +03:00
// - The first line is a comment.
2024-04-14 06:13:27 +03:00
// - The second line is an empty line.
2024-04-22 00:39:44 +03:00
// - 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.
2024-04-14 06:13:27 +03:00
// - All remaining lines are the names of done exercises.
fn write(&mut self) -> Result<()> {
self.file_buf.clear();
2024-04-22 00:39:44 +03:00
self.file_buf
.extend_from_slice(b"DON'T EDIT THIS FILE!\n\n");
2024-04-14 06:13:27 +03:00
self.file_buf
.extend_from_slice(self.current_exercise().name.as_bytes());
2024-04-14 17:03:49 +03:00
self.file_buf.push(b'\n');
2024-04-14 06:13:27 +03:00
for exercise in &self.exercises {
if exercise.done {
self.file_buf.push(b'\n');
2024-04-14 17:03:49 +03:00
self.file_buf.extend_from_slice(exercise.name.as_bytes());
2024-04-14 06:13:27 +03:00
}
}
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.
";
2024-04-18 12:21:39 +03:00
const FENISH_LINE: &str = "+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
\\/\x1b[31m
\x1b[0m
";