1- use crate :: exercise:: { Exercise , Mode , State } ;
1+ use crate :: exercise:: { CompiledExercise , Exercise , Mode , State } ;
22use console:: style;
33use indicatif:: ProgressBar ;
44
55pub fn verify < ' a > ( start_at : impl IntoIterator < Item = & ' a Exercise > ) -> Result < ( ) , & ' a Exercise > {
66 for exercise in start_at {
77 let compile_result = match exercise. mode {
88 Mode :: Test => compile_and_test ( & exercise, RunMode :: Interactive ) ,
9- Mode :: Compile => compile_only ( & exercise) ,
9+ Mode :: Compile => compile_and_run_interactively ( & exercise) ,
1010 Mode :: Clippy => compile_only ( & exercise) ,
1111 } ;
1212 if !compile_result. unwrap_or ( false ) {
@@ -30,52 +30,53 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
3030 let progress_bar = ProgressBar :: new_spinner ( ) ;
3131 progress_bar. set_message ( format ! ( "Compiling {}..." , exercise) . as_str ( ) ) ;
3232 progress_bar. enable_steady_tick ( 100 ) ;
33- let compilation_result = exercise. compile ( ) ;
33+
34+ let _ = compile ( & exercise, & progress_bar) ?;
3435 progress_bar. finish_and_clear ( ) ;
3536
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 ) )
5039}
5140
52- fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
41+ fn compile_and_run_interactively ( exercise : & Exercise ) -> Result < bool , ( ) > {
5342 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 ( ) ) ;
5544 progress_bar. enable_steady_tick ( 100 ) ;
5645
57- let compilation_result = exercise . compile ( ) ;
46+ let compilation = compile ( & exercise , & progress_bar ) ? ;
5847
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,
6154 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) ;
6857 return Err ( ( ) ) ;
6958 }
7059 } ;
7160
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) ?;
7272 let result = compilation. run ( ) ;
7373 progress_bar. finish_and_clear ( ) ;
7474
7575 match result {
7676 Ok ( _) => {
77+ success ! ( "Successfully tested {}" , & exercise) ;
7778 if let RunMode :: Interactive = run_mode {
78- Ok ( prompt_for_completion ( & exercise) )
79+ Ok ( prompt_for_completion ( & exercise, None ) )
7980 } else {
8081 Ok ( true )
8182 }
@@ -91,7 +92,27 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
9192 }
9293}
9394
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 {
95116 let context = match exercise. state ( ) {
96117 State :: Done => return true ,
97118 State :: Pending ( context) => context,
@@ -106,6 +127,15 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
106127 println ! ( "" ) ;
107128 println ! ( "🎉 🎉 {} 🎉 🎉" , success_msg) ;
108129 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+
109139 println ! ( "You can keep working on this exercise," ) ;
110140 println ! (
111141 "or jump into the next one by removing the {} comment:" ,
@@ -129,3 +159,7 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
129159
130160 false
131161}
162+
163+ fn separator ( ) -> console:: StyledObject < & ' static str > {
164+ style ( "====================" ) . bold ( )
165+ }
0 commit comments