mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
Compare commits
No commits in common. "0432e07864d5ee4d2bac1954c965b2077c0447c6" and "84a42a2b24687ed11f4d2a5c9b624d00b74de916" have entirely different histories.
0432e07864
...
84a42a2b24
86
Cargo.lock
generated
86
Cargo.lock
generated
|
@ -2,6 +2,18 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.15"
|
||||
|
@ -59,9 +71,9 @@ checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
|
|||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
|
@ -83,9 +95,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.20"
|
||||
version = "4.5.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
|
||||
checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
|
@ -93,9 +105,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.20"
|
||||
version = "4.5.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
|
||||
checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
|
@ -186,12 +198,6 @@ dependencies = [
|
|||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2"
|
||||
|
||||
[[package]]
|
||||
name = "fsevent-sys"
|
||||
version = "4.1.0"
|
||||
|
@ -203,9 +209,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.0"
|
||||
version = "0.14.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
|
||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
|
@ -221,9 +227,9 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
|||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.6.0"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
|
||||
checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
|
@ -371,9 +377,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.20.2"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "os_pipe"
|
||||
|
@ -410,9 +416,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.87"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
@ -428,9 +434,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.7"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f"
|
||||
checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b"
|
||||
dependencies = [
|
||||
"bitflags 2.6.0",
|
||||
]
|
||||
|
@ -452,10 +458,10 @@ dependencies = [
|
|||
name = "rustlings"
|
||||
version = "6.3.0"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"anyhow",
|
||||
"clap",
|
||||
"crossterm",
|
||||
"foldhash",
|
||||
"notify",
|
||||
"os_pipe",
|
||||
"rustix",
|
||||
|
@ -581,9 +587,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.79"
|
||||
version = "2.0.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
|
||||
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -592,9 +598,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.13.0"
|
||||
version = "3.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
|
||||
checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"fastrand",
|
||||
|
@ -637,6 +643,12 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
|
@ -840,3 +852,23 @@ checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
|
|||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -46,10 +46,10 @@ include = [
|
|||
]
|
||||
|
||||
[dependencies]
|
||||
ahash = { version = "0.8.11", default-features = false }
|
||||
anyhow = "1.0.89"
|
||||
clap = { version = "4.5.20", features = ["derive"] }
|
||||
clap = { version = "4.5.18", features = ["derive"] }
|
||||
crossterm = { version = "0.28.1", default-features = false, features = ["windows", "events"] }
|
||||
foldhash = "0.1.3"
|
||||
notify = { version = "6.1.1", default-features = false, features = ["macos_fsevent"] }
|
||||
os_pipe = "1.2.1"
|
||||
rustlings-macros = { path = "rustlings-macros", version = "=6.3.0" }
|
||||
|
@ -61,7 +61,7 @@ toml_edit.workspace = true
|
|||
rustix = { version = "0.38.37", default-features = false, features = ["std", "stdio", "termios"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.13.0"
|
||||
tempfile = "3.12.0"
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
|
|
|
@ -5,7 +5,7 @@ disallowed-types = [
|
|||
]
|
||||
|
||||
disallowed-methods = [
|
||||
# We use `foldhash` instead of the default hasher.
|
||||
# We use `ahash` instead of the default hasher.
|
||||
"std::collections::HashSet::new",
|
||||
"std::collections::HashSet::with_capacity",
|
||||
# Inefficient. Use `.queue(…)` instead.
|
||||
|
@ -13,6 +13,4 @@ disallowed-methods = [
|
|||
# Use `thread::Builder::spawn` instead and handle the error.
|
||||
"std::thread::spawn",
|
||||
"std::thread::Scope::spawn",
|
||||
# Return `ExitCode` instead.
|
||||
"std::process::exit",
|
||||
]
|
||||
|
|
215
src/app_state.rs
215
src/app_state.rs
|
@ -1,15 +1,10 @@
|
|||
use anyhow::{bail, Context, Error, Result};
|
||||
use crossterm::{cursor, terminal, QueueableCommand};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::{
|
||||
env,
|
||||
fs::{File, OpenOptions},
|
||||
io::{Read, Seek, StdoutLock, Write},
|
||||
io::{self, Read, Seek, StdoutLock, Write},
|
||||
path::{Path, MAIN_SEPARATOR_STR},
|
||||
process::{Command, Stdio},
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering::Relaxed},
|
||||
mpsc,
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
|
@ -20,11 +15,10 @@ use crate::{
|
|||
embedded::EMBEDDED_FILES,
|
||||
exercise::{Exercise, RunnableExercise},
|
||||
info_file::ExerciseInfo,
|
||||
term::{self, CheckProgressVisualizer},
|
||||
term,
|
||||
};
|
||||
|
||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||
const DEFAULT_CHECK_PARALLELISM: usize = 8;
|
||||
|
||||
#[must_use]
|
||||
pub enum ExercisesProgress {
|
||||
|
@ -41,12 +35,10 @@ pub enum StateFileStatus {
|
|||
NotRead,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum CheckProgress {
|
||||
None,
|
||||
Checking,
|
||||
Done,
|
||||
Pending,
|
||||
enum AllExercisesCheck {
|
||||
Pending(usize),
|
||||
AllDone,
|
||||
CheckedUntil(usize),
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
|
@ -148,11 +140,11 @@ impl AppState {
|
|||
|
||||
let mut done_exercises = hash_set_with_capacity(exercises.len());
|
||||
|
||||
for done_exercise_name in lines {
|
||||
if done_exercise_name.is_empty() {
|
||||
for done_exerise_name in lines {
|
||||
if done_exerise_name.is_empty() {
|
||||
break;
|
||||
}
|
||||
done_exercises.insert(done_exercise_name);
|
||||
done_exercises.insert(done_exerise_name);
|
||||
}
|
||||
|
||||
for (ind, exercise) in exercises.iter_mut().enumerate() {
|
||||
|
@ -202,11 +194,6 @@ impl AppState {
|
|||
self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn n_pending(&self) -> u16 {
|
||||
self.exercises.len() as u16 - self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn current_exercise(&self) -> &Exercise {
|
||||
&self.exercises[self.current_exercise_ind]
|
||||
|
@ -283,31 +270,15 @@ impl AppState {
|
|||
self.write()
|
||||
}
|
||||
|
||||
// Set the status of an exercise without saving. Returns `true` if the
|
||||
// status actually changed (and thus needs saving later).
|
||||
pub fn set_status(&mut self, exercise_ind: usize, done: bool) -> Result<bool> {
|
||||
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
|
||||
let exercise = self
|
||||
.exercises
|
||||
.get_mut(exercise_ind)
|
||||
.context(BAD_INDEX_ERR)?;
|
||||
|
||||
if exercise.done == done {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
exercise.done = done;
|
||||
if done {
|
||||
self.n_done += 1;
|
||||
} else {
|
||||
if exercise.done {
|
||||
exercise.done = false;
|
||||
self.n_done -= 1;
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
// Set the status of an exercise to "pending" and save.
|
||||
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
|
||||
if self.set_status(exercise_ind, false)? {
|
||||
self.write()?;
|
||||
}
|
||||
|
||||
|
@ -408,114 +379,63 @@ impl AppState {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||
let term_width = terminal::size()
|
||||
.context("Failed to get the terminal size")?
|
||||
.0;
|
||||
let mut progress_visualizer = CheckProgressVisualizer::build(stdout, term_width)?;
|
||||
// Return the exercise index of the first pending exercise found.
|
||||
fn check_all_exercises(&self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||
stdout.write_all(FINAL_CHECK_MSG)?;
|
||||
let n_exercises = self.exercises.len();
|
||||
|
||||
let next_exercise_ind = AtomicUsize::new(0);
|
||||
let mut progresses = vec![CheckProgress::None; self.exercises.len()];
|
||||
|
||||
thread::scope(|s| {
|
||||
let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel();
|
||||
let n_threads = thread::available_parallelism()
|
||||
.map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get());
|
||||
|
||||
for _ in 0..n_threads {
|
||||
let exercise_progress_sender = exercise_progress_sender.clone();
|
||||
let next_exercise_ind = &next_exercise_ind;
|
||||
let slf = &self;
|
||||
let status = thread::scope(|s| {
|
||||
let handles = self
|
||||
.exercises
|
||||
.iter()
|
||||
.map(|exercise| {
|
||||
thread::Builder::new()
|
||||
.spawn_scoped(s, move || loop {
|
||||
let exercise_ind = next_exercise_ind.fetch_add(1, Relaxed);
|
||||
let Some(exercise) = slf.exercises.get(exercise_ind) else {
|
||||
// No more exercises.
|
||||
break;
|
||||
};
|
||||
|
||||
if exercise_progress_sender
|
||||
.send((exercise_ind, CheckProgress::Checking))
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
};
|
||||
|
||||
let success = exercise.run_exercise(None, &slf.cmd_runner);
|
||||
let progress = match success {
|
||||
Ok(true) => CheckProgress::Done,
|
||||
Ok(false) => CheckProgress::Pending,
|
||||
Err(_) => CheckProgress::None,
|
||||
};
|
||||
|
||||
if exercise_progress_sender
|
||||
.send((exercise_ind, progress))
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
.spawn_scoped(s, || exercise.run_exercise(None, &self.cmd_runner))
|
||||
})
|
||||
.context("Failed to spawn a thread to check all exercises")?;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for (exercise_ind, spawn_res) in handles.into_iter().enumerate() {
|
||||
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
||||
stdout.flush()?;
|
||||
|
||||
let Ok(handle) = spawn_res else {
|
||||
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
||||
};
|
||||
|
||||
let Ok(success) = handle.join().unwrap() else {
|
||||
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
||||
};
|
||||
|
||||
if !success {
|
||||
return Ok(AllExercisesCheck::Pending(exercise_ind));
|
||||
}
|
||||
}
|
||||
|
||||
// Drop this sender to detect when the last thread is done.
|
||||
drop(exercise_progress_sender);
|
||||
|
||||
while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() {
|
||||
progresses[exercise_ind] = progress;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
}
|
||||
|
||||
Ok::<_, Error>(())
|
||||
Ok::<_, io::Error>(AllExercisesCheck::AllDone)
|
||||
})?;
|
||||
|
||||
let mut first_pending_exercise_ind = None;
|
||||
for exercise_ind in 0..progresses.len() {
|
||||
match progresses[exercise_ind] {
|
||||
CheckProgress::Done => {
|
||||
self.set_status(exercise_ind, true)?;
|
||||
}
|
||||
CheckProgress::Pending => {
|
||||
self.set_status(exercise_ind, false)?;
|
||||
if first_pending_exercise_ind.is_none() {
|
||||
first_pending_exercise_ind = Some(exercise_ind);
|
||||
}
|
||||
}
|
||||
CheckProgress::None | CheckProgress::Checking => {
|
||||
// If we got an error while checking all exercises in parallel,
|
||||
// it could be because we exceeded the limit of open file descriptors.
|
||||
// Therefore, try running exercises with errors sequentially.
|
||||
progresses[exercise_ind] = CheckProgress::Checking;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
let mut exercise_ind = match status {
|
||||
AllExercisesCheck::Pending(exercise_ind) => return Ok(Some(exercise_ind)),
|
||||
AllExercisesCheck::AllDone => return Ok(None),
|
||||
AllExercisesCheck::CheckedUntil(ind) => ind,
|
||||
};
|
||||
|
||||
// We got an error while checking all exercises in parallel.
|
||||
// This could be because we exceeded the limit of open file descriptors.
|
||||
// Therefore, try to continue the check sequentially.
|
||||
for exercise in &self.exercises[exercise_ind..] {
|
||||
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
||||
stdout.flush()?;
|
||||
|
||||
let exercise = &self.exercises[exercise_ind];
|
||||
let success = exercise.run_exercise(None, &self.cmd_runner)?;
|
||||
if success {
|
||||
progresses[exercise_ind] = CheckProgress::Done;
|
||||
} else {
|
||||
progresses[exercise_ind] = CheckProgress::Pending;
|
||||
if first_pending_exercise_ind.is_none() {
|
||||
first_pending_exercise_ind = Some(exercise_ind);
|
||||
}
|
||||
}
|
||||
self.set_status(exercise_ind, success)?;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
}
|
||||
}
|
||||
if !success {
|
||||
return Ok(Some(exercise_ind));
|
||||
}
|
||||
|
||||
self.write()?;
|
||||
|
||||
Ok(first_pending_exercise_ind)
|
||||
exercise_ind += 1;
|
||||
}
|
||||
|
||||
// Return the exercise index of the first pending exercise found.
|
||||
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||
stdout.queue(cursor::Hide)?;
|
||||
let res = self.check_all_exercises_impl(stdout);
|
||||
stdout.queue(cursor::Show)?;
|
||||
|
||||
res
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Mark the current exercise as done and move on to the next pending exercise if one exists.
|
||||
|
@ -542,18 +462,20 @@ impl AppState {
|
|||
stdout.write_all(b"\n")?;
|
||||
}
|
||||
|
||||
if let Some(first_pending_exercise_ind) = self.check_all_exercises(stdout)? {
|
||||
self.set_current_exercise_ind(first_pending_exercise_ind)?;
|
||||
if let Some(pending_exercise_ind) = self.check_all_exercises(stdout)? {
|
||||
stdout.write_all(b"\n\n")?;
|
||||
|
||||
self.current_exercise_ind = pending_exercise_ind;
|
||||
self.exercises[pending_exercise_ind].done = false;
|
||||
// All exercises were marked as done.
|
||||
self.n_done -= 1;
|
||||
self.write()?;
|
||||
return Ok(ExercisesProgress::NewPending);
|
||||
}
|
||||
|
||||
self.render_final_message(stdout)?;
|
||||
// Write that the last exercise is done.
|
||||
self.write()?;
|
||||
|
||||
Ok(ExercisesProgress::AllDone)
|
||||
}
|
||||
|
||||
pub fn render_final_message(&self, stdout: &mut StdoutLock) -> Result<()> {
|
||||
clear_terminal(stdout)?;
|
||||
stdout.write_all(FENISH_LINE.as_bytes())?;
|
||||
|
||||
|
@ -563,12 +485,15 @@ impl AppState {
|
|||
stdout.write_all(b"\n")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(ExercisesProgress::AllDone)
|
||||
}
|
||||
}
|
||||
|
||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
||||
const STATE_FILE_HEADER: &[u8] = b"DON'T EDIT THIS FILE!\n\n";
|
||||
const FINAL_CHECK_MSG: &[u8] = b"All exercises seem to be done.
|
||||
Recompiling and running all exercises to make sure that all of them are actually done.
|
||||
";
|
||||
const FENISH_LINE: &str = "+----------------------------------------------------+
|
||||
| You made it to the Fe-nish line! |
|
||||
+-------------------------- ------------------------+
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use foldhash::fast::FixedState;
|
||||
use ahash::AHasher;
|
||||
use std::hash::BuildHasherDefault;
|
||||
|
||||
/// DOS attacks aren't a concern for Rustlings. Therefore, we use `foldhash` with a fixed state.
|
||||
pub type HashSet<T> = std::collections::HashSet<T, FixedState>;
|
||||
/// DOS attacks aren't a concern for Rustlings. Therefore, we use `ahash` with fixed seeds.
|
||||
pub type HashSet<T> = std::collections::HashSet<T, BuildHasherDefault<AHasher>>;
|
||||
|
||||
#[inline]
|
||||
pub fn hash_set_with_capacity<T>(capacity: usize) -> HashSet<T> {
|
||||
HashSet::with_capacity_and_hasher(capacity, FixedState::default())
|
||||
HashSet::with_capacity_and_hasher(capacity, BuildHasherDefault::<AHasher>::default())
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ use crate::{
|
|||
CURRENT_FORMAT_VERSION,
|
||||
};
|
||||
|
||||
const MAX_N_EXERCISES: usize = 999;
|
||||
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
||||
|
||||
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
||||
|
@ -202,7 +201,7 @@ fn check_exercises_unsolved(
|
|||
|
||||
for (exercise_name, handle) in handles {
|
||||
let Ok(result) = handle.join() else {
|
||||
bail!("Panic while trying to run the exercise {exercise_name}");
|
||||
bail!("Panic while trying to run the exericse {exercise_name}");
|
||||
};
|
||||
|
||||
match result {
|
||||
|
@ -300,7 +299,7 @@ fn check_solutions(
|
|||
for (exercise_info, handle) in info_file.exercises.iter().zip(handles) {
|
||||
let Ok(check_result) = handle.join() else {
|
||||
bail!(
|
||||
"Panic while trying to run the solution of the exercise {}",
|
||||
"Panic while trying to run the solution of the exericse {}",
|
||||
exercise_info.name,
|
||||
);
|
||||
};
|
||||
|
@ -348,10 +347,6 @@ fn check_solutions(
|
|||
pub fn check(require_solutions: bool) -> Result<()> {
|
||||
let info_file = InfoFile::parse()?;
|
||||
|
||||
if info_file.exercises.len() > MAX_N_EXERCISES {
|
||||
bail!("The maximum number of exercises is {MAX_N_EXERCISES}");
|
||||
}
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
// A hack to make `cargo run -- dev check` work when developing Rustlings.
|
||||
check_cargo_toml(&info_file.exercises, "dev/Cargo.toml", b"../")?;
|
||||
|
|
50
src/main.rs
50
src/main.rs
|
@ -4,7 +4,7 @@ use clap::{Parser, Subcommand};
|
|||
use std::{
|
||||
io::{self, IsTerminal, Write},
|
||||
path::Path,
|
||||
process::ExitCode,
|
||||
process::exit,
|
||||
};
|
||||
use term::{clear_terminal, press_enter_prompt};
|
||||
|
||||
|
@ -47,8 +47,6 @@ enum Subcommands {
|
|||
/// The name of the exercise
|
||||
name: Option<String>,
|
||||
},
|
||||
/// Check all the exercises, marking them as done or pending accordingly.
|
||||
CheckAll,
|
||||
/// Reset a single exercise
|
||||
Reset {
|
||||
/// The name of the exercise
|
||||
|
@ -64,26 +62,22 @@ enum Subcommands {
|
|||
Dev(DevCommands),
|
||||
}
|
||||
|
||||
fn main() -> Result<ExitCode> {
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() {
|
||||
bail!("{OLD_METHOD_ERR}");
|
||||
}
|
||||
|
||||
'priority_cmd: {
|
||||
match args.command {
|
||||
Some(Subcommands::Init) => init::init().context("Initialization failed")?,
|
||||
Some(Subcommands::Dev(dev_command)) => dev_command.run()?,
|
||||
_ => break 'priority_cmd,
|
||||
}
|
||||
|
||||
return Ok(ExitCode::SUCCESS);
|
||||
Some(Subcommands::Init) => return init::init().context("Initialization failed"),
|
||||
Some(Subcommands::Dev(dev_command)) => return dev_command.run(),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if !Path::new("exercises").is_dir() {
|
||||
println!("{PRE_INIT_MSG}");
|
||||
return Ok(ExitCode::FAILURE);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
let info_file = InfoFile::parse()?;
|
||||
|
@ -142,35 +136,7 @@ fn main() -> Result<ExitCode> {
|
|||
if let Some(name) = name {
|
||||
app_state.set_current_exercise_by_name(&name)?;
|
||||
}
|
||||
return run::run(&mut app_state);
|
||||
}
|
||||
Some(Subcommands::CheckAll) => {
|
||||
let mut stdout = io::stdout().lock();
|
||||
if let Some(first_pending_exercise_ind) = app_state.check_all_exercises(&mut stdout)? {
|
||||
if app_state.current_exercise().done {
|
||||
app_state.set_current_exercise_ind(first_pending_exercise_ind)?;
|
||||
}
|
||||
|
||||
stdout.write_all(b"\n\n")?;
|
||||
let pending = app_state.n_pending();
|
||||
if pending == 1 {
|
||||
stdout.write_all(b"One exercise pending: ")?;
|
||||
} else {
|
||||
write!(
|
||||
stdout,
|
||||
"{pending}/{} exercises pending. The first: ",
|
||||
app_state.exercises().len(),
|
||||
)?;
|
||||
}
|
||||
app_state
|
||||
.current_exercise()
|
||||
.terminal_file_link(&mut stdout)?;
|
||||
stdout.write_all(b"\n")?;
|
||||
|
||||
return Ok(ExitCode::FAILURE);
|
||||
} else {
|
||||
app_state.render_final_message(&mut stdout)?;
|
||||
}
|
||||
run::run(&mut app_state)?;
|
||||
}
|
||||
Some(Subcommands::Reset { name }) => {
|
||||
app_state.set_current_exercise_by_name(&name)?;
|
||||
|
@ -187,7 +153,7 @@ fn main() -> Result<ExitCode> {
|
|||
Some(Subcommands::Init | Subcommands::Dev(_)) => (),
|
||||
}
|
||||
|
||||
Ok(ExitCode::SUCCESS)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const OLD_METHOD_ERR: &str =
|
||||
|
|
|
@ -5,7 +5,7 @@ use crossterm::{
|
|||
};
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
process::ExitCode,
|
||||
process::exit,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -13,7 +13,7 @@ use crate::{
|
|||
exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY},
|
||||
};
|
||||
|
||||
pub fn run(app_state: &mut AppState) -> Result<ExitCode> {
|
||||
pub fn run(app_state: &mut AppState) -> Result<()> {
|
||||
let exercise = app_state.current_exercise();
|
||||
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
||||
let success = exercise.run_exercise(Some(&mut output), app_state.cmd_runner())?;
|
||||
|
@ -29,8 +29,7 @@ pub fn run(app_state: &mut AppState) -> Result<ExitCode> {
|
|||
.current_exercise()
|
||||
.terminal_file_link(&mut stdout)?;
|
||||
stdout.write_all(b" with errors\n")?;
|
||||
|
||||
return Ok(ExitCode::FAILURE);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
stdout.queue(SetForegroundColor(Color::Green))?;
|
||||
|
@ -56,5 +55,5 @@ pub fn run(app_state: &mut AppState) -> Result<ExitCode> {
|
|||
ExercisesProgress::AllDone => (),
|
||||
}
|
||||
|
||||
Ok(ExitCode::SUCCESS)
|
||||
Ok(())
|
||||
}
|
||||
|
|
86
src/term.rs
86
src/term.rs
|
@ -1,6 +1,6 @@
|
|||
use crossterm::{
|
||||
cursor::MoveTo,
|
||||
style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
|
||||
style::{Attribute, Color, SetAttribute, SetForegroundColor},
|
||||
terminal::{Clear, ClearType},
|
||||
Command, QueueableCommand,
|
||||
};
|
||||
|
@ -9,8 +9,6 @@ use std::{
|
|||
io::{self, BufRead, StdoutLock, Write},
|
||||
};
|
||||
|
||||
use crate::app_state::CheckProgress;
|
||||
|
||||
pub struct MaxLenWriter<'a, 'b> {
|
||||
pub stdout: &'a mut StdoutLock<'b>,
|
||||
len: usize,
|
||||
|
@ -87,86 +85,14 @@ impl<'a> CountedWrite<'a> for StdoutLock<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct CheckProgressVisualizer<'a, 'b> {
|
||||
stdout: &'a mut StdoutLock<'b>,
|
||||
n_cols: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b> CheckProgressVisualizer<'a, 'b> {
|
||||
const CHECKING_COLOR: Color = Color::Blue;
|
||||
const DONE_COLOR: Color = Color::Green;
|
||||
const PENDING_COLOR: Color = Color::Red;
|
||||
|
||||
pub fn build(stdout: &'a mut StdoutLock<'b>, term_width: u16) -> io::Result<Self> {
|
||||
clear_terminal(stdout)?;
|
||||
stdout.write_all("Checking all exercises…\n".as_bytes())?;
|
||||
|
||||
// Legend
|
||||
stdout.write_all(b"Color of exercise number: ")?;
|
||||
stdout.queue(SetForegroundColor(Self::CHECKING_COLOR))?;
|
||||
stdout.write_all(b"Checking")?;
|
||||
stdout.queue(ResetColor)?;
|
||||
stdout.write_all(b" - ")?;
|
||||
stdout.queue(SetForegroundColor(Self::DONE_COLOR))?;
|
||||
stdout.write_all(b"Done")?;
|
||||
stdout.queue(ResetColor)?;
|
||||
stdout.write_all(b" - ")?;
|
||||
stdout.queue(SetForegroundColor(Self::PENDING_COLOR))?;
|
||||
stdout.write_all(b"Pending")?;
|
||||
stdout.queue(ResetColor)?;
|
||||
stdout.write_all(b"\n")?;
|
||||
|
||||
// Exercise numbers with up to 3 digits.
|
||||
// +1 because the last column doesn't end with a whitespace.
|
||||
let n_cols = usize::from(term_width + 1) / 4;
|
||||
|
||||
Ok(Self { stdout, n_cols })
|
||||
}
|
||||
|
||||
pub fn update(&mut self, progresses: &[CheckProgress]) -> io::Result<()> {
|
||||
self.stdout.queue(MoveTo(0, 2))?;
|
||||
|
||||
let mut exercise_num = 1;
|
||||
for exercise_progress in progresses {
|
||||
match exercise_progress {
|
||||
CheckProgress::None => (),
|
||||
CheckProgress::Checking => {
|
||||
self.stdout
|
||||
.queue(SetForegroundColor(Self::CHECKING_COLOR))?;
|
||||
}
|
||||
CheckProgress::Done => {
|
||||
self.stdout.queue(SetForegroundColor(Self::DONE_COLOR))?;
|
||||
}
|
||||
CheckProgress::Pending => {
|
||||
self.stdout.queue(SetForegroundColor(Self::PENDING_COLOR))?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(self.stdout, "{exercise_num:<3}")?;
|
||||
self.stdout.queue(ResetColor)?;
|
||||
|
||||
if exercise_num != progresses.len() {
|
||||
if exercise_num % self.n_cols == 0 {
|
||||
self.stdout.write_all(b"\n")?;
|
||||
} else {
|
||||
self.stdout.write_all(b" ")?;
|
||||
}
|
||||
|
||||
exercise_num += 1;
|
||||
}
|
||||
}
|
||||
|
||||
self.stdout.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/// Terminal progress bar to be used when not using Ratataui.
|
||||
pub fn progress_bar<'a>(
|
||||
writer: &mut impl CountedWrite<'a>,
|
||||
progress: u16,
|
||||
total: u16,
|
||||
term_width: u16,
|
||||
line_width: u16,
|
||||
) -> io::Result<()> {
|
||||
debug_assert!(total <= 999);
|
||||
debug_assert!(total < 1000);
|
||||
debug_assert!(progress <= total);
|
||||
|
||||
const PREFIX: &[u8] = b"Progress: [";
|
||||
|
@ -175,7 +101,7 @@ pub fn progress_bar<'a>(
|
|||
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
|
||||
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
|
||||
|
||||
if term_width < MIN_LINE_WIDTH {
|
||||
if line_width < MIN_LINE_WIDTH {
|
||||
writer.write_ascii(b"Progress: ")?;
|
||||
// Integers are in ASCII.
|
||||
return writer.write_ascii(format!("{progress}/{total}").as_bytes());
|
||||
|
@ -184,7 +110,7 @@ pub fn progress_bar<'a>(
|
|||
let stdout = writer.stdout();
|
||||
stdout.write_all(PREFIX)?;
|
||||
|
||||
let width = term_width - WRAPPER_WIDTH;
|
||||
let width = line_width - WRAPPER_WIDTH;
|
||||
let filled = (width * progress) / total;
|
||||
|
||||
stdout.queue(SetForegroundColor(Color::Green))?;
|
||||
|
|
|
@ -103,13 +103,6 @@ fn run_watch(
|
|||
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?,
|
||||
WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?,
|
||||
WatchEvent::Input(InputEvent::List) => return Ok(WatchExit::List),
|
||||
WatchEvent::Input(InputEvent::CheckAll) => match watch_state
|
||||
.check_all_exercises(&mut stdout)?
|
||||
{
|
||||
ExercisesProgress::AllDone => break,
|
||||
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,
|
||||
ExercisesProgress::CurrentPending => watch_state.render(&mut stdout)?,
|
||||
},
|
||||
WatchEvent::Input(InputEvent::Reset) => watch_state.reset_exercise(&mut stdout)?,
|
||||
WatchEvent::Input(InputEvent::Quit) => {
|
||||
stdout.write_all(QUIT_MSG)?;
|
||||
|
|
|
@ -157,9 +157,8 @@ impl<'a> WatchState<'a> {
|
|||
|
||||
/// Move on to the next exercise if the current one is done.
|
||||
pub fn next_exercise(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> {
|
||||
match self.done_status {
|
||||
DoneStatus::DoneWithSolution(_) | DoneStatus::DoneWithoutSolution => (),
|
||||
DoneStatus::Pending => return Ok(ExercisesProgress::CurrentPending),
|
||||
if self.done_status == DoneStatus::Pending {
|
||||
return Ok(ExercisesProgress::CurrentPending);
|
||||
}
|
||||
|
||||
self.app_state.done_current_exercise::<true>(stdout)
|
||||
|
@ -196,11 +195,6 @@ impl<'a> WatchState<'a> {
|
|||
stdout.queue(ResetColor)?;
|
||||
stdout.write_all(b":list / ")?;
|
||||
|
||||
stdout.queue(SetAttribute(Attribute::Bold))?;
|
||||
stdout.write_all(b"c")?;
|
||||
stdout.queue(ResetColor)?;
|
||||
stdout.write_all(b":check all / ")?;
|
||||
|
||||
stdout.queue(SetAttribute(Attribute::Bold))?;
|
||||
stdout.write_all(b"x")?;
|
||||
stdout.queue(ResetColor)?;
|
||||
|
@ -280,22 +274,6 @@ impl<'a> WatchState<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> {
|
||||
if let Some(first_pending_exercise_ind) = self.app_state.check_all_exercises(stdout)? {
|
||||
// Only change exercise if the current one is done.
|
||||
if self.app_state.current_exercise().done {
|
||||
self.app_state
|
||||
.set_current_exercise_ind(first_pending_exercise_ind)?;
|
||||
Ok(ExercisesProgress::NewPending)
|
||||
} else {
|
||||
Ok(ExercisesProgress::CurrentPending)
|
||||
}
|
||||
} else {
|
||||
self.app_state.render_final_message(stdout)?;
|
||||
Ok(ExercisesProgress::AllDone)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_term_width(&mut self, width: u16, stdout: &mut StdoutLock) -> io::Result<()> {
|
||||
if self.term_width != width {
|
||||
self.term_width = width;
|
||||
|
|
|
@ -11,7 +11,6 @@ pub enum InputEvent {
|
|||
Run,
|
||||
Hint,
|
||||
List,
|
||||
CheckAll,
|
||||
Reset,
|
||||
Quit,
|
||||
}
|
||||
|
@ -38,7 +37,6 @@ pub fn terminal_event_handler(
|
|||
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
||||
KeyCode::Char('h') => InputEvent::Hint,
|
||||
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
|
||||
KeyCode::Char('c') => InputEvent::CheckAll,
|
||||
KeyCode::Char('x') => {
|
||||
if sender.send(WatchEvent::Input(InputEvent::Reset)).is_err() {
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue