@ -2,10 +2,18 @@ use crate::exercise::{CompiledExercise, Exercise, Mode, State};
use console ::style ;
use indicatif ::ProgressBar ;
pub fn verify < ' a > ( start_at : impl IntoIterator < Item = & ' a Exercise > ) -> Result < ( ) , & ' a Exercise > {
// Verify that the provided container of Exercise objects
// can be compiled and run without any failures.
// Any such failures will be reported to the end user.
// If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed.
pub fn verify < ' a > (
start_at : impl IntoIterator < Item = & ' a Exercise > ,
verbose : bool
) -> Result < ( ) , & ' a Exercise > {
for exercise in start_at {
let compile_result = match exercise . mode {
Mode ::Test = > compile_and_test ( & exercise , RunMode ::Interactive ) ,
Mode ::Test = > compile_and_test ( & exercise , RunMode ::Interactive , verbose ),
Mode ::Compile = > compile_and_run_interactively ( & exercise ) ,
Mode ::Clippy = > compile_only ( & exercise ) ,
} ;
@ -21,11 +29,13 @@ enum RunMode {
NonInteractive ,
}
pub fn test ( exercise : & Exercise ) -> Result < ( ) , ( ) > {
compile_and_test ( exercise , RunMode ::NonInteractive ) ? ;
// Compile and run the resulting test harness of the given Exercise
pub fn test ( exercise : & Exercise , verbose : bool ) -> Result < ( ) , ( ) > {
compile_and_test ( exercise , RunMode ::NonInteractive , verbose ) ? ;
Ok ( ( ) )
}
// Invoke the rust compiler without running the resulting binary
fn compile_only ( exercise : & Exercise ) -> Result < bool , ( ) > {
let progress_bar = ProgressBar ::new_spinner ( ) ;
progress_bar . set_message ( format! ( "Compiling {}..." , exercise ) . as_str ( ) ) ;
@ -38,6 +48,7 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
Ok ( prompt_for_completion ( & exercise , None ) )
}
// Compile the given Exercise and run the resulting binary in an interactive mode
fn compile_and_run_interactively ( exercise : & Exercise ) -> Result < bool , ( ) > {
let progress_bar = ProgressBar ::new_spinner ( ) ;
progress_bar . set_message ( format! ( "Compiling {}..." , exercise ) . as_str ( ) ) ;
@ -63,7 +74,11 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
Ok ( prompt_for_completion ( & exercise , Some ( output . stdout ) ) )
}
fn compile_and_test ( exercise : & Exercise , run_mode : RunMode ) -> Result < bool , ( ) > {
// Compile the given Exercise as a test harness and display
// the output if verbose is set to true
fn compile_and_test (
exercise : & Exercise , run_mode : RunMode , verbose : bool
) -> Result < bool , ( ) > {
let progress_bar = ProgressBar ::new_spinner ( ) ;
progress_bar . set_message ( format! ( "Testing {}..." , exercise ) . as_str ( ) ) ;
progress_bar . enable_steady_tick ( 100 ) ;
@ -73,7 +88,10 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
progress_bar . finish_and_clear ( ) ;
match result {
Ok ( _ ) = > {
Ok ( output ) = > {
if verbose {
println! ( "{}" , output . stdout ) ;
}
success ! ( "Successfully tested {}" , & exercise ) ;
if let RunMode ::Interactive = run_mode {
Ok ( prompt_for_completion ( & exercise , None ) )
@ -92,6 +110,8 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
}
}
// Compile the given Exercise and return an object with information
// about the state of the compilation
fn compile < ' a , ' b > (
exercise : & ' a Exercise ,
progress_bar : & ' b ProgressBar ,