1
1
use clap:: { App , Arg , SubCommand , crate_version} ;
2
- use console:: { style, Emoji } ;
3
- use indicatif:: ProgressBar ;
4
2
use syntect:: easy:: HighlightFile ;
5
3
use syntect:: parsing:: SyntaxSet ;
6
4
use syntect:: highlighting:: { ThemeSet , Style } ;
7
5
use syntect:: util:: { as_24_bit_terminal_escaped} ;
8
- use std:: fs:: remove_file;
9
6
use std:: io:: BufRead ;
10
- use std:: process:: Command ;
11
7
use std:: sync:: mpsc:: channel;
12
8
use std:: time:: Duration ;
13
9
use notify:: DebouncedEvent ;
14
10
use notify:: { RecommendedWatcher , Watcher , RecursiveMode } ;
11
+ use crate :: verify:: verify;
12
+ use crate :: run:: run;
13
+
14
+ mod run;
15
+ mod verify;
16
+ mod util;
15
17
16
18
fn main ( ) {
17
19
let matches = App :: new ( "rustlings" )
@@ -38,48 +40,7 @@ fn main() {
38
40
println ! ( "" ) ;
39
41
40
42
if let Some ( matches) = matches. subcommand_matches ( "run" ) {
41
- if let Some ( filename) = matches. value_of ( "file" ) {
42
- let bar = ProgressBar :: new_spinner ( ) ;
43
- bar. set_message ( format ! ( "Compiling {}..." , filename) . as_str ( ) ) ;
44
- bar. enable_steady_tick ( 100 ) ;
45
- let compilecmd = Command :: new ( "rustc" )
46
- . args ( & [ filename, "-o" , "temp" ] )
47
- . output ( )
48
- . expect ( "fail" ) ;
49
- bar. set_message ( format ! ( "Running {}..." , filename) . as_str ( ) ) ;
50
- if compilecmd. status . success ( ) {
51
- let runcmd = Command :: new ( "./temp" ) . output ( ) . expect ( "fail" ) ;
52
- bar. finish_and_clear ( ) ;
53
-
54
- if runcmd. status . success ( ) {
55
- println ! ( "{}" , String :: from_utf8_lossy( & runcmd. stdout) ) ;
56
- let formatstr =
57
- format ! ( "{} Successfully ran {}" , Emoji ( "✅" , "✓" ) , filename) ;
58
- println ! ( "{}" , style( formatstr) . green( ) ) ;
59
- clean ( ) ;
60
- } else {
61
- println ! ( "{}" , String :: from_utf8_lossy( & runcmd. stdout) ) ;
62
- println ! ( "{}" , String :: from_utf8_lossy( & runcmd. stderr) ) ;
63
-
64
- let formatstr =
65
- format ! ( "{} Ran {} with errors" , Emoji ( "⚠️ " , "!" ) , filename) ;
66
- println ! ( "{}" , style( formatstr) . red( ) ) ;
67
- clean ( ) ;
68
- }
69
- } else {
70
- bar. finish_and_clear ( ) ;
71
- let formatstr = format ! (
72
- "{} Compilation of {} failed! Compiler error message:\n " ,
73
- Emoji ( "⚠️ " , "!" ) ,
74
- filename
75
- ) ;
76
- println ! ( "{}" , style( formatstr) . red( ) ) ;
77
- println ! ( "{}" , String :: from_utf8_lossy( & compilecmd. stderr) ) ;
78
- clean ( ) ;
79
- }
80
- } else {
81
- panic ! ( "Please supply a filename!" ) ;
82
- }
43
+ run ( matches. clone ( ) ) ;
83
44
}
84
45
85
46
if let Some ( _) = matches. subcommand_matches ( "verify" ) {
@@ -128,112 +89,3 @@ fn watch() -> notify::Result<()> {
128
89
}
129
90
}
130
91
}
131
-
132
- fn verify ( ) -> Result < ( ) , ( ) > {
133
- compile_only ( "exercises/ex1.rs" ) ?;
134
- compile_only ( "exercises/ex2.rs" ) ?;
135
- compile_only ( "exercises/ex3.rs" ) ?;
136
- compile_only ( "exercises/ex4.rs" ) ?;
137
- compile_only ( "exercises/ex5.rs" ) ?;
138
- compile_only ( "exercises/variables/variables1.rs" ) ?;
139
- compile_only ( "exercises/variables/variables2.rs" ) ?;
140
- compile_only ( "exercises/variables/variables3.rs" ) ?;
141
- compile_only ( "exercises/variables/variables4.rs" ) ?;
142
- compile_only ( "exercises/functions/functions1.rs" ) ?;
143
- compile_only ( "exercises/functions/functions2.rs" ) ?;
144
- compile_only ( "exercises/functions/functions3.rs" ) ?;
145
- compile_only ( "exercises/functions/functions4.rs" ) ?;
146
- compile_only ( "exercises/functions/functions5.rs" ) ?;
147
- compile_only ( "exercises/primitive_types/primitive_types1.rs" ) ?;
148
- compile_only ( "exercises/primitive_types/primitive_types2.rs" ) ?;
149
- compile_only ( "exercises/primitive_types/primitive_types3.rs" ) ?;
150
- compile_only ( "exercises/primitive_types/primitive_types4.rs" ) ?;
151
- compile_only ( "exercises/primitive_types/primitive_types5.rs" ) ?;
152
- compile_only ( "exercises/primitive_types/primitive_types6.rs" ) ?;
153
- test ( "exercises/tests/tests1.rs" ) ?;
154
- test ( "exercises/tests/tests2.rs" ) ?;
155
- test ( "exercises/tests/tests3.rs" ) ?;
156
- test ( "exercises/tests/tests4.rs" ) ?;
157
- test ( "exercises/if/if1.rs" ) ?;
158
- compile_only ( "exercises/strings/strings1.rs" ) ?;
159
- compile_only ( "exercises/strings/strings2.rs" ) ?;
160
- compile_only ( "exercises/strings/strings3.rs" ) ?;
161
- compile_only ( "exercises/move_semantics/move_semantics1.rs" ) ?;
162
- compile_only ( "exercises/move_semantics/move_semantics2.rs" ) ?;
163
- compile_only ( "exercises/move_semantics/move_semantics3.rs" ) ?;
164
- compile_only ( "exercises/move_semantics/move_semantics4.rs" ) ?;
165
- compile_only ( "exercises/modules/modules1.rs" ) ?;
166
- compile_only ( "exercises/modules/modules2.rs" ) ?;
167
- compile_only ( "exercises/macros/macros1.rs" ) ?;
168
- compile_only ( "exercises/macros/macros2.rs" ) ?;
169
- compile_only ( "exercises/macros/macros3.rs" ) ?;
170
- compile_only ( "exercises/macros/macros4.rs" ) ?;
171
- test ( "exercises/error_handling/errors1.rs" ) ?;
172
- test ( "exercises/error_handling/errors2.rs" ) ?;
173
- test ( "exercises/error_handling/errors3.rs" ) ?;
174
- test ( "exercises/error_handling/errorsn.rs" ) ?;
175
- compile_only ( "exercises/error_handling/option1.rs" ) ?;
176
- test ( "exercises/error_handling/result1.rs" ) ?;
177
- Ok ( ( ) )
178
- }
179
-
180
- fn compile_only ( filename : & str ) -> Result < ( ) , ( ) > {
181
- let bar = ProgressBar :: new_spinner ( ) ;
182
- bar. set_message ( format ! ( "Compiling {}..." , filename) . as_str ( ) ) ;
183
- bar. enable_steady_tick ( 100 ) ;
184
- let compilecmd = Command :: new ( "rustc" )
185
- . args ( & [ filename, "-o" , "temp" , "--color" , "always" ] )
186
- . output ( )
187
- . expect ( "fail" ) ;
188
- bar. finish_and_clear ( ) ;
189
- if compilecmd. status . success ( ) {
190
- let formatstr = format ! (
191
- "{} Successfully compiled {}!" ,
192
- Emoji ( "✅" , "✓" ) ,
193
- filename
194
- ) ;
195
- println ! ( "{}" , style( formatstr) . green( ) ) ;
196
- clean ( ) ;
197
- Ok ( ( ) )
198
- } else {
199
- let formatstr = format ! (
200
- "{} Compilation of {} failed! Compiler error message:\n " ,
201
- Emoji ( "⚠️ " , "!" ) ,
202
- filename
203
- ) ;
204
- println ! ( "{}" , style( formatstr) . red( ) ) ;
205
- println ! ( "{}" , String :: from_utf8_lossy( & compilecmd. stderr) ) ;
206
- clean ( ) ;
207
- Err ( ( ) )
208
- }
209
- }
210
-
211
- fn test ( filename : & str ) -> Result < ( ) , ( ) > {
212
- let bar = ProgressBar :: new_spinner ( ) ;
213
- bar. set_message ( format ! ( "Testing {}..." , filename) . as_str ( ) ) ;
214
- bar. enable_steady_tick ( 100 ) ;
215
- let testcmd = Command :: new ( "rustc" )
216
- . args ( & [ "--test" , filename, "-o" , "temp" ] )
217
- . output ( )
218
- . expect ( "fail" ) ;
219
- bar. finish_and_clear ( ) ;
220
- if testcmd. status . success ( ) {
221
- let formatstr = format ! ( "{} Successfully tested {}!" , Emoji ( "✅" , "✓" ) , filename) ;
222
- println ! ( "{}" , style( formatstr) . green( ) ) ;
223
- clean ( ) ;
224
- Ok ( ( ) )
225
- } else {
226
- let formatstr = format ! (
227
- "{} Testing of {} failed! Please try again." ,
228
- Emoji ( "⚠️ " , "!" ) ,
229
- filename
230
- ) ;
231
- println ! ( "{}" , style( formatstr) . red( ) ) ;
232
- clean ( ) ;
233
- Err ( ( ) )
234
- }
235
- }
236
-
237
- fn clean ( ) {
238
- let _ignored = remove_file ( "temp" ) ;
239
- }
0 commit comments