1
- use crate :: exercise:: { Exercise , Mode , State } ;
1
+ use crate :: exercise:: { CompiledExercise , Exercise , Mode , State } ;
2
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
8
Mode :: Test => compile_and_test ( & exercise, RunMode :: Interactive ) ,
9
- Mode :: Compile => compile_only ( & exercise) ,
9
+ Mode :: Compile => compile_and_run_interactively ( & exercise) ,
10
10
Mode :: Clippy => compile_only ( & exercise) ,
11
11
} ;
12
12
if !compile_result. unwrap_or ( false ) {
@@ -30,52 +30,53 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
30
30
let progress_bar = ProgressBar :: new_spinner ( ) ;
31
31
progress_bar. set_message ( format ! ( "Compiling {}..." , exercise) . as_str ( ) ) ;
32
32
progress_bar. enable_steady_tick ( 100 ) ;
33
- let compilation_result = exercise. compile ( ) ;
33
+
34
+ let _ = compile ( & exercise, & progress_bar) ?;
34
35
progress_bar. finish_and_clear ( ) ;
35
36
36
- match compilation_result {
37
- Ok ( _) => {
38
- success ! ( "Successfully compiled {}!" , exercise) ;
39
- Ok ( prompt_for_completion ( & exercise) )
40
- }
41
- Err ( output) => {
42
- warn ! (
43
- "Compilation of {} failed! Compiler error message:\n " ,
44
- exercise
45
- ) ;
46
- println ! ( "{}" , output. stderr) ;
47
- Err ( ( ) )
48
- }
49
- }
37
+ success ! ( "Successfully compiled {}!" , exercise) ;
38
+ Ok ( prompt_for_completion ( & exercise, None ) )
50
39
}
51
40
52
- fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
41
+ fn compile_and_run_interactively ( exercise : & Exercise ) -> Result < bool , ( ) > {
53
42
let progress_bar = ProgressBar :: new_spinner ( ) ;
54
- progress_bar. set_message ( format ! ( "Testing {}..." , exercise) . as_str ( ) ) ;
43
+ progress_bar. set_message ( format ! ( "Compiling {}..." , exercise) . as_str ( ) ) ;
55
44
progress_bar. enable_steady_tick ( 100 ) ;
56
45
57
- let compilation_result = exercise . compile ( ) ;
46
+ let compilation = compile ( & exercise , & progress_bar ) ? ;
58
47
59
- let compilation = match compilation_result {
60
- Ok ( compilation) => compilation,
48
+ progress_bar. set_message ( format ! ( "Running {}..." , exercise) . as_str ( ) ) ;
49
+ let result = compilation. run ( ) ;
50
+ progress_bar. finish_and_clear ( ) ;
51
+
52
+ let output = match result {
53
+ Ok ( output) => output,
61
54
Err ( output) => {
62
- progress_bar. finish_and_clear ( ) ;
63
- warn ! (
64
- "Compiling of {} failed! Please try again. Here's the output:" ,
65
- exercise
66
- ) ;
67
- println ! ( "{}" , output. stderr) ;
55
+ warn ! ( "Ran {} with errors" , exercise) ;
56
+ println ! ( "{}" , output. stdout) ;
68
57
return Err ( ( ) ) ;
69
58
}
70
59
} ;
71
60
61
+ success ! ( "Successfully ran {}!" , exercise) ;
62
+
63
+ Ok ( prompt_for_completion ( & exercise, Some ( output. stdout ) ) )
64
+ }
65
+
66
+ fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
67
+ let progress_bar = ProgressBar :: new_spinner ( ) ;
68
+ progress_bar. set_message ( format ! ( "Testing {}..." , exercise) . as_str ( ) ) ;
69
+ progress_bar. enable_steady_tick ( 100 ) ;
70
+
71
+ let compilation = compile ( exercise, & progress_bar) ?;
72
72
let result = compilation. run ( ) ;
73
73
progress_bar. finish_and_clear ( ) ;
74
74
75
75
match result {
76
76
Ok ( _) => {
77
+ success ! ( "Successfully tested {}" , & exercise) ;
77
78
if let RunMode :: Interactive = run_mode {
78
- Ok ( prompt_for_completion ( & exercise) )
79
+ Ok ( prompt_for_completion ( & exercise, None ) )
79
80
} else {
80
81
Ok ( true )
81
82
}
@@ -91,7 +92,27 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
91
92
}
92
93
}
93
94
94
- fn prompt_for_completion ( exercise : & Exercise ) -> bool {
95
+ fn compile < ' a , ' b > (
96
+ exercise : & ' a Exercise ,
97
+ progress_bar : & ' b ProgressBar ,
98
+ ) -> Result < CompiledExercise < ' a > , ( ) > {
99
+ let compilation_result = exercise. compile ( ) ;
100
+
101
+ match compilation_result {
102
+ Ok ( compilation) => Ok ( compilation) ,
103
+ Err ( output) => {
104
+ progress_bar. finish_and_clear ( ) ;
105
+ warn ! (
106
+ "Compiling of {} failed! Please try again. Here's the output:" ,
107
+ exercise
108
+ ) ;
109
+ println ! ( "{}" , output. stderr) ;
110
+ Err ( ( ) )
111
+ }
112
+ }
113
+ }
114
+
115
+ fn prompt_for_completion ( exercise : & Exercise , prompt_output : Option < String > ) -> bool {
95
116
let context = match exercise. state ( ) {
96
117
State :: Done => return true ,
97
118
State :: Pending ( context) => context,
@@ -106,6 +127,15 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
106
127
println ! ( "" ) ;
107
128
println ! ( "🎉 🎉 {} 🎉 🎉" , success_msg) ;
108
129
println ! ( "" ) ;
130
+
131
+ if let Some ( output) = prompt_output {
132
+ println ! ( "Output:" ) ;
133
+ println ! ( "{}" , separator( ) ) ;
134
+ println ! ( "{}" , output) ;
135
+ println ! ( "{}" , separator( ) ) ;
136
+ println ! ( "" ) ;
137
+ }
138
+
109
139
println ! ( "You can keep working on this exercise," ) ;
110
140
println ! (
111
141
"or jump into the next one by removing the {} comment:" ,
@@ -129,3 +159,7 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
129
159
130
160
false
131
161
}
162
+
163
+ fn separator ( ) -> console:: StyledObject < & ' static str > {
164
+ style ( "====================" ) . bold ( )
165
+ }
0 commit comments