mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-10-22 00:00:03 +03:00
Compare commits
17 commits
45099dc919
...
f48a347c8b
Author | SHA1 | Date | |
---|---|---|---|
f48a347c8b | |||
dbbeb7d4ed | |||
bfa00ffbdc | |||
10eb1a3aee | |||
fd2bf9f6f6 | |||
fc1f9f0124 | |||
789492d1a9 | |||
afc320bed4 | |||
cba4a6f9c8 | |||
5556d42b46 | |||
7d2bc1c7a4 | |||
c209c874a9 | |||
59e8f70e55 | |||
4c8365fe88 | |||
52af0674c1 | |||
938b90e5f2 | |||
55cc8584bd |
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -203,9 +203,9 @@ checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "filetime"
|
name = "filetime"
|
||||||
version = "0.2.24"
|
version = "0.2.25"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bf401df4a4e3872c4fe8151134cf483738e74b67fc934d6532c882b3d24a4550"
|
checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
|
@ -469,9 +469,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "0.38.34"
|
version = "0.38.35"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
|
checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.6.0",
|
||||||
"errno",
|
"errno",
|
||||||
|
|
|
@ -116,6 +116,8 @@ bin = [
|
||||||
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
|
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
|
||||||
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
|
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
|
||||||
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
|
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
|
||||||
|
{ name = "generics3", path = "../exercises/14_generics/generics3.rs" },
|
||||||
|
{ name = "generics3_sol", path = "../solutions/14_generics/generics3.rs" },
|
||||||
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
|
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
|
||||||
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
|
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
|
||||||
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },
|
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },
|
||||||
|
|
54
exercises/14_generics/generics3.rs
Normal file
54
exercises/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// This function should take an array of `Option` elements and returns array of not None elements
|
||||||
|
// TODO fix this function signature
|
||||||
|
fn into_dispose_nulls(list: Vec<Option<&str>>) -> Vec<&str> {
|
||||||
|
list.into_iter().flatten().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_str_on_list() {
|
||||||
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
|
let only_values = into_dispose_nulls(names_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_custom_type_on_list() {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct Rectangle {
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
}
|
||||||
|
impl Rectangle {
|
||||||
|
fn new(width: i32, height: i32) -> Self {
|
||||||
|
Self { width, height }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let custom_list = vec![
|
||||||
|
Some(Rectangle::new(1, 2)),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(Rectangle::new(3, 4)),
|
||||||
|
];
|
||||||
|
let only_values = into_dispose_nulls(custom_list);
|
||||||
|
assert_eq!(only_values.len(), 2);
|
||||||
|
}
|
||||||
|
}
|
|
@ -744,6 +744,17 @@ hint = """
|
||||||
Related section in The Book:
|
Related section in The Book:
|
||||||
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
|
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "generics3"
|
||||||
|
dir = "14_generics"
|
||||||
|
hint = """
|
||||||
|
Vectors in Rust use generics to create dynamically-sized arrays of any type.
|
||||||
|
The `into_dispose_nulls` function takes a vector as an argument, but only accepts vectors that store the &str type.
|
||||||
|
To allow the function to accept vectors that store any type, you can leverage your knowledge about generics.
|
||||||
|
If you're unsure how to proceed, please refer to the Rust Book at:
|
||||||
|
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-function-definitions.
|
||||||
|
"""
|
||||||
|
|
||||||
# TRAITS
|
# TRAITS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|
53
solutions/14_generics/generics3.rs
Normal file
53
solutions/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// Here we added generic type `T` to function signature
|
||||||
|
// Now this function can be used with vector of any
|
||||||
|
fn into_dispose_nulls<T>(list: Vec<Option<T>>) -> Vec<T> {
|
||||||
|
list.into_iter().flatten().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_str_on_list() {
|
||||||
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
|
let only_values = into_dispose_nulls(names_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_custom_type_on_list() {
|
||||||
|
struct Rectangle {
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
}
|
||||||
|
impl Rectangle {
|
||||||
|
fn new(width: i32, height: i32) -> Self {
|
||||||
|
Self { width, height }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let custom_list = vec![
|
||||||
|
Some(Rectangle::new(1, 2)),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(Rectangle::new(3, 4)),
|
||||||
|
];
|
||||||
|
let only_values = into_dispose_nulls(custom_list);
|
||||||
|
assert_eq!(only_values.len(), 2);
|
||||||
|
}
|
||||||
|
}
|
292
src/app_state.rs
292
src/app_state.rs
|
@ -1,7 +1,8 @@
|
||||||
use anyhow::{bail, Context, Error, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, File},
|
env,
|
||||||
io::{Read, StdoutLock, Write},
|
fs::{File, OpenOptions},
|
||||||
|
io::{self, Read, Seek, StdoutLock, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
thread,
|
thread,
|
||||||
|
@ -17,7 +18,6 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub enum ExercisesProgress {
|
pub enum ExercisesProgress {
|
||||||
|
@ -34,72 +34,44 @@ pub enum StateFileStatus {
|
||||||
NotRead,
|
NotRead,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum AllExercisesCheck {
|
||||||
|
Pending(usize),
|
||||||
|
AllDone,
|
||||||
|
CheckedUntil(usize),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
current_exercise_ind: usize,
|
current_exercise_ind: usize,
|
||||||
exercises: Vec<Exercise>,
|
exercises: Vec<Exercise>,
|
||||||
// Caches the number of done exercises to avoid iterating over all exercises every time.
|
// Caches the number of done exercises to avoid iterating over all exercises every time.
|
||||||
n_done: u16,
|
n_done: u16,
|
||||||
final_message: String,
|
final_message: String,
|
||||||
|
state_file: File,
|
||||||
// Preallocated buffer for reading and writing the state file.
|
// Preallocated buffer for reading and writing the state file.
|
||||||
file_buf: Vec<u8>,
|
file_buf: Vec<u8>,
|
||||||
official_exercises: bool,
|
official_exercises: bool,
|
||||||
cmd_runner: CmdRunner,
|
cmd_runner: CmdRunner,
|
||||||
|
// Running in VS Code.
|
||||||
|
vs_code: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
// Update the app state from the state file.
|
|
||||||
fn update_from_file(&mut self) -> StateFileStatus {
|
|
||||||
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))
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
return StateFileStatus::NotRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
// See `Self::write` for more information about the file format.
|
|
||||||
let mut lines = self.file_buf.split(|c| *c == b'\n').skip(2);
|
|
||||||
|
|
||||||
let Some(current_exercise_name) = lines.next() else {
|
|
||||||
return StateFileStatus::NotRead;
|
|
||||||
};
|
|
||||||
|
|
||||||
if current_exercise_name.is_empty() || lines.next().is_none() {
|
|
||||||
return StateFileStatus::NotRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut done_exercises = hash_set_with_capacity(self.exercises.len());
|
|
||||||
|
|
||||||
for done_exerise_name in lines {
|
|
||||||
if done_exerise_name.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
done_exercises.insert(done_exerise_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ind, exercise) in self.exercises.iter_mut().enumerate() {
|
|
||||||
if done_exercises.contains(exercise.name.as_bytes()) {
|
|
||||||
exercise.done = true;
|
|
||||||
self.n_done += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if exercise.name.as_bytes() == current_exercise_name {
|
|
||||||
self.current_exercise_ind = ind;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StateFileStatus::Read
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
exercise_infos: Vec<ExerciseInfo>,
|
exercise_infos: Vec<ExerciseInfo>,
|
||||||
final_message: String,
|
final_message: String,
|
||||||
) -> Result<(Self, StateFileStatus)> {
|
) -> Result<(Self, StateFileStatus)> {
|
||||||
let cmd_runner = CmdRunner::build()?;
|
let cmd_runner = CmdRunner::build()?;
|
||||||
|
let mut state_file = OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.truncate(false)
|
||||||
|
.open(STATE_FILE_NAME)
|
||||||
|
.with_context(|| {
|
||||||
|
format!("Failed to open or create the state file {STATE_FILE_NAME}")
|
||||||
|
})?;
|
||||||
|
|
||||||
let exercises = exercise_infos
|
let mut exercises = exercise_infos
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|exercise_info| {
|
.map(|exercise_info| {
|
||||||
// Leaking to be able to borrow in the watch mode `Table`.
|
// Leaking to be able to borrow in the watch mode `Table`.
|
||||||
|
@ -117,23 +89,68 @@ impl AppState {
|
||||||
test: exercise_info.test,
|
test: exercise_info.test,
|
||||||
strict_clippy: exercise_info.strict_clippy,
|
strict_clippy: exercise_info.strict_clippy,
|
||||||
hint,
|
hint,
|
||||||
// Updated in `Self::update_from_file`.
|
// Updated below.
|
||||||
done: false,
|
done: false,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut slf = Self {
|
let mut current_exercise_ind = 0;
|
||||||
current_exercise_ind: 0,
|
let mut n_done = 0;
|
||||||
exercises,
|
let mut file_buf = Vec::with_capacity(2048);
|
||||||
n_done: 0,
|
let state_file_status = 'block: {
|
||||||
final_message,
|
if state_file.read_to_end(&mut file_buf).is_err() {
|
||||||
file_buf: Vec::with_capacity(2048),
|
break 'block StateFileStatus::NotRead;
|
||||||
official_exercises: !Path::new("info.toml").exists(),
|
}
|
||||||
cmd_runner,
|
|
||||||
|
// See `Self::write` for more information about the file format.
|
||||||
|
let mut lines = file_buf.split(|c| *c == b'\n').skip(2);
|
||||||
|
|
||||||
|
let Some(current_exercise_name) = lines.next() else {
|
||||||
|
break 'block StateFileStatus::NotRead;
|
||||||
};
|
};
|
||||||
|
|
||||||
let state_file_status = slf.update_from_file();
|
if current_exercise_name.is_empty() || lines.next().is_none() {
|
||||||
|
break 'block StateFileStatus::NotRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut done_exercises = hash_set_with_capacity(exercises.len());
|
||||||
|
|
||||||
|
for done_exerise_name in lines {
|
||||||
|
if done_exerise_name.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
done_exercises.insert(done_exerise_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ind, exercise) in exercises.iter_mut().enumerate() {
|
||||||
|
if done_exercises.contains(exercise.name.as_bytes()) {
|
||||||
|
exercise.done = true;
|
||||||
|
n_done += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if exercise.name.as_bytes() == current_exercise_name {
|
||||||
|
current_exercise_ind = ind;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StateFileStatus::Read
|
||||||
|
};
|
||||||
|
|
||||||
|
file_buf.clear();
|
||||||
|
file_buf.extend_from_slice(STATE_FILE_HEADER);
|
||||||
|
|
||||||
|
let slf = Self {
|
||||||
|
current_exercise_ind,
|
||||||
|
exercises,
|
||||||
|
n_done,
|
||||||
|
final_message,
|
||||||
|
state_file,
|
||||||
|
file_buf,
|
||||||
|
official_exercises: !Path::new("info.toml").exists(),
|
||||||
|
cmd_runner,
|
||||||
|
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
|
||||||
|
};
|
||||||
|
|
||||||
Ok((slf, state_file_status))
|
Ok((slf, state_file_status))
|
||||||
}
|
}
|
||||||
|
@ -163,6 +180,11 @@ impl AppState {
|
||||||
&self.cmd_runner
|
&self.cmd_runner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn vs_code(&self) -> bool {
|
||||||
|
self.vs_code
|
||||||
|
}
|
||||||
|
|
||||||
// Write the state file.
|
// Write the state file.
|
||||||
// The file's format is very simple:
|
// The file's format is very simple:
|
||||||
// - The first line is a comment.
|
// - The first line is a comment.
|
||||||
|
@ -172,10 +194,8 @@ impl AppState {
|
||||||
// - The fourth line is an empty line.
|
// - The fourth line is an empty line.
|
||||||
// - All remaining lines are the names of done exercises.
|
// - All remaining lines are the names of done exercises.
|
||||||
fn write(&mut self) -> Result<()> {
|
fn write(&mut self) -> Result<()> {
|
||||||
self.file_buf.clear();
|
self.file_buf.truncate(STATE_FILE_HEADER.len());
|
||||||
|
|
||||||
self.file_buf
|
|
||||||
.extend_from_slice(b"DON'T EDIT THIS FILE!\n\n");
|
|
||||||
self.file_buf
|
self.file_buf
|
||||||
.extend_from_slice(self.current_exercise().name.as_bytes());
|
.extend_from_slice(self.current_exercise().name.as_bytes());
|
||||||
self.file_buf.push(b'\n');
|
self.file_buf.push(b'\n');
|
||||||
|
@ -187,7 +207,14 @@ impl AppState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::write(STATE_FILE_NAME, &self.file_buf)
|
self.state_file
|
||||||
|
.rewind()
|
||||||
|
.with_context(|| format!("Failed to rewind the state file {STATE_FILE_NAME}"))?;
|
||||||
|
self.state_file
|
||||||
|
.set_len(0)
|
||||||
|
.with_context(|| format!("Failed to truncate the state file {STATE_FILE_NAME}"))?;
|
||||||
|
self.state_file
|
||||||
|
.write_all(&self.file_buf)
|
||||||
.with_context(|| format!("Failed to write the state file {STATE_FILE_NAME}"))?;
|
.with_context(|| format!("Failed to write the state file {STATE_FILE_NAME}"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -286,25 +313,22 @@ impl AppState {
|
||||||
|
|
||||||
// Return the index of the next pending exercise or `None` if all exercises are done.
|
// Return the index of the next pending exercise or `None` if all exercises are done.
|
||||||
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
||||||
if self.current_exercise_ind == self.exercises.len() - 1 {
|
let next_ind = self.current_exercise_ind + 1;
|
||||||
// The last exercise is done.
|
self.exercises
|
||||||
// Search for exercises not done from the start.
|
// If the exercise done isn't the last, search for pending exercises after it.
|
||||||
return self.exercises[..self.current_exercise_ind]
|
.get(next_ind..)
|
||||||
.iter()
|
.and_then(|later_exercises| {
|
||||||
.position(|exercise| !exercise.done);
|
later_exercises
|
||||||
}
|
|
||||||
|
|
||||||
// The done exercise isn't the last one.
|
|
||||||
// Search for a pending exercise after the current one and then from the start.
|
|
||||||
match self.exercises[self.current_exercise_ind + 1..]
|
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exercise| !exercise.done)
|
.position(|exercise| !exercise.done)
|
||||||
{
|
.map(|ind| next_ind + ind)
|
||||||
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
|
})
|
||||||
None => self.exercises[..self.current_exercise_ind]
|
// Search from the start.
|
||||||
|
.or_else(|| {
|
||||||
|
self.exercises[..self.current_exercise_ind]
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exercise| !exercise.done),
|
.position(|exercise| !exercise.done)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Official exercises: Dump the solution file form the binary and return its path.
|
/// Official exercises: Dump the solution file form the binary and return its path.
|
||||||
|
@ -321,20 +345,68 @@ impl AppState {
|
||||||
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
||||||
.map(Some)
|
.map(Some)
|
||||||
} else {
|
} else {
|
||||||
let solution_path = if let Some(dir) = current_exercise.dir {
|
let sol_path = current_exercise.sol_path();
|
||||||
format!("solutions/{dir}/{}.rs", current_exercise.name)
|
|
||||||
} else {
|
|
||||||
format!("solutions/{}.rs", current_exercise.name)
|
|
||||||
};
|
|
||||||
|
|
||||||
if Path::new(&solution_path).exists() {
|
if Path::new(&sol_path).exists() {
|
||||||
return Ok(Some(solution_path));
|
return Ok(Some(sol_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the exercise index of the first pending exercise found.
|
||||||
|
fn check_all_exercises(&self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||||
|
stdout.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
||||||
|
let n_exercises = self.exercises.len();
|
||||||
|
|
||||||
|
let status = thread::scope(|s| {
|
||||||
|
let handles = self
|
||||||
|
.exercises
|
||||||
|
.iter()
|
||||||
|
.map(|exercise| s.spawn(|| exercise.run_exercise(None, &self.cmd_runner)))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for (exercise_ind, handle) in handles.into_iter().enumerate() {
|
||||||
|
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
||||||
|
stdout.flush()?;
|
||||||
|
|
||||||
|
let Ok(success) = handle.join().unwrap() else {
|
||||||
|
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
||||||
|
};
|
||||||
|
|
||||||
|
if !success {
|
||||||
|
return Ok(AllExercisesCheck::Pending(exercise_ind));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok::<_, io::Error>(AllExercisesCheck::AllDone)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
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 success = exercise.run_exercise(None, &self.cmd_runner)?;
|
||||||
|
if !success {
|
||||||
|
return Ok(Some(exercise_ind));
|
||||||
|
}
|
||||||
|
|
||||||
|
exercise_ind += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
/// Mark the current exercise as done and move on to the next pending exercise if one exists.
|
/// 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
|
/// 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.
|
/// done. If an exercise which is marked as done fails, mark it as pending and continue on it.
|
||||||
|
@ -350,44 +422,13 @@ impl AppState {
|
||||||
return Ok(ExercisesProgress::NewPending);
|
return Ok(ExercisesProgress::NewPending);
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
if let Some(pending_exercise_ind) = self.check_all_exercises(stdout)? {
|
||||||
|
|
||||||
let n_exercises = self.exercises.len();
|
|
||||||
|
|
||||||
let pending_exercise_ind = thread::scope(|s| {
|
|
||||||
let handles = self
|
|
||||||
.exercises
|
|
||||||
.iter_mut()
|
|
||||||
.map(|exercise| {
|
|
||||||
s.spawn(|| {
|
|
||||||
let success = exercise.run_exercise(None, &self.cmd_runner)?;
|
|
||||||
exercise.done = success;
|
|
||||||
Ok::<_, Error>(success)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for (exercise_ind, handle) in handles.into_iter().enumerate() {
|
|
||||||
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
|
||||||
stdout.flush()?;
|
|
||||||
|
|
||||||
let success = handle.join().unwrap()?;
|
|
||||||
if !success {
|
|
||||||
stdout.write_all(b"\n\n")?;
|
stdout.write_all(b"\n\n")?;
|
||||||
return Ok(Some(exercise_ind));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok::<_, Error>(None)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if let Some(pending_exercise_ind) = pending_exercise_ind {
|
|
||||||
self.current_exercise_ind = pending_exercise_ind;
|
self.current_exercise_ind = pending_exercise_ind;
|
||||||
self.n_done = self
|
self.exercises[pending_exercise_ind].done = false;
|
||||||
.exercises
|
// All exercises were marked as done.
|
||||||
.iter()
|
self.n_done -= 1;
|
||||||
.filter(|exercise| exercise.done)
|
|
||||||
.count() as u16;
|
|
||||||
self.write()?;
|
self.write()?;
|
||||||
return Ok(ExercisesProgress::NewPending);
|
return Ok(ExercisesProgress::NewPending);
|
||||||
}
|
}
|
||||||
|
@ -408,11 +449,12 @@ impl AppState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
|
const RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
|
||||||
All exercises seem to be done.
|
All exercises seem to be done.
|
||||||
Recompiling and running all exercises to make sure that all of them are actually done.
|
Recompiling and running all exercises to make sure that all of them are actually done.
|
||||||
";
|
";
|
||||||
|
|
||||||
const FENISH_LINE: &str = "+----------------------------------------------------+
|
const FENISH_LINE: &str = "+----------------------------------------------------+
|
||||||
| You made it to the Fe-nish line! |
|
| You made it to the Fe-nish line! |
|
||||||
+-------------------------- ------------------------+
|
+-------------------------- ------------------------+
|
||||||
|
@ -458,9 +500,11 @@ mod tests {
|
||||||
exercises: vec![dummy_exercise(), dummy_exercise(), dummy_exercise()],
|
exercises: vec![dummy_exercise(), dummy_exercise(), dummy_exercise()],
|
||||||
n_done: 0,
|
n_done: 0,
|
||||||
final_message: String::new(),
|
final_message: String::new(),
|
||||||
|
state_file: tempfile::tempfile().unwrap(),
|
||||||
file_buf: Vec::new(),
|
file_buf: Vec::new(),
|
||||||
official_exercises: true,
|
official_exercises: true,
|
||||||
cmd_runner: CmdRunner::build().unwrap(),
|
cmd_runner: CmdRunner::build().unwrap(),
|
||||||
|
vs_code: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
|
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::info_file::ExerciseInfo;
|
use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
|
||||||
|
|
||||||
/// Initial capacity of the bins buffer.
|
/// Initial capacity of the bins buffer.
|
||||||
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;
|
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;
|
||||||
|
|
|
@ -17,6 +17,8 @@ use crate::{
|
||||||
CURRENT_FORMAT_VERSION,
|
CURRENT_FORMAT_VERSION,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
||||||
|
|
||||||
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
||||||
fn forbidden_char(input: &str) -> Option<char> {
|
fn forbidden_char(input: &str) -> Option<char> {
|
||||||
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
|
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
|
||||||
|
@ -59,6 +61,9 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
bail!("Found an empty exercise name in `info.toml`");
|
bail!("Found an empty exercise name in `info.toml`");
|
||||||
}
|
}
|
||||||
|
if name.len() > MAX_EXERCISE_NAME_LEN {
|
||||||
|
bail!("The length of the exercise name `{name}` is bigger than the maximum {MAX_EXERCISE_NAME_LEN}");
|
||||||
|
}
|
||||||
if let Some(c) = forbidden_char(name) {
|
if let Some(c) = forbidden_char(name) {
|
||||||
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
|
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@ pub struct Exercise {
|
||||||
|
|
||||||
pub trait RunnableExercise {
|
pub trait RunnableExercise {
|
||||||
fn name(&self) -> &str;
|
fn name(&self) -> &str;
|
||||||
|
fn dir(&self) -> Option<&str>;
|
||||||
fn strict_clippy(&self) -> bool;
|
fn strict_clippy(&self) -> bool;
|
||||||
fn test(&self) -> bool;
|
fn test(&self) -> bool;
|
||||||
|
|
||||||
|
@ -145,6 +146,31 @@ pub trait RunnableExercise {
|
||||||
|
|
||||||
self.run::<true>(&bin_name, output, cmd_runner)
|
self.run::<true>(&bin_name, output, cmd_runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sol_path(&self) -> String {
|
||||||
|
let name = self.name();
|
||||||
|
|
||||||
|
let mut path = if let Some(dir) = self.dir() {
|
||||||
|
// 14 = 10 + 1 + 3
|
||||||
|
// solutions/ + / + .rs
|
||||||
|
let mut path = String::with_capacity(14 + dir.len() + name.len());
|
||||||
|
path.push_str("solutions/");
|
||||||
|
path.push_str(dir);
|
||||||
|
path.push('/');
|
||||||
|
path
|
||||||
|
} else {
|
||||||
|
// 13 = 10 + 3
|
||||||
|
// solutions/ + .rs
|
||||||
|
let mut path = String::with_capacity(13 + name.len());
|
||||||
|
path.push_str("solutions/");
|
||||||
|
path
|
||||||
|
};
|
||||||
|
|
||||||
|
path.push_str(name);
|
||||||
|
path.push_str(".rs");
|
||||||
|
|
||||||
|
path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunnableExercise for Exercise {
|
impl RunnableExercise for Exercise {
|
||||||
|
@ -153,6 +179,11 @@ impl RunnableExercise for Exercise {
|
||||||
self.name
|
self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dir(&self) -> Option<&str> {
|
||||||
|
self.dir
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn strict_clippy(&self) -> bool {
|
fn strict_clippy(&self) -> bool {
|
||||||
self.strict_clippy
|
self.strict_clippy
|
||||||
|
|
|
@ -52,30 +52,6 @@ impl ExerciseInfo {
|
||||||
|
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Path to the solution file starting with the `solutions/` directory.
|
|
||||||
pub fn sol_path(&self) -> String {
|
|
||||||
let mut path = if let Some(dir) = &self.dir {
|
|
||||||
// 14 = 10 + 1 + 3
|
|
||||||
// solutions/ + / + .rs
|
|
||||||
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
|
|
||||||
path.push_str("solutions/");
|
|
||||||
path.push_str(dir);
|
|
||||||
path.push('/');
|
|
||||||
path
|
|
||||||
} else {
|
|
||||||
// 13 = 10 + 3
|
|
||||||
// solutions/ + .rs
|
|
||||||
let mut path = String::with_capacity(13 + self.name.len());
|
|
||||||
path.push_str("solutions/");
|
|
||||||
path
|
|
||||||
};
|
|
||||||
|
|
||||||
path.push_str(&self.name);
|
|
||||||
path.push_str(".rs");
|
|
||||||
|
|
||||||
path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunnableExercise for ExerciseInfo {
|
impl RunnableExercise for ExerciseInfo {
|
||||||
|
@ -84,6 +60,11 @@ impl RunnableExercise for ExerciseInfo {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dir(&self) -> Option<&str> {
|
||||||
|
self.dir.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn strict_clippy(&self) -> bool {
|
fn strict_clippy(&self) -> bool {
|
||||||
self.strict_clippy
|
self.strict_clippy
|
||||||
|
|
|
@ -13,8 +13,8 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile,
|
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise,
|
||||||
term::press_enter_prompt,
|
info_file::InfoFile, term::press_enter_prompt,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
@ -14,13 +14,11 @@ use crate::{
|
||||||
app_state::AppState,
|
app_state::AppState,
|
||||||
exercise::Exercise,
|
exercise::Exercise,
|
||||||
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
|
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
|
||||||
MAX_EXERCISE_NAME_LEN,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::scroll_state::ScrollState;
|
use super::scroll_state::ScrollState;
|
||||||
|
|
||||||
// +1 for column padding.
|
const COL_SPACING: usize = 2;
|
||||||
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
|
|
||||||
|
|
||||||
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
|
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
stdout
|
stdout
|
||||||
|
@ -41,7 +39,7 @@ pub struct ListState<'a> {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
app_state: &'a mut AppState,
|
app_state: &'a mut AppState,
|
||||||
scroll_state: ScrollState,
|
scroll_state: ScrollState,
|
||||||
name_col_width: usize,
|
name_col_padding: Vec<u8>,
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
term_width: u16,
|
term_width: u16,
|
||||||
term_height: u16,
|
term_height: u16,
|
||||||
|
@ -61,6 +59,7 @@ impl<'a> ListState<'a> {
|
||||||
.map(|exercise| exercise.name.len())
|
.map(|exercise| exercise.name.len())
|
||||||
.max()
|
.max()
|
||||||
.map_or(name_col_title_len, |max| max.max(name_col_title_len));
|
.map_or(name_col_title_len, |max| max.max(name_col_title_len));
|
||||||
|
let name_col_padding = vec![b' '; name_col_width + COL_SPACING];
|
||||||
|
|
||||||
let filter = Filter::None;
|
let filter = Filter::None;
|
||||||
let n_rows_with_filter = app_state.exercises().len();
|
let n_rows_with_filter = app_state.exercises().len();
|
||||||
|
@ -73,7 +72,7 @@ impl<'a> ListState<'a> {
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
app_state,
|
app_state,
|
||||||
scroll_state,
|
scroll_state,
|
||||||
name_col_width,
|
name_col_padding,
|
||||||
filter,
|
filter,
|
||||||
// Set by `set_term_size`
|
// Set by `set_term_size`
|
||||||
term_width: 0,
|
term_width: 0,
|
||||||
|
@ -162,9 +161,15 @@ impl<'a> ListState<'a> {
|
||||||
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
|
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
|
||||||
|
|
||||||
writer.write_str(exercise.name)?;
|
writer.write_str(exercise.name)?;
|
||||||
writer.write_ascii(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?;
|
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
|
||||||
|
|
||||||
|
// The list links aren't shown correctly in VS Code on Windows.
|
||||||
|
// But VS Code shows its own links anyway.
|
||||||
|
if self.app_state.vs_code() {
|
||||||
|
writer.write_str(exercise.path)?;
|
||||||
|
} else {
|
||||||
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
|
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
|
||||||
|
}
|
||||||
|
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(ResetColor)?;
|
||||||
|
@ -184,7 +189,7 @@ impl<'a> ListState<'a> {
|
||||||
// Header
|
// Header
|
||||||
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
||||||
writer.write_ascii(b" Current State Name")?;
|
writer.write_ascii(b" Current State Name")?;
|
||||||
writer.write_ascii(&SPACE[..self.name_col_width - 2])?;
|
writer.write_ascii(&self.name_col_padding[4..])?;
|
||||||
writer.write_ascii(b"Path")?;
|
writer.write_ascii(b"Path")?;
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
|
@ -258,15 +263,18 @@ impl<'a> ListState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.write_ascii(b" | <q>uit list")?;
|
writer.write_ascii(b" | <q>uit list")?;
|
||||||
|
next_ln(stdout)?;
|
||||||
} else {
|
} else {
|
||||||
writer.stdout.queue(SetForegroundColor(Color::Magenta))?;
|
writer.stdout.queue(SetForegroundColor(Color::Magenta))?;
|
||||||
writer.write_str(&self.message)?;
|
writer.write_str(&self.message)?;
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(ResetColor)?;
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
}
|
|
||||||
|
|
||||||
|
if self.narrow_term {
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stdout.queue(EndSynchronizedUpdate)?.flush()
|
stdout.queue(EndSynchronizedUpdate)?.flush()
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ mod term;
|
||||||
mod watch;
|
mod watch;
|
||||||
|
|
||||||
const CURRENT_FORMAT_VERSION: u8 = 1;
|
const CURRENT_FORMAT_VERSION: u8 = 1;
|
||||||
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
|
||||||
|
|
||||||
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
12
src/term.rs
12
src/term.rs
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
cell::Cell,
|
fmt, fs,
|
||||||
env, fmt, fs,
|
|
||||||
io::{self, BufRead, StdoutLock, Write},
|
io::{self, BufRead, StdoutLock, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,10 +10,6 @@ use crossterm::{
|
||||||
Command, QueueableCommand,
|
Command, QueueableCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static VS_CODE: Cell<bool> = Cell::new(env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MaxLenWriter<'a, 'b> {
|
pub struct MaxLenWriter<'a, 'b> {
|
||||||
pub stdout: &'a mut StdoutLock<'b>,
|
pub stdout: &'a mut StdoutLock<'b>,
|
||||||
len: usize,
|
len: usize,
|
||||||
|
@ -161,11 +156,6 @@ pub fn terminal_file_link<'a>(
|
||||||
path: &str,
|
path: &str,
|
||||||
color: Color,
|
color: Color,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
// VS Code shows its own links. This also avoids some issues, especially on Windows.
|
|
||||||
if VS_CODE.get() {
|
|
||||||
return writer.write_str(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
let canonical_path = fs::canonicalize(path).ok();
|
let canonical_path = fs::canonicalize(path).ok();
|
||||||
|
|
||||||
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {
|
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {
|
||||||
|
|
Loading…
Reference in a new issue