1
1
use crate :: exercise:: { Exercise , Mode , State } ;
2
- use console:: { style, Emoji } ;
2
+ use console:: style;
3
3
use indicatif:: ProgressBar ;
4
4
5
5
pub fn verify < ' a > ( start_at : impl IntoIterator < Item = & ' a Exercise > ) -> Result < ( ) , & ' a Exercise > {
6
6
for exercise in start_at {
7
7
let compile_result = match exercise. mode {
8
- Mode :: Test => compile_and_test_interactively ( & exercise) ,
8
+ Mode :: Test => compile_and_test ( & exercise, RunMode :: Interactive ) ,
9
9
Mode :: Compile => compile_only ( & exercise) ,
10
10
} ;
11
11
if !compile_result. unwrap_or ( false ) {
@@ -15,78 +15,78 @@ pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<()
15
15
Ok ( ( ) )
16
16
}
17
17
18
+ enum RunMode {
19
+ Interactive ,
20
+ NonInteractive ,
21
+ }
22
+
18
23
pub fn test ( exercise : & Exercise ) -> Result < ( ) , ( ) > {
19
- compile_and_test ( exercise, true ) ?;
24
+ compile_and_test ( exercise, RunMode :: NonInteractive ) ?;
20
25
Ok ( ( ) )
21
26
}
22
27
23
28
fn compile_only ( exercise : & Exercise ) -> Result < bool , ( ) > {
24
29
let progress_bar = ProgressBar :: new_spinner ( ) ;
25
30
progress_bar. set_message ( format ! ( "Compiling {}..." , exercise) . as_str ( ) ) ;
26
31
progress_bar. enable_steady_tick ( 100 ) ;
27
- let compile_output = exercise. compile ( ) ;
32
+ let compilation_result = exercise. compile ( ) ;
28
33
progress_bar. finish_and_clear ( ) ;
29
- if compile_output. status . success ( ) {
30
- let formatstr = format ! ( "{} Successfully compiled {}!" , Emoji ( "✅" , "✓" ) , exercise) ;
31
- println ! ( "{}" , style( formatstr) . green( ) ) ;
32
- exercise. clean ( ) ;
33
- Ok ( prompt_for_completion ( & exercise) )
34
- } else {
35
- let formatstr = format ! (
36
- "{} Compilation of {} failed! Compiler error message:\n " ,
37
- Emoji ( "⚠️ " , "!" ) ,
38
- exercise
39
- ) ;
40
- println ! ( "{}" , style( formatstr) . red( ) ) ;
41
- println ! ( "{}" , String :: from_utf8_lossy( & compile_output. stderr) ) ;
42
- exercise. clean ( ) ;
43
- Err ( ( ) )
44
- }
45
- }
46
34
47
- fn compile_and_test_interactively ( exercise : & Exercise ) -> Result < bool , ( ) > {
48
- compile_and_test ( exercise, false )
35
+ match compilation_result {
36
+ Ok ( _) => {
37
+ success ! ( "Successfully compiled {}!" , exercise) ;
38
+ Ok ( prompt_for_completion ( & exercise) )
39
+ }
40
+ Err ( output) => {
41
+ warn ! (
42
+ "Compilation of {} failed! Compiler error message:\n " ,
43
+ exercise
44
+ ) ;
45
+ println ! ( "{}" , output. stderr) ;
46
+ Err ( ( ) )
47
+ }
48
+ }
49
49
}
50
50
51
- fn compile_and_test ( exercise : & Exercise , skip_prompt : bool ) -> Result < bool , ( ) > {
51
+ fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
52
52
let progress_bar = ProgressBar :: new_spinner ( ) ;
53
53
progress_bar. set_message ( format ! ( "Testing {}..." , exercise) . as_str ( ) ) ;
54
54
progress_bar. enable_steady_tick ( 100 ) ;
55
55
56
- let compile_output = exercise. compile ( ) ;
57
- if compile_output. status . success ( ) {
58
- progress_bar. set_message ( format ! ( "Running {}..." , exercise) . as_str ( ) ) ;
56
+ let compilation_result = exercise. compile ( ) ;
57
+
58
+ let compilation = match compilation_result {
59
+ Ok ( compilation) => compilation,
60
+ Err ( output) => {
61
+ progress_bar. finish_and_clear ( ) ;
62
+ warn ! (
63
+ "Compiling of {} failed! Please try again. Here's the output:" ,
64
+ exercise
65
+ ) ;
66
+ println ! ( "{}" , output. stderr) ;
67
+ return Err ( ( ) ) ;
68
+ }
69
+ } ;
59
70
60
- let runcmd = exercise . run ( ) ;
61
- progress_bar. finish_and_clear ( ) ;
71
+ let result = compilation . run ( ) ;
72
+ progress_bar. finish_and_clear ( ) ;
62
73
63
- if runcmd. status . success ( ) {
64
- let formatstr = format ! ( "{} Successfully tested {}!" , Emoji ( "✅" , "✓" ) , exercise) ;
65
- println ! ( "{}" , style( formatstr) . green( ) ) ;
66
- exercise. clean ( ) ;
67
- Ok ( skip_prompt || prompt_for_completion ( exercise) )
68
- } else {
69
- let formatstr = format ! (
70
- "{} Testing of {} failed! Please try again. Here's the output:" ,
71
- Emoji ( "⚠️ " , "!" ) ,
74
+ match result {
75
+ Ok ( _) => {
76
+ if let RunMode :: Interactive = run_mode {
77
+ Ok ( prompt_for_completion ( & exercise) )
78
+ } else {
79
+ Ok ( true )
80
+ }
81
+ }
82
+ Err ( output) => {
83
+ warn ! (
84
+ "Testing of {} failed! Please try again. Here's the output:" ,
72
85
exercise
73
86
) ;
74
- println ! ( "{}" , style( formatstr) . red( ) ) ;
75
- println ! ( "{}" , String :: from_utf8_lossy( & runcmd. stdout) ) ;
76
- exercise. clean ( ) ;
87
+ println ! ( "{}" , output. stdout) ;
77
88
Err ( ( ) )
78
89
}
79
- } else {
80
- progress_bar. finish_and_clear ( ) ;
81
- let formatstr = format ! (
82
- "{} Compiling of {} failed! Please try again. Here's the output:" ,
83
- Emoji ( "⚠️ " , "!" ) ,
84
- exercise
85
- ) ;
86
- println ! ( "{}" , style( formatstr) . red( ) ) ;
87
- println ! ( "{}" , String :: from_utf8_lossy( & compile_output. stderr) ) ;
88
- exercise. clean ( ) ;
89
- Err ( ( ) )
90
90
}
91
91
}
92
92
0 commit comments