Use exercises as leaked

This commit is contained in:
mo8it 2024-04-09 21:16:27 +02:00
parent f0ce2c1afa
commit 787bec9875
5 changed files with 21 additions and 18 deletions

View file

@ -13,7 +13,7 @@ use crate::{exercise::Exercise, state_file::StateFile};
use self::state::{Filter, UiState}; use self::state::{Filter, UiState};
pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> { pub fn list(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<()> {
let mut stdout = io::stdout().lock(); let mut stdout = io::stdout().lock();
stdout.execute(EnterAlternateScreen)?; stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?; enable_raw_mode()?;

View file

@ -16,18 +16,18 @@ pub enum Filter {
None, None,
} }
pub struct UiState<'a> { pub struct UiState {
pub table: Table<'a>, pub table: Table<'static>,
pub message: String, pub message: String,
pub filter: Filter, pub filter: Filter,
exercises: &'a [Exercise], exercises: &'static [Exercise],
progress: u16, progress: u16,
selected: usize, selected: usize,
table_state: TableState, table_state: TableState,
last_ind: usize, last_ind: usize,
} }
impl<'a> UiState<'a> { impl UiState {
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self { pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let mut rows_counter: usize = 0; let mut rows_counter: usize = 0;
let mut progress: u16 = 0; let mut progress: u16 = 0;
@ -79,7 +79,7 @@ impl<'a> UiState<'a> {
self self
} }
pub fn new(state_file: &StateFile, exercises: &'a [Exercise]) -> Self { pub fn new(state_file: &StateFile, exercises: &'static [Exercise]) -> Self {
let header = Row::new(["Next", "State", "Name", "Path"]); let header = Row::new(["Next", "State", "Name", "Path"]);
let max_name_len = exercises let max_name_len = exercises

View file

@ -56,7 +56,7 @@ enum Subcommands {
List, List,
} }
fn find_exercise<'a>(name: &str, exercises: &'a [Exercise]) -> Result<(usize, &'a Exercise)> { fn find_exercise(name: &str, exercises: &'static [Exercise]) -> Result<(usize, &'static Exercise)> {
if name == "next" { if name == "next" {
for (ind, exercise) in exercises.iter().enumerate() { for (ind, exercise) in exercises.iter().enumerate() {
if !exercise.looks_done()? { if !exercise.looks_done()? {
@ -89,7 +89,7 @@ Try running `cargo --version` to diagnose the problem.",
let exercises = InfoFile::parse()?.exercises.leak(); let exercises = InfoFile::parse()?.exercises.leak();
if matches!(args.command, Some(Subcommands::Init)) { if matches!(args.command, Some(Subcommands::Init)) {
init::init(&exercises).context("Initialization failed")?; init::init(exercises).context("Initialization failed")?;
println!( println!(
"\nDone initialization!\n "\nDone initialization!\n
Run `cd rustlings` to go into the generated directory. Run `cd rustlings` to go into the generated directory.
@ -107,7 +107,7 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
exit(1); exit(1);
} }
let mut state_file = StateFile::read_or_default(&exercises); let mut state_file = StateFile::read_or_default(exercises);
match args.command { match args.command {
None | Some(Subcommands::Watch) => { None | Some(Subcommands::Watch) => {
@ -116,23 +116,23 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
// `Init` is handled above. // `Init` is handled above.
Some(Subcommands::Init) => (), Some(Subcommands::Init) => (),
Some(Subcommands::List) => { Some(Subcommands::List) => {
list::list(&mut state_file, &exercises)?; list::list(&mut state_file, exercises)?;
} }
Some(Subcommands::Run { name }) => { Some(Subcommands::Run { name }) => {
let (_, exercise) = find_exercise(&name, &exercises)?; let (_, exercise) = find_exercise(&name, exercises)?;
run(exercise).unwrap_or_else(|_| exit(1)); run(exercise).unwrap_or_else(|_| exit(1));
} }
Some(Subcommands::Reset { name }) => { Some(Subcommands::Reset { name }) => {
let (ind, exercise) = find_exercise(&name, &exercises)?; let (ind, exercise) = find_exercise(&name, exercises)?;
exercise.reset()?; exercise.reset()?;
state_file.reset(ind)?; state_file.reset(ind)?;
println!("The exercise {exercise} has been reset!"); println!("The exercise {exercise} has been reset!");
} }
Some(Subcommands::Hint { name }) => { Some(Subcommands::Hint { name }) => {
let (_, exercise) = find_exercise(&name, &exercises)?; let (_, exercise) = find_exercise(&name, exercises)?;
println!("{}", exercise.hint); println!("{}", exercise.hint);
} }
Some(Subcommands::Verify) => match verify(&exercises, 0)? { Some(Subcommands::Verify) => match verify(exercises, 0)? {
VerifyState::AllExercisesDone => println!("All exercises done!"), VerifyState::AllExercisesDone => println!("All exercises done!"),
VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"), VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"),
}, },

View file

@ -4,9 +4,9 @@ use std::io::{stdout, Write};
use crate::exercise::{Exercise, Mode, State}; use crate::exercise::{Exercise, Mode, State};
pub enum VerifyState<'a> { pub enum VerifyState {
AllExercisesDone, AllExercisesDone,
Failed(&'a Exercise), Failed(&'static Exercise),
} }
// Verify that the provided container of Exercise objects // Verify that the provided container of Exercise objects
@ -14,7 +14,10 @@ pub enum VerifyState<'a> {
// Any such failures will be reported to the end user. // Any such failures will be reported to the end user.
// If the Exercise being verified is a test, the verbose boolean // If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed. // determines whether or not the test harness outputs are displayed.
pub fn verify(exercises: &[Exercise], mut current_exercise_ind: usize) -> Result<VerifyState<'_>> { pub fn verify(
exercises: &'static [Exercise],
mut current_exercise_ind: usize,
) -> Result<VerifyState> {
while current_exercise_ind < exercises.len() { while current_exercise_ind < exercises.len() {
let exercise = &exercises[current_exercise_ind]; let exercise = &exercises[current_exercise_ind];

View file

@ -18,7 +18,7 @@ use crate::{
pub struct WatchState<'a> { pub struct WatchState<'a> {
writer: StdoutLock<'a>, writer: StdoutLock<'a>,
exercises: &'static [Exercise], exercises: &'static [Exercise],
exercise: &'a Exercise, exercise: &'static Exercise,
current_exercise_ind: usize, current_exercise_ind: usize,
stdout: Option<Vec<u8>>, stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>, stderr: Option<Vec<u8>>,