From 51b4c240ed006a8279bd94e9b7ed5df67086c86e Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 00:30:01 +0100 Subject: [PATCH 01/13] Use `which` instead of running `rustc --version` --- Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 16 ++-------------- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3950c47..1bfd301 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,6 +195,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "encode_unicode" version = "0.3.6" @@ -207,6 +213,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "filetime" version = "0.2.23" @@ -354,6 +370,12 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + [[package]] name = "log" version = "0.4.21" @@ -521,6 +543,19 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "rustix" +version = "0.38.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +dependencies = [ + "bitflags 2.4.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "rustlings" version = "5.6.1" @@ -537,6 +572,7 @@ dependencies = [ "serde", "serde_json", "toml", + "which", ] [[package]] @@ -694,6 +730,18 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "which" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" +dependencies = [ + "either", + "home", + "rustix", + "winsafe", +] + [[package]] name = "winapi" version = "0.3.9" @@ -865,3 +913,9 @@ checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" dependencies = [ "memchr", ] + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" diff --git a/Cargo.toml b/Cargo.toml index 218b799..de65fc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ regex = "1.10.3" serde_json = "1.0.114" serde = { version = "1.0.197", features = ["derive"] } toml = "0.8.10" +which = "6.0.1" [[bin]] name = "rustlings" diff --git a/src/main.rs b/src/main.rs index a06f0c5..f932631 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use std::ffi::OsStr; use std::fs; use std::io::{self, prelude::*}; use std::path::Path; -use std::process::{Command, Stdio}; +use std::process::Command; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{channel, RecvTimeoutError}; use std::sync::{Arc, Mutex}; @@ -100,7 +100,7 @@ fn main() { std::process::exit(1); } - if !rustc_exists() { + if which::which("rustc").is_err() { println!("We cannot find `rustc`."); println!("Try running `rustc --version` to diagnose your problem."); println!("For instructions on how to install Rust, check the README."); @@ -403,18 +403,6 @@ fn watch( } } -fn rustc_exists() -> bool { - Command::new("rustc") - .args(["--version"]) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .stdin(Stdio::null()) - .spawn() - .and_then(|mut child| child.wait()) - .map(|status| status.success()) - .unwrap_or(false) -} - const DEFAULT_OUT: &str = r#"Thanks for installing Rustlings! Is this your first time? Don't worry, Rustlings was made for beginners! We are From 83cd91ccca22e36ed94e03cc622a88ef45e6da10 Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 02:35:51 +0100 Subject: [PATCH 02/13] Replace toml with toml_edit --- Cargo.lock | 18 +++--------------- Cargo.toml | 2 +- src/main.rs | 6 ++++-- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3950c47..52b2725 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,7 +536,7 @@ dependencies = [ "regex", "serde", "serde_json", - "toml", + "toml_edit", ] [[package]] @@ -617,18 +617,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" -[[package]] -name = "toml" -version = "0.8.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a9aad4a3066010876e8dcf5a8a06e70a558751117a145c6ce2b82c2e2054290" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - [[package]] name = "toml_datetime" version = "0.6.5" @@ -640,9 +628,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.6" +version = "0.22.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1b5fd4128cc8d3e0cb74d4ed9a9cc7c7284becd4df68f5f940e1ad123606f6" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" dependencies = [ "indexmap", "serde", diff --git a/Cargo.toml b/Cargo.toml index 218b799..2861459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ notify-debouncer-mini = "0.4.1" regex = "1.10.3" serde_json = "1.0.114" serde = { version = "1.0.197", features = ["derive"] } -toml = "0.8.10" +toml_edit = { version = "0.22.9", default-features = false, features = ["parse", "serde"] } [[bin]] name = "rustlings" diff --git a/src/main.rs b/src/main.rs index a06f0c5..8e0029d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -107,8 +107,10 @@ fn main() { std::process::exit(1); } - let toml_str = &fs::read_to_string("info.toml").unwrap(); - let exercises = toml::from_str::(toml_str).unwrap().exercises; + let info_file = fs::read_to_string("info.toml").unwrap(); + let exercises = toml_edit::de::from_str::(&info_file) + .unwrap() + .exercises; let verbose = args.nocapture; let command = args.command.unwrap_or_else(|| { From e4520602f52935ff310534afc65160bcc5796a97 Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 02:41:45 +0100 Subject: [PATCH 03/13] Use the NotFound variant of the IO error --- src/main.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8e0029d..d6542aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,15 +91,6 @@ fn main() { println!("\n{WELCOME}\n"); } - if !Path::new("info.toml").exists() { - println!( - "{} must be run from the rustlings directory", - std::env::current_exe().unwrap().to_str().unwrap() - ); - println!("Try `cd rustlings/`!"); - std::process::exit(1); - } - if !rustc_exists() { println!("We cannot find `rustc`."); println!("Try running `rustc --version` to diagnose your problem."); @@ -107,7 +98,15 @@ fn main() { std::process::exit(1); } - let info_file = fs::read_to_string("info.toml").unwrap(); + let info_file = fs::read_to_string("info.toml").unwrap_or_else(|e| { + match e.kind() { + io::ErrorKind::NotFound => println!( + "The program must be run from the rustlings directory\nTry `cd rustlings/`!", + ), + _ => println!("Failed to read the info.toml file: {e}"), + } + std::process::exit(1); + }); let exercises = toml_edit::de::from_str::(&info_file) .unwrap() .exercises; From dca3ea355ea1809318ea545f23f396405d86aa0a Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 14:10:51 +0100 Subject: [PATCH 04/13] Remove the home dependency since it is not used --- Cargo.lock | 10 ---------- Cargo.toml | 1 - 2 files changed, 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3950c47..9d8606a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -255,15 +255,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "home" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "indexmap" version = "2.2.5" @@ -529,7 +520,6 @@ dependencies = [ "clap", "console", "glob", - "home", "indicatif", "notify-debouncer-mini", "predicates", diff --git a/Cargo.toml b/Cargo.toml index 218b799..2e6ab3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ edition = "2021" clap = { version = "4.5.2", features = ["derive"] } console = "0.15.8" glob = "0.3.0" -home = "0.5.9" indicatif = "0.17.8" notify-debouncer-mini = "0.4.1" regex = "1.10.3" From d911586788ad411be92e43cdc2f7e88fee7e78a4 Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 17:21:54 +0100 Subject: [PATCH 05/13] Pipe the output to null instead of capturing and ignoring it --- src/exercise.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/exercise.rs b/src/exercise.rs index 664b362..e6a9222 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -5,7 +5,7 @@ use std::fmt::{self, Display, Formatter}; use std::fs::{self, remove_file, File}; use std::io::Read; use std::path::PathBuf; -use std::process::{self, Command}; +use std::process::{self, Command, Stdio}; const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"]; const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"]; @@ -148,7 +148,10 @@ path = "{}.rs""#, .args(RUSTC_COLOR_ARGS) .args(RUSTC_EDITION_ARGS) .args(RUSTC_NO_DEBUG_ARGS) - .output() + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() .expect("Failed to compile!"); // Due to an issue with Clippy, a cargo clean is required to catch all lints. // See https://github.com/rust-lang/rust-clippy/issues/2604 @@ -157,7 +160,10 @@ path = "{}.rs""#, Command::new("cargo") .args(["clean", "--manifest-path", CLIPPY_CARGO_TOML_PATH]) .args(RUSTC_COLOR_ARGS) - .output() + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() .expect("Failed to run 'cargo clean'"); Command::new("cargo") .args(["clippy", "--manifest-path", CLIPPY_CARGO_TOML_PATH]) From 853d0593d061119b042a45b602ff52af229dad83 Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:47:33 +0100 Subject: [PATCH 06/13] Derive Eq when PartialEq is derived --- src/exercise.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/exercise.rs b/src/exercise.rs index 664b362..a13ed2c 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -58,7 +58,7 @@ pub struct Exercise { // An enum to track of the state of an Exercise. // An Exercise can be either Done or Pending -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Eq, Debug)] pub enum State { // The state of the exercise once it's been completed Done, @@ -67,7 +67,7 @@ pub enum State { } // The context information of a pending exercise -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Eq, Debug)] pub struct ContextLine { // The source code that is still pending completion pub line: String, From f36efae25deee03cb6f98ce7fc1e59efb7e72985 Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:48:06 +0100 Subject: [PATCH 07/13] Only use arg instead of args AND arg --- src/run.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/run.rs b/src/run.rs index e0ada4c..6dd0388 100644 --- a/src/run.rs +++ b/src/run.rs @@ -21,7 +21,8 @@ pub fn run(exercise: &Exercise, verbose: bool) -> Result<(), ()> { // Resets the exercise by stashing the changes. pub fn reset(exercise: &Exercise) -> Result<(), ()> { let command = Command::new("git") - .args(["stash", "--"]) + .arg("stash") + .arg("--") .arg(&exercise.path) .spawn(); From ed0fcf8e3d05f5420b55370d4ff4ad8e0ded127b Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:49:05 +0100 Subject: [PATCH 08/13] Formatting --- src/main.rs | 7 ++----- src/verify.rs | 32 +++++++++++++++----------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index a06f0c5..a0b3af2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,10 +223,7 @@ fn main() { Subcommands::Watch { success_hints } => match watch(&exercises, verbose, success_hints) { Err(e) => { - println!( - "Error: Could not watch your progress. Error message was {:?}.", - e - ); + println!("Error: Could not watch your progress. Error message was {e:?}."); println!("Most likely you've run out of disk space or your 'inotify limit' has been reached."); std::process::exit(1); } @@ -280,7 +277,7 @@ fn spawn_watch_shell( if parts.is_empty() { println!("no command provided"); } else if let Err(e) = Command::new(parts[0]).args(&parts[1..]).status() { - println!("failed to execute command `{}`: {}", cmd, e); + println!("failed to execute command `{cmd}`: {e}"); } } else { println!("unknown command: {input}"); diff --git a/src/verify.rs b/src/verify.rs index aee2afa..3123e45 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -24,7 +24,7 @@ pub fn verify<'a>( .progress_chars("#>-"), ); bar.set_position(num_done as u64); - bar.set_message(format!("({:.1} %)", percentage)); + bar.set_message(format!("({percentage:.1} %)")); for exercise in exercises { let compile_result = match exercise.mode { @@ -37,7 +37,7 @@ pub fn verify<'a>( } percentage += 100.0 / total as f32; bar.inc(1); - bar.set_message(format!("({:.1} %)", percentage)); + bar.set_message(format!("({percentage:.1} %)")); if bar.position() == total as u64 { println!( "Progress: You completed {} / {} exercises ({:.1} %).", @@ -191,27 +191,25 @@ fn prompt_for_completion( Mode::Test => "The code is compiling, and the tests pass!", Mode::Clippy => clippy_success_msg, }; - println!(); + if no_emoji { - println!("~*~ {success_msg} ~*~") + println!("\n~*~ {success_msg} ~*~\n"); } else { - println!("šŸŽ‰ šŸŽ‰ {success_msg} šŸŽ‰ šŸŽ‰") + println!("\nšŸŽ‰ šŸŽ‰ {success_msg} šŸŽ‰ šŸŽ‰\n"); } - println!(); if let Some(output) = prompt_output { - println!("Output:"); - println!("{}", separator()); - println!("{output}"); - println!("{}", separator()); - println!(); + println!( + "Output:\n{separator}\n{output}\n{separator}\n", + separator = separator(), + ); } if success_hints { - println!("Hints:"); - println!("{}", separator()); - println!("{}", exercise.hint); - println!("{}", separator()); - println!(); + println!( + "Hints:\n{separator}\n{}\n{separator}\n", + exercise.hint, + separator = separator(), + ); } println!("You can keep working on this exercise,"); @@ -231,7 +229,7 @@ fn prompt_for_completion( "{:>2} {} {}", style(context_line.number).blue().bold(), style("|").blue(), - formatted_line + formatted_line, ); } From 1f2029ae5503024f71203893fe1eab7b90aa80af Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:49:25 +0100 Subject: [PATCH 09/13] Add missing semicolon --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a0b3af2..6884a0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -217,7 +217,7 @@ fn main() { println!("Failed to write rust-project.json to disk for rust-analyzer"); } else { println!("Successfully generated rust-project.json"); - println!("rust-analyzer will now parse exercises, restart your language server or editor") + println!("rust-analyzer will now parse exercises, restart your language server or editor"); } } From 980ffa2a2bb791992ef05ca9b05aadba62ec6abc Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:49:48 +0100 Subject: [PATCH 10/13] Use == on simple enums --- src/verify.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/verify.rs b/src/verify.rs index 3123e45..e2fa98f 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -51,6 +51,7 @@ pub fn verify<'a>( Ok(()) } +#[derive(PartialEq, Eq)] enum RunMode { Interactive, NonInteractive, @@ -124,7 +125,7 @@ fn compile_and_test( if verbose { println!("{}", output.stdout); } - if let RunMode::Interactive = run_mode { + if run_mode == RunMode::Interactive { Ok(prompt_for_completion(exercise, None, success_hints)) } else { Ok(true) From e89028581cd03c02cb0971a2772fa382667019a3 Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:49:55 +0100 Subject: [PATCH 11/13] Use == instead of eq --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6884a0e..559be69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -289,7 +289,7 @@ fn spawn_watch_shell( } fn find_exercise<'a>(name: &str, exercises: &'a [Exercise]) -> &'a Exercise { - if name.eq("next") { + if name == "next" { exercises .iter() .find(|e| !e.looks_done()) From a610fc1bc21a04017542208ef70a8010ee00c04c Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:50:10 +0100 Subject: [PATCH 12/13] Remove unneeded closure --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 559be69..eca73fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -335,7 +335,6 @@ fn watch( clear_screen(); - let to_owned_hint = |t: &Exercise| t.hint.to_owned(); let failed_exercise_hint = match verify( exercises.iter(), (0, exercises.len()), @@ -343,7 +342,7 @@ fn watch( success_hints, ) { Ok(_) => return Ok(WatchStatus::Finished), - Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))), + Err(exercise) => Arc::new(Mutex::new(Some(exercise.hint.clone()))), }; spawn_watch_shell(&failed_exercise_hint, Arc::clone(&should_quit)); loop { @@ -380,7 +379,7 @@ fn watch( Err(exercise) => { let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap(); - *failed_exercise_hint = Some(to_owned_hint(exercise)); + *failed_exercise_hint = Some(exercise.hint.clone()); } } } From 87001a68c0cc6b3498a253d0923e9c609355c4ee Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 17:50:29 +0100 Subject: [PATCH 13/13] The string doesn't have to be a raw string --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index eca73fa..141549c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -411,7 +411,7 @@ fn rustc_exists() -> bool { .unwrap_or(false) } -const DEFAULT_OUT: &str = r#"Thanks for installing Rustlings! +const DEFAULT_OUT: &str = "Thanks for installing Rustlings! Is this your first time? Don't worry, Rustlings was made for beginners! We are going to teach you a lot of things about Rust, but before we can get @@ -437,7 +437,7 @@ started, here's a couple of notes about how Rustlings operates: autocompletion, run the command `rustlings lsp`. Got all that? Great! To get started, run `rustlings watch` in order to get the first -exercise. Make sure to have your editor open!"#; +exercise. Make sure to have your editor open!"; const FENISH_LINE: &str = "+----------------------------------------------------+ | You made it to the Fe-nish line! |