feat(cli): added success-hints option for the rustlings command

closes #1373
This commit is contained in:
Romain Bayle
2023-05-01 02:49:19 +02:00
parent 6d4a980b04
commit 5d3696a9e6
2 changed files with 28 additions and 18 deletions

View File

@@ -12,6 +12,7 @@ pub fn verify<'a>(
exercises: impl IntoIterator<Item = &'a Exercise>,
progress: (usize, usize),
verbose: bool,
success_hints: bool,
) -> Result<(), &'a Exercise> {
let (num_done, total) = progress;
let bar = ProgressBar::new(total as u64);
@@ -25,9 +26,9 @@ pub fn verify<'a>(
for exercise in exercises {
let compile_result = match exercise.mode {
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose),
Mode::Compile => compile_and_run_interactively(exercise),
Mode::Clippy => compile_only(exercise),
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose, success_hints),
Mode::Compile => compile_and_run_interactively(exercise, success_hints),
Mode::Clippy => compile_only(exercise, success_hints),
};
if !compile_result.unwrap_or(false) {
return Err(exercise);
@@ -46,12 +47,12 @@ enum RunMode {
// Compile and run the resulting test harness of the given Exercise
pub fn test(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
compile_and_test(exercise, RunMode::NonInteractive, verbose)?;
compile_and_test(exercise, RunMode::NonInteractive, verbose, false)?;
Ok(())
}
// Invoke the rust compiler without running the resulting binary
fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
fn compile_only(exercise: &Exercise, success_hints: bool) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {exercise}..."));
progress_bar.enable_steady_tick(100);
@@ -59,11 +60,11 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
let _ = compile(exercise, &progress_bar)?;
progress_bar.finish_and_clear();
Ok(prompt_for_completion(exercise, None))
Ok(prompt_for_completion(exercise, None, success_hints))
}
// Compile the given Exercise and run the resulting binary in an interactive mode
fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
fn compile_and_run_interactively(exercise: &Exercise, success_hints: bool) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {exercise}..."));
progress_bar.enable_steady_tick(100);
@@ -84,12 +85,12 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
}
};
Ok(prompt_for_completion(exercise, Some(output.stdout)))
Ok(prompt_for_completion(exercise, Some(output.stdout), success_hints))
}
// Compile the given Exercise as a test harness and display
// the output if verbose is set to true
fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Result<bool, ()> {
fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool, success_hints: bool) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Testing {exercise}..."));
progress_bar.enable_steady_tick(100);
@@ -104,7 +105,7 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
println!("{}", output.stdout);
}
if let RunMode::Interactive = run_mode {
Ok(prompt_for_completion(exercise, None))
Ok(prompt_for_completion(exercise, None, success_hints))
} else {
Ok(true)
}
@@ -142,12 +143,11 @@ fn compile<'a, 'b>(
}
}
fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) -> bool {
fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>, success_hints: bool) -> bool {
let context = match exercise.state() {
State::Done => return true,
State::Pending(context) => context,
};
match exercise.mode {
Mode::Compile => success!("Successfully ran {}!", exercise),
Mode::Test => success!("Successfully tested {}!", exercise),
@@ -167,7 +167,6 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
Mode::Test => "The code is compiling, and the tests pass!",
Mode::Clippy => clippy_success_msg,
};
println!();
if no_emoji {
println!("~*~ {success_msg} ~*~")
@@ -183,6 +182,13 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
println!("{}", separator());
println!();
}
if success_hints {
println!("Hints:");
println!("{}", separator());
println!("{}", exercise.hint);
println!("{}", separator());
println!();
}
println!("You can keep working on this exercise,");
println!(