diff --git a/src/cargo_toml.rs b/src/cargo_toml.rs index e966809f35..ce0dfd0cf3 100644 --- a/src/cargo_toml.rs +++ b/src/cargo_toml.rs @@ -134,7 +134,14 @@ mod tests { ); assert_eq!( - updated_cargo_toml(&exercise_infos, "abc\nbin = [xxx]\n123", b"../").unwrap(), + updated_cargo_toml( + &exercise_infos, + "abc\n\ + bin = [xxx]\n\ + 123", + b"../" + ) + .unwrap(), br#"abc bin = [ { name = "1", path = "../exercises/1.rs" }, diff --git a/src/dev/check.rs b/src/dev/check.rs index 67f54930fd..f71110630d 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -106,13 +106,15 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result> { 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." ); } @@ -227,7 +229,10 @@ 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), @@ -242,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 => (), } diff --git a/src/init.rs b/src/init.rs index a60fba70df..68011ed4e9 100644 --- a/src/init.rs +++ b/src/init.rs @@ -35,7 +35,27 @@ pub fn init() -> Result<()> { .stdin(Stdio::null()) .stderr(Stdio::null()) .output() - .context(CARGO_LOCATE_PROJECT_ERR)?; + .context( + "Failed to run the command `cargo locate-project …`\n\ + Did you already install Rust?\n\ + Try running `cargo --version` to diagnose the problem.", + )?; + + if !Command::new("cargo") + .arg("clippy") + .arg("--version") + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .context("Failed to run the command `cargo clippy --version`")? + .success() + { + bail!( + "Clippy, the official Rust linter, is missing.\n\ + Please install it first before initializing Rustlings." + ) + } let mut stdout = io::stdout().lock(); let mut init_git = true; @@ -58,11 +78,13 @@ pub fn init() -> Result<()> { && !workspace_manifest_content.contains("workspace.") { bail!( - "The current directory is already part of a Cargo project.\nPlease initialize Rustlings in a different directory" + "The current directory is already part of a Cargo project.\n\ + Please initialize Rustlings in a different directory" ); } - stdout.write_all(b"This command will create the directory `rustlings/` as a member of this Cargo workspace.\nPress ENTER to continue ")?; + stdout.write_all(b"This command will create the directory `rustlings/` as a member of this Cargo workspace.\n\ + Press ENTER to continue ")?; press_enter_prompt(&mut stdout)?; // Make sure "rustlings" is added to `workspace.members` by making @@ -78,7 +100,8 @@ pub fn init() -> Result<()> { .status()?; if !status.success() { bail!( - "Failed to initialize a new Cargo workspace member.\nPlease initialize Rustlings in a different directory" + "Failed to initialize a new Cargo workspace member.\n\ + Please initialize Rustlings in a different directory" ); } @@ -87,7 +110,8 @@ pub fn init() -> Result<()> { .context("Failed to remove the temporary directory `rustlings/`")?; init_git = false; } else { - stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?; + stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\n\ + Press ENTER to continue ")?; press_enter_prompt(&mut stdout)?; } @@ -166,10 +190,6 @@ pub fn init() -> Result<()> { Ok(()) } -const CARGO_LOCATE_PROJECT_ERR: &str = "Failed to run the command `cargo locate-project …` -Did you already install Rust? -Try running `cargo --version` to diagnose the problem."; - const INIT_SOLUTION_FILE: &[u8] = b"fn main() { // DON'T EDIT THIS SOLUTION FILE! // It will be automatically filled after you finish the exercise. diff --git a/src/main.rs b/src/main.rs index bce2593dc6..29de56b3d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,7 +104,11 @@ fn main() -> Result { clear_terminal(&mut stdout)?; let welcome_message = welcome_message.trim_ascii(); - write!(stdout, "{welcome_message}\n\nPress ENTER to continue ")?; + write!( + stdout, + "{welcome_message}\n\n\ + Press ENTER to continue " + )?; press_enter_prompt(&mut stdout)?; clear_terminal(&mut stdout)?; // Flush to be able to show errors occurring before printing a newline to stdout.