From 642c3bd37e3195f7f744a5fa60a53e59d8da5526 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 21 Apr 2024 20:22:01 +0200 Subject: Fix the generated Cargo.toml after rustlings init --- src/dev/check.rs | 51 ++++++++++----------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) (limited to 'src/dev/check.rs') diff --git a/src/dev/check.rs b/src/dev/check.rs index cd115b7..9859c3e 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -7,6 +7,7 @@ use std::{ }; use crate::{ + cargo_toml::{append_bins, bins_start_end_ind}, info_file::{ExerciseInfo, InfoFile}, CURRENT_FORMAT_VERSION, DEBUG_PROFILE, }; @@ -136,41 +137,6 @@ fn check_exercises(info_file: &InfoFile) -> Result<()> { Ok(()) } -pub fn bins_start_end_ind(cargo_toml: &str) -> Result<(usize, usize)> { - let start_ind = cargo_toml - .find("bin = [") - .context("Failed to find the start of the `bin` list (`bin = [`)")? - + 7; - let end_ind = start_ind - + cargo_toml - .get(start_ind..) - .and_then(|slice| slice.as_bytes().iter().position(|c| *c == b']')) - .context("Failed to find the end of the `bin` list (`]`)")?; - - Ok((start_ind, end_ind)) -} - -pub fn append_bins( - buf: &mut Vec, - exercise_infos: &[ExerciseInfo], - exercise_path_prefix: &[u8], -) { - buf.push(b'\n'); - for exercise_info in exercise_infos { - buf.extend_from_slice(b" { name = \""); - buf.extend_from_slice(exercise_info.name.as_bytes()); - buf.extend_from_slice(b"\", path = \""); - buf.extend_from_slice(exercise_path_prefix); - buf.extend_from_slice(b"exercises/"); - if let Some(dir) = &exercise_info.dir { - buf.extend_from_slice(dir.as_bytes()); - buf.push(b'/'); - } - buf.extend_from_slice(exercise_info.name.as_bytes()); - buf.extend_from_slice(b".rs\" },\n"); - } -} - fn check_cargo_toml( exercise_infos: &[ExerciseInfo], current_cargo_toml: &str, @@ -183,7 +149,13 @@ fn check_cargo_toml( append_bins(&mut new_bins, exercise_infos, exercise_path_prefix); if old_bins != new_bins { - bail!("`Cargo.toml` is outdated. Run `rustlings dev update` to update it"); + if DEBUG_PROFILE { + bail!("The file `dev/Cargo.toml` is outdated. Please run `cargo run -- dev update` to update it"); + } else { + bail!( + "The file `Cargo.toml` is outdated. Please run `rustlings dev update` to update it", + ); + } } Ok(()) @@ -198,14 +170,11 @@ pub fn check() -> Result<()> { &info_file.exercises, include_str!("../../dev/Cargo.toml"), b"../", - ) - .context("The file `dev/Cargo.toml` is outdated. Please run `cargo run -- dev update` to update it")?; + )?; } else { let current_cargo_toml = fs::read_to_string("Cargo.toml").context("Failed to read the file `Cargo.toml`")?; - check_cargo_toml(&info_file.exercises, ¤t_cargo_toml, b"").context( - "The file `Cargo.toml` is outdated. Please run `rustlings dev update` to update it", - )?; + check_cargo_toml(&info_file.exercises, ¤t_cargo_toml, b"")?; } println!("\nEverything looks fine!"); -- cgit v1.2.3