diff options
Diffstat (limited to 'src/dev')
| -rw-r--r-- | src/dev/check.rs | 50 | ||||
| -rw-r--r-- | src/dev/new.rs | 3 |
2 files changed, 25 insertions, 28 deletions
diff --git a/src/dev/check.rs b/src/dev/check.rs index 9cde7f2..f711106 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -15,6 +15,7 @@ use crate::{ cmd::CmdRunner, exercise::{OUTPUT_CAPACITY, RunnableExercise}, info_file::{ExerciseInfo, InfoFile}, + term::ProgressCounter, }; const MAX_N_EXERCISES: usize = 999; @@ -105,13 +106,15 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> { if !file_buf.contains("fn main()") { bail!( - "The `main` function is missing in the file `{path}`.\nCreate at least an empty `main` function to avoid language server errors" + "The `main` function is missing in the file `{path}`.\n\ + Create at least an empty `main` function to avoid language server errors" ); } if !file_buf.contains("// TODO") { bail!( - "Didn't find any `// TODO` comment in the file `{path}`.\nYou need to have at least one such comment to guide the user." + "Didn't find any `// TODO` comment in the file `{path}`.\n\ + You need to have at least one such comment to guide the user." ); } @@ -217,10 +220,7 @@ fn check_exercises_unsolved( .collect::<Result<Vec<_>, _>>() .context("Failed to spawn a thread to check if an exercise is already solved")?; - let n_handles = handles.len(); - write!(stdout, "Progress: 0/{n_handles}")?; - stdout.flush()?; - let mut handle_num = 1; + let mut progress_counter = ProgressCounter::new(&mut stdout, handles.len())?; for (exercise_name, handle) in handles { let Ok(result) = handle.join() else { @@ -229,17 +229,17 @@ fn check_exercises_unsolved( match result { Ok(true) => { - bail!("The exercise {exercise_name} is already solved.\n{SKIP_CHECK_UNSOLVED_HINT}",) + bail!( + "The exercise {exercise_name} is already solved.\n\ + {SKIP_CHECK_UNSOLVED_HINT}", + ) } Ok(false) => (), Err(e) => return Err(e), } - write!(stdout, "\rProgress: {handle_num}/{n_handles}")?; - stdout.flush()?; - handle_num += 1; + progress_counter.increment()?; } - stdout.write_all(b"\n")?; Ok(()) } @@ -247,10 +247,12 @@ fn check_exercises_unsolved( fn check_exercises(info_file: &'static InfoFile, cmd_runner: &'static CmdRunner) -> Result<()> { match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) { Ordering::Less => bail!( - "`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version" + "`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\n\ + Please migrate to the latest format version" ), Ordering::Greater => bail!( - "`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program" + "`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\n\ + Try updating the Rustlings program" ), Ordering::Equal => (), } @@ -318,10 +320,7 @@ fn check_solutions( .arg("always") .stdin(Stdio::null()); - let n_handles = handles.len(); - write!(stdout, "Progress: 0/{n_handles}")?; - stdout.flush()?; - let mut handle_num = 1; + let mut progress_counter = ProgressCounter::new(&mut stdout, handles.len())?; for (exercise_info, handle) in info_file.exercises.iter().zip(handles) { let Ok(check_result) = handle.join() else { @@ -338,7 +337,7 @@ fn check_solutions( } SolutionCheck::MissingOptional => (), SolutionCheck::RunFailure { output } => { - stdout.write_all(b"\n\n")?; + drop(progress_counter); stdout.write_all(&output)?; bail!( "Running the solution of the exercise {} failed with the error above", @@ -348,22 +347,21 @@ fn check_solutions( SolutionCheck::Err(e) => return Err(e), } - write!(stdout, "\rProgress: {handle_num}/{n_handles}")?; - stdout.flush()?; - handle_num += 1; + progress_counter.increment()?; } - stdout.write_all(b"\n")?; + let n_solutions = sol_paths.len(); let handle = thread::Builder::new() .spawn(move || check_unexpected_files("solutions", &sol_paths)) .context( "Failed to spawn a thread to check for unexpected files in the solutions directory", )?; - if !fmt_cmd - .status() - .context("Failed to run `rustfmt` on all solution files")? - .success() + if n_solutions > 0 + && !fmt_cmd + .status() + .context("Failed to run `rustfmt` on all solution files")? + .success() { bail!("Some solutions aren't formatted. Run `rustfmt` on them"); } diff --git a/src/dev/new.rs b/src/dev/new.rs index 883b6fa..7c72a6b 100644 --- a/src/dev/new.rs +++ b/src/dev/new.rs @@ -78,8 +78,7 @@ pub fn new(path: &Path, no_git: bool) -> Result<()> { Ok(()) } -pub const GITIGNORE: &[u8] = b".rustlings-state.txt -Cargo.lock +pub const GITIGNORE: &[u8] = b"Cargo.lock target/ .vscode/ !.vscode/extensions.json |
