11use crate :: exercise:: { Exercise , Mode , State } ;
2- use console:: { style, Emoji } ;
2+ use 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 {
8- Mode :: Test => compile_and_test_interactively ( & exercise) ,
8+ Mode :: Test => compile_and_test ( & exercise, RunMode :: Interactive ) ,
99 Mode :: Compile => compile_only ( & exercise) ,
1010 } ;
1111 if !compile_result. unwrap_or ( false ) {
@@ -15,78 +15,78 @@ pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<()
1515 Ok ( ( ) )
1616}
1717
18+ enum RunMode {
19+ Interactive ,
20+ NonInteractive ,
21+ }
22+
1823pub fn test ( exercise : & Exercise ) -> Result < ( ) , ( ) > {
19- compile_and_test ( exercise, true ) ?;
24+ compile_and_test ( exercise, RunMode :: NonInteractive ) ?;
2025 Ok ( ( ) )
2126}
2227
2328fn compile_only ( exercise : & Exercise ) -> Result < bool , ( ) > {
2429 let progress_bar = ProgressBar :: new_spinner ( ) ;
2530 progress_bar. set_message ( format ! ( "Compiling {}..." , exercise) . as_str ( ) ) ;
2631 progress_bar. enable_steady_tick ( 100 ) ;
27- let compile_output = exercise. compile ( ) ;
32+ let compilation_result = exercise. compile ( ) ;
2833 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- }
4634
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+ }
4949}
5050
51- fn compile_and_test ( exercise : & Exercise , skip_prompt : bool ) -> Result < bool , ( ) > {
51+ fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
5252 let progress_bar = ProgressBar :: new_spinner ( ) ;
5353 progress_bar. set_message ( format ! ( "Testing {}..." , exercise) . as_str ( ) ) ;
5454 progress_bar. enable_steady_tick ( 100 ) ;
5555
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+ } ;
5970
60- let runcmd = exercise . run ( ) ;
61- progress_bar. finish_and_clear ( ) ;
71+ let result = compilation . run ( ) ;
72+ progress_bar. finish_and_clear ( ) ;
6273
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:" ,
7285 exercise
7386 ) ;
74- println ! ( "{}" , style( formatstr) . red( ) ) ;
75- println ! ( "{}" , String :: from_utf8_lossy( & runcmd. stdout) ) ;
76- exercise. clean ( ) ;
87+ println ! ( "{}" , output. stdout) ;
7788 Err ( ( ) )
7889 }
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 ( ( ) )
9090 }
9191}
9292
0 commit comments