summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-09 21:16:27 +0200
committermo8it <mo8it@proton.me>2024-04-09 21:16:27 +0200
commit787bec9875ec3e76d5870808cc7299da1d26dea6 (patch)
tree79492b65170a6424fc3dd7669b02646cfb976d94 /src
parentf0ce2c1afa21fdaa34aed8f21c1ef4d3c47cebdd (diff)
Use exercises as leaked
Diffstat (limited to 'src')
-rw-r--r--src/list.rs2
-rw-r--r--src/list/state.rs10
-rw-r--r--src/main.rs16
-rw-r--r--src/verify.rs9
-rw-r--r--src/watch/state.rs2
5 files changed, 21 insertions, 18 deletions
diff --git a/src/list.rs b/src/list.rs
index db83ea4..c92b369 100644
--- a/src/list.rs
+++ b/src/list.rs
@@ -13,7 +13,7 @@ use crate::{exercise::Exercise, state_file::StateFile};
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();
stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?;
diff --git a/src/list/state.rs b/src/list/state.rs
index 7bfc163..b67c624 100644
--- a/src/list/state.rs
+++ b/src/list/state.rs
@@ -16,18 +16,18 @@ pub enum Filter {
None,
}
-pub struct UiState<'a> {
- pub table: Table<'a>,
+pub struct UiState {
+ pub table: Table<'static>,
pub message: String,
pub filter: Filter,
- exercises: &'a [Exercise],
+ exercises: &'static [Exercise],
progress: u16,
selected: usize,
table_state: TableState,
last_ind: usize,
}
-impl<'a> UiState<'a> {
+impl UiState {
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let mut rows_counter: usize = 0;
let mut progress: u16 = 0;
@@ -79,7 +79,7 @@ impl<'a> UiState<'a> {
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 max_name_len = exercises
diff --git a/src/main.rs b/src/main.rs
index 6af66bd..62bfd98 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -56,7 +56,7 @@ enum Subcommands {
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" {
for (ind, exercise) in exercises.iter().enumerate() {
if !exercise.looks_done()? {
@@ -89,7 +89,7 @@ Try running `cargo --version` to diagnose the problem.",
let exercises = InfoFile::parse()?.exercises.leak();
if matches!(args.command, Some(Subcommands::Init)) {
- init::init(&exercises).context("Initialization failed")?;
+ init::init(exercises).context("Initialization failed")?;
println!(
"\nDone initialization!\n
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);
}
- let mut state_file = StateFile::read_or_default(&exercises);
+ let mut state_file = StateFile::read_or_default(exercises);
match args.command {
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.
Some(Subcommands::Init) => (),
Some(Subcommands::List) => {
- list::list(&mut state_file, &exercises)?;
+ list::list(&mut state_file, exercises)?;
}
Some(Subcommands::Run { name }) => {
- let (_, exercise) = find_exercise(&name, &exercises)?;
+ let (_, exercise) = find_exercise(&name, exercises)?;
run(exercise).unwrap_or_else(|_| exit(1));
}
Some(Subcommands::Reset { name }) => {
- let (ind, exercise) = find_exercise(&name, &exercises)?;
+ let (ind, exercise) = find_exercise(&name, exercises)?;
exercise.reset()?;
state_file.reset(ind)?;
println!("The exercise {exercise} has been reset!");
}
Some(Subcommands::Hint { name }) => {
- let (_, exercise) = find_exercise(&name, &exercises)?;
+ let (_, exercise) = find_exercise(&name, exercises)?;
println!("{}", exercise.hint);
}
- Some(Subcommands::Verify) => match verify(&exercises, 0)? {
+ Some(Subcommands::Verify) => match verify(exercises, 0)? {
VerifyState::AllExercisesDone => println!("All exercises done!"),
VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"),
},
diff --git a/src/verify.rs b/src/verify.rs
index c4368cc..cea6bdf 100644
--- a/src/verify.rs
+++ b/src/verify.rs
@@ -4,9 +4,9 @@ use std::io::{stdout, Write};
use crate::exercise::{Exercise, Mode, State};
-pub enum VerifyState<'a> {
+pub enum VerifyState {
AllExercisesDone,
- Failed(&'a Exercise),
+ Failed(&'static Exercise),
}
// 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.
// If the Exercise being verified is a test, the verbose boolean
// 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() {
let exercise = &exercises[current_exercise_ind];
diff --git a/src/watch/state.rs b/src/watch/state.rs
index f614ae0..d8fed5b 100644
--- a/src/watch/state.rs
+++ b/src/watch/state.rs
@@ -18,7 +18,7 @@ use crate::{
pub struct WatchState<'a> {
writer: StdoutLock<'a>,
exercises: &'static [Exercise],
- exercise: &'a Exercise,
+ exercise: &'static Exercise,
current_exercise_ind: usize,
stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>,