@@ -8,7 +8,7 @@ use crossterm::{
8
8
use std:: {
9
9
io:: { self , IsTerminal , Write } ,
10
10
path:: Path ,
11
- process:: exit ,
11
+ process:: ExitCode ,
12
12
} ;
13
13
use term:: { clear_terminal, press_enter_prompt} ;
14
14
@@ -51,8 +51,8 @@ enum Subcommands {
51
51
/// The name of the exercise
52
52
name : Option < String > ,
53
53
} ,
54
- /// Run all the exercises, marking them as done or pending accordingly.
55
- RunAll ,
54
+ /// Check all the exercises, marking them as done or pending accordingly.
55
+ CheckAll ,
56
56
/// Reset a single exercise
57
57
Reset {
58
58
/// The name of the exercise
@@ -68,22 +68,26 @@ enum Subcommands {
68
68
Dev ( DevCommands ) ,
69
69
}
70
70
71
- fn main ( ) -> Result < ( ) > {
71
+ fn main ( ) -> Result < ExitCode > {
72
72
let args = Args :: parse ( ) ;
73
73
74
74
if cfg ! ( not( debug_assertions) ) && Path :: new ( "dev/rustlings-repo.txt" ) . exists ( ) {
75
75
bail ! ( "{OLD_METHOD_ERR}" ) ;
76
76
}
77
77
78
- match args. command {
79
- Some ( Subcommands :: Init ) => return init:: init ( ) . context ( "Initialization failed" ) ,
80
- Some ( Subcommands :: Dev ( dev_command) ) => return dev_command. run ( ) ,
81
- _ => ( ) ,
78
+ ' priority_cmd: {
79
+ match args. command {
80
+ Some ( Subcommands :: Init ) => init:: init ( ) . context ( "Initialization failed" ) ?,
81
+ Some ( Subcommands :: Dev ( dev_command) ) => dev_command. run ( ) ?,
82
+ _ => break ' priority_cmd,
83
+ }
84
+
85
+ return Ok ( ExitCode :: SUCCESS ) ;
82
86
}
83
87
84
88
if !Path :: new ( "exercises" ) . is_dir ( ) {
85
89
println ! ( "{PRE_INIT_MSG}" ) ;
86
- exit ( 1 ) ;
90
+ return Ok ( ExitCode :: FAILURE ) ;
87
91
}
88
92
89
93
let info_file = InfoFile :: parse ( ) ?;
@@ -142,33 +146,29 @@ fn main() -> Result<()> {
142
146
if let Some ( name) = name {
143
147
app_state. set_current_exercise_by_name ( & name) ?;
144
148
}
145
- run:: run ( & mut app_state) ? ;
149
+ return run:: run ( & mut app_state) ;
146
150
}
147
- Some ( Subcommands :: RunAll ) => {
151
+ Some ( Subcommands :: CheckAll ) => {
148
152
let mut stdout = io:: stdout ( ) . lock ( ) ;
149
- if let Some ( first_fail) = app_state. check_all_exercises ( & mut stdout) ? {
150
- let pending = app_state
151
- . exercises ( )
152
- . iter ( )
153
- . filter ( |exercise| !exercise. done )
154
- . count ( ) ;
153
+ if let Some ( first_pending_exercise_ind) = app_state. check_all_exercises ( & mut stdout) ? {
155
154
if app_state. current_exercise ( ) . done {
156
- app_state. set_current_exercise_ind ( first_fail ) ?;
155
+ app_state. set_current_exercise_ind ( first_pending_exercise_ind ) ?;
157
156
}
158
- stdout
159
- . queue ( SetForegroundColor ( Color :: Red ) ) ?
160
- . queue ( Print ( format ! ( "{pending}" ) ) ) ?
161
- . queue ( ResetColor ) ?;
157
+
158
+ let pending = app_state. n_pending ( ) ;
162
159
if pending == 1 {
163
- stdout. queue ( Print ( " exercise has some errors : " ) ) ?;
160
+ stdout. queue ( Print ( "One exercise pending : " ) ) ?;
164
161
} else {
165
- stdout. queue ( Print ( " exercises have errors, including " ) ) ?;
162
+ stdout. queue ( SetForegroundColor ( Color :: Red ) ) ?;
163
+ write ! ( stdout, "{pending}" ) ?;
164
+ stdout. queue ( ResetColor ) ?;
165
+ stdout. queue ( Print ( " exercises are pending. The first: " ) ) ?;
166
166
}
167
167
app_state
168
168
. current_exercise ( )
169
169
. terminal_file_link ( & mut stdout) ?;
170
- stdout. write_all ( b". \n " ) ?;
171
- exit ( 1 ) ;
170
+ stdout. write_all ( b"\n " ) ?;
171
+ return Ok ( ExitCode :: FAILURE ) ;
172
172
} else {
173
173
app_state. render_final_message ( & mut stdout) ?;
174
174
}
@@ -188,7 +188,7 @@ fn main() -> Result<()> {
188
188
Some ( Subcommands :: Init | Subcommands :: Dev ( _) ) => ( ) ,
189
189
}
190
190
191
- Ok ( ( ) )
191
+ Ok ( ExitCode :: SUCCESS )
192
192
}
193
193
194
194
const OLD_METHOD_ERR : & str =
0 commit comments