@@ -11,7 +11,7 @@ use std::ffi::OsStr;
11
11
use std:: fs;
12
12
use std:: io:: { self , prelude:: * } ;
13
13
use std:: path:: Path ;
14
- use std:: process:: { Command , Stdio } ;
14
+ use std:: process:: Command ;
15
15
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
16
16
use std:: sync:: mpsc:: { channel, RecvTimeoutError } ;
17
17
use std:: sync:: { Arc , Mutex } ;
@@ -92,24 +92,25 @@ fn main() {
92
92
println ! ( "\n {WELCOME}\n " ) ;
93
93
}
94
94
95
- if !Path :: new ( "info.toml" ) . exists ( ) {
96
- println ! (
97
- "{} must be run from the rustlings directory" ,
98
- std:: env:: current_exe( ) . unwrap( ) . to_str( ) . unwrap( )
99
- ) ;
100
- println ! ( "Try `cd rustlings/`!" ) ;
101
- std:: process:: exit ( 1 ) ;
102
- }
103
-
104
- if !rustc_exists ( ) {
95
+ if which:: which ( "rustc" ) . is_err ( ) {
105
96
println ! ( "We cannot find `rustc`." ) ;
106
97
println ! ( "Try running `rustc --version` to diagnose your problem." ) ;
107
98
println ! ( "For instructions on how to install Rust, check the README." ) ;
108
99
std:: process:: exit ( 1 ) ;
109
100
}
110
101
111
- let toml_str = & fs:: read_to_string ( "info.toml" ) . unwrap ( ) ;
112
- let exercises = toml:: from_str :: < ExerciseList > ( toml_str) . unwrap ( ) . exercises ;
102
+ let info_file = fs:: read_to_string ( "info.toml" ) . unwrap_or_else ( |e| {
103
+ match e. kind ( ) {
104
+ io:: ErrorKind :: NotFound => println ! (
105
+ "The program must be run from the rustlings directory\n Try `cd rustlings/`!" ,
106
+ ) ,
107
+ _ => println ! ( "Failed to read the info.toml file: {e}" ) ,
108
+ }
109
+ std:: process:: exit ( 1 ) ;
110
+ } ) ;
111
+ let exercises = toml_edit:: de:: from_str :: < ExerciseList > ( & info_file)
112
+ . unwrap ( )
113
+ . exercises ;
113
114
let verbose = args. nocapture ;
114
115
115
116
let command = args. command . unwrap_or_else ( || {
@@ -218,16 +219,13 @@ fn main() {
218
219
println ! ( "Failed to write rust-project.json to disk for rust-analyzer" ) ;
219
220
} else {
220
221
println ! ( "Successfully generated rust-project.json" ) ;
221
- println ! ( "rust-analyzer will now parse exercises, restart your language server or editor" )
222
+ println ! ( "rust-analyzer will now parse exercises, restart your language server or editor" ) ;
222
223
}
223
224
}
224
225
225
226
Subcommands :: Watch { success_hints } => match watch ( & exercises, verbose, success_hints) {
226
227
Err ( e) => {
227
- println ! (
228
- "Error: Could not watch your progress. Error message was {:?}." ,
229
- e
230
- ) ;
228
+ println ! ( "Error: Could not watch your progress. Error message was {e:?}." ) ;
231
229
println ! ( "Most likely you've run out of disk space or your 'inotify limit' has been reached." ) ;
232
230
std:: process:: exit ( 1 ) ;
233
231
}
@@ -295,7 +293,7 @@ fn spawn_watch_shell(
295
293
}
296
294
297
295
fn find_exercise < ' a > ( name : & str , exercises : & ' a [ Exercise ] ) -> & ' a Exercise {
298
- if name. eq ( "next" ) {
296
+ if name == "next" {
299
297
exercises
300
298
. iter ( )
301
299
. find ( |e| !e. looks_done ( ) )
@@ -341,15 +339,14 @@ fn watch(
341
339
342
340
clear_screen ( ) ;
343
341
344
- let to_owned_hint = |t : & Exercise | t. hint . to_owned ( ) ;
345
342
let failed_exercise_hint = match verify (
346
343
exercises. iter ( ) ,
347
344
( 0 , exercises. len ( ) ) ,
348
345
verbose,
349
346
success_hints,
350
347
) {
351
348
Ok ( _) => return Ok ( WatchStatus :: Finished ) ,
352
- Err ( exercise) => Arc :: new ( Mutex :: new ( Some ( to_owned_hint ( exercise) ) ) ) ,
349
+ Err ( exercise) => Arc :: new ( Mutex :: new ( Some ( exercise. hint . clone ( ) ) ) ) ,
353
350
} ;
354
351
spawn_watch_shell ( Arc :: clone ( & failed_exercise_hint) , Arc :: clone ( & should_quit) ) ;
355
352
loop {
@@ -386,7 +383,7 @@ fn watch(
386
383
Err ( exercise) => {
387
384
let mut failed_exercise_hint =
388
385
failed_exercise_hint. lock ( ) . unwrap ( ) ;
389
- * failed_exercise_hint = Some ( to_owned_hint ( exercise) ) ;
386
+ * failed_exercise_hint = Some ( exercise. hint . clone ( ) ) ;
390
387
}
391
388
}
392
389
}
@@ -406,19 +403,7 @@ fn watch(
406
403
}
407
404
}
408
405
409
- fn rustc_exists ( ) -> bool {
410
- Command :: new ( "rustc" )
411
- . args ( [ "--version" ] )
412
- . stdout ( Stdio :: null ( ) )
413
- . stderr ( Stdio :: null ( ) )
414
- . stdin ( Stdio :: null ( ) )
415
- . spawn ( )
416
- . and_then ( |mut child| child. wait ( ) )
417
- . map ( |status| status. success ( ) )
418
- . unwrap_or ( false )
419
- }
420
-
421
- const DEFAULT_OUT : & str = r#"Thanks for installing Rustlings!
406
+ const DEFAULT_OUT : & str = "Thanks for installing Rustlings!
422
407
423
408
Is this your first time? Don't worry, Rustlings was made for beginners! We are
424
409
going to teach you a lot of things about Rust, but before we can get
@@ -444,7 +429,7 @@ started, here's a couple of notes about how Rustlings operates:
444
429
autocompletion, run the command `rustlings lsp`.
445
430
446
431
Got all that? Great! To get started, run `rustlings watch` in order to get the first
447
- exercise. Make sure to have your editor open!"# ;
432
+ exercise. Make sure to have your editor open!" ;
448
433
449
434
const FENISH_LINE : & str = "+----------------------------------------------------+
450
435
| You made it to the Fe-nish line! |
0 commit comments