|
1 | | -use crate::util; |
| 1 | +use crate::exercise::{Mode, Exercise}; |
2 | 2 | use crate::verify::test; |
3 | 3 | use console::{style, Emoji}; |
4 | 4 | use indicatif::ProgressBar; |
5 | | -use std::fs; |
6 | | -use toml::Value; |
7 | 5 |
|
8 | | -pub fn run(matches: clap::ArgMatches) -> Result<(), ()> { |
9 | | - if let Some(filename) = matches.value_of("file") { |
10 | | - let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap(); |
11 | | - let tomlvec: &Vec<Value> = toml.get("exercises").unwrap().as_array().unwrap(); |
12 | | - let mut exercises = tomlvec.clone(); |
13 | | - exercises.retain(|i| i.get("path").unwrap().as_str().unwrap() == filename); |
14 | | - if exercises.is_empty() { |
15 | | - println!("No exercise found for your filename!"); |
16 | | - std::process::exit(1); |
17 | | - } |
18 | | - |
19 | | - let exercise: &Value = &exercises[0]; |
20 | | - match exercise.get("mode").unwrap().as_str().unwrap() { |
21 | | - "test" => test(exercise.get("path").unwrap().as_str().unwrap())?, |
22 | | - "compile" => compile_and_run(exercise.get("path").unwrap().as_str().unwrap())?, |
23 | | - _ => (), |
24 | | - } |
25 | | - Ok(()) |
26 | | - } else { |
27 | | - panic!("Please supply a filename!"); |
| 6 | +pub fn run(exercise: &Exercise) -> Result<(), ()> { |
| 7 | + match exercise.mode { |
| 8 | + Mode::Test => test(exercise)?, |
| 9 | + Mode::Compile => compile_and_run(exercise)?, |
28 | 10 | } |
| 11 | + Ok(()) |
29 | 12 | } |
30 | 13 |
|
31 | | -pub fn compile_and_run(filename: &str) -> Result<(), ()> { |
| 14 | +pub fn compile_and_run(exercise: &Exercise) -> Result<(), ()> { |
32 | 15 | let progress_bar = ProgressBar::new_spinner(); |
33 | | - progress_bar.set_message(format!("Compiling {}...", filename).as_str()); |
| 16 | + progress_bar.set_message(format!("Compiling {}...", exercise).as_str()); |
34 | 17 | progress_bar.enable_steady_tick(100); |
35 | 18 |
|
36 | | - let compilecmd = util::compile_cmd(filename); |
37 | | - progress_bar.set_message(format!("Running {}...", filename).as_str()); |
| 19 | + let compilecmd = exercise.compile(); |
| 20 | + progress_bar.set_message(format!("Running {}...", exercise).as_str()); |
38 | 21 | if compilecmd.status.success() { |
39 | | - let runcmd = util::run_cmd(); |
| 22 | + let runcmd = exercise.run(); |
40 | 23 | progress_bar.finish_and_clear(); |
41 | 24 |
|
42 | 25 | if runcmd.status.success() { |
43 | 26 | println!("{}", String::from_utf8_lossy(&runcmd.stdout)); |
44 | | - let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename); |
| 27 | + let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), exercise); |
45 | 28 | println!("{}", style(formatstr).green()); |
46 | | - util::clean(); |
| 29 | + exercise.clean(); |
47 | 30 | Ok(()) |
48 | 31 | } else { |
49 | 32 | println!("{}", String::from_utf8_lossy(&runcmd.stdout)); |
50 | 33 | println!("{}", String::from_utf8_lossy(&runcmd.stderr)); |
51 | 34 |
|
52 | | - let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename); |
| 35 | + let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), exercise); |
53 | 36 | println!("{}", style(formatstr).red()); |
54 | | - util::clean(); |
| 37 | + exercise.clean(); |
55 | 38 | Err(()) |
56 | 39 | } |
57 | 40 | } else { |
58 | 41 | progress_bar.finish_and_clear(); |
59 | 42 | let formatstr = format!( |
60 | 43 | "{} Compilation of {} failed! Compiler error message:\n", |
61 | 44 | Emoji("⚠️ ", "!"), |
62 | | - filename |
| 45 | + exercise |
63 | 46 | ); |
64 | 47 | println!("{}", style(formatstr).red()); |
65 | 48 | println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); |
66 | | - util::clean(); |
| 49 | + exercise.clean(); |
67 | 50 | Err(()) |
68 | 51 | } |
69 | 52 | } |
0 commit comments