From 70e59cca3caf92a1daedac4fcf5d8940215e5529 Mon Sep 17 00:00:00 2001 From: lyn <819880950@qq.com> Date: Wed, 6 Mar 2019 21:47:00 +0100 Subject: [PATCH] standardize exercise running via an external toml file --- Cargo.toml | 1 + info.toml | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/run.rs | 99 ++++++++++++++----------- src/verify.rs | 53 +++----------- 5 files changed, 270 insertions(+), 84 deletions(-) create mode 100644 info.toml diff --git a/Cargo.toml b/Cargo.toml index 599e180b..b2208f42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ indicatif = "0.9.0" console = "0.6.2" syntect = "3.0.2" notify = "4.0.0" +toml = "0.4.10" diff --git a/info.toml b/info.toml new file mode 100644 index 00000000..e2eb7d7a --- /dev/null +++ b/info.toml @@ -0,0 +1,199 @@ +# VARIABLES + +[[exercises]] +path = "exercises/variables/variables1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/variables/variables2.rs" +mode = "compile" + +[[exercises]] +path = "exercises/variables/variables3.rs" +mode = "compile" + +[[exercises]] +path = "exercises/variables/variables4.rs" +mode = "compile" + +# IF + +[[exercises]] +path = "exercises/if/if1.rs" +mode = "test" + +# FUNCTIONS + +[[exercises]] +path = "exercises/functions/functions1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/functions/functions2.rs" +mode = "compile" + +[[exercises]] +path = "exercises/functions/functions3.rs" +mode = "compile" + +[[exercises]] +path = "exercises/functions/functions4.rs" +mode = "compile" + +[[exercises]] +path = "exercises/functions/functions5.rs" +mode = "compile" + +# TEST 1 + +[[exercises]] +path = "exercises/test1.rs" +mode = "test" + +# PRIMITIVE TYPES + +[[exercises]] +path = "exercises/primitive_types/primitive_types1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/primitive_types/primitive_types2.rs" +mode = "compile" + +[[exercises]] +path = "exercises/primitive_types/primitive_types3.rs" +mode = "compile" + +[[exercises]] +path = "exercises/primitive_types/primitive_types4.rs" +mode = "compile" + +[[exercises]] +path = "exercises/primitive_types/primitive_types5.rs" +mode = "compile" + +[[exercises]] +path = "exercises/primitive_types/primitive_types6.rs" +mode = "compile" + +# TESTS + +[[exercises]] +path = "exercises/tests/tests1.rs" +mode = "test" + +[[exercises]] +path = "exercises/tests/tests2.rs" +mode = "test" + +[[exercises]] +path = "exercises/tests/tests3.rs" +mode = "test" + +# TEST 2 + +[[exercises]] +path = "exercises/test2.rs" +mode = "test" + +# STRINGS + +[[exercises]] +path = "exercises/strings/strings1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/strings/strings2.rs" +mode = "compile" + +# TEST 3 + +[[exercises]] +path = "exercises/test3.rs" +mode = "compile" + +# MODULES + +[[exercises]] +path = "exercises/modules/modules1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/modules/modules2.rs" +mode = "compile" + +# MACROS + +[[exercises]] +path = "exercises/macros/macros1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/macros/macros2.rs" +mode = "compile" + +[[exercises]] +path = "exercises/macros/macros3.rs" +mode = "compile" + +[[exercises]] +path = "exercises/macros/macros4.rs" +mode = "compile" + +# TEST 4 + +[[exercises]] +path = "exercises/test4.rs" +mode = "compile" + +# MOVE SEMANTICS + +[[exercises]] +path = "exercises/move_semantics/move_semantics1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/move_semantics/move_semantics2.rs" +mode = "compile" + +[[exercises]] +path = "exercises/move_semantics/move_semantics3.rs" +mode = "compile" + +[[exercises]] +path = "exercises/move_semantics/move_semantics4.rs" +mode = "compile" + +# ERROR HANDLING + +[[exercises]] +path = "exercises/error_handling/errors1.rs" +mode = "test" + +[[exercises]] +path = "exercises/error_handling/errors2.rs" +mode = "test" + +[[exercises]] +path = "exercises/error_handling/errors3.rs" +mode = "test" + +[[exercises]] +path = "exercises/error_handling/errorsn.rs" +mode = "test" + +# OPTIONS / RESULTS + +[[exercises]] +path = "exercises/error_handling/option1.rs" +mode = "compile" + +[[exercises]] +path = "exercises/error_handling/result1.rs" +mode = "test" + +# THREADS + +[[exercises]] +path = "exercises/threads/threads1.rs" +mode = "compile" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index de13f1c3..39f11dc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ fn main() { } if let Some(matches) = matches.subcommand_matches("run") { - run(matches.clone()); + run(matches.clone()).unwrap(); } if let Some(_) = matches.subcommand_matches("verify") { diff --git a/src/run.rs b/src/run.rs index ea790699..b41ed33e 100644 --- a/src/run.rs +++ b/src/run.rs @@ -2,54 +2,71 @@ use crate::util::clean; use crate::verify::test; use console::{style, Emoji}; use indicatif::ProgressBar; +use std::fs; use std::process::Command; +use toml::Value; -pub fn run(matches: clap::ArgMatches) { +pub fn run(matches: clap::ArgMatches) -> Result<(), ()> { if let Some(filename) = matches.value_of("file") { - if matches.is_present("test") { - match test(filename) { - Ok(_) => (), - Err(_) => (), - } - std::process::exit(0); + let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap(); + let tomlvec: &Vec = toml.get("exercises").unwrap().as_array().unwrap(); + let mut exercises = tomlvec.clone(); + exercises.retain(|i| i.get("path").unwrap().as_str().unwrap() == filename); + if exercises.is_empty() { + println!("No exercise found for your filename!"); + std::process::exit(1); } - let bar = ProgressBar::new_spinner(); - bar.set_message(format!("Compiling {}...", filename).as_str()); - bar.enable_steady_tick(100); - let compilecmd = Command::new("rustc") - .args(&[filename, "-o", "temp", "--color", "always"]) - .output() - .expect("fail"); - bar.set_message(format!("Running {}...", filename).as_str()); - if compilecmd.status.success() { - let runcmd = Command::new("./temp").output().expect("fail"); - bar.finish_and_clear(); - if runcmd.status.success() { - println!("{}", String::from_utf8_lossy(&runcmd.stdout)); - let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename); - println!("{}", style(formatstr).green()); - clean(); - } else { - println!("{}", String::from_utf8_lossy(&runcmd.stdout)); - println!("{}", String::from_utf8_lossy(&runcmd.stderr)); - - let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename); - println!("{}", style(formatstr).red()); - clean(); - } - } else { - bar.finish_and_clear(); - let formatstr = format!( - "{} Compilation of {} failed! Compiler error message:\n", - Emoji("⚠️ ", "!"), - filename - ); - println!("{}", style(formatstr).red()); - println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); - clean(); + let exercise: &Value = &exercises[0]; + match exercise.get("mode").unwrap().as_str().unwrap() { + "test" => test(exercise.get("path").unwrap().as_str().unwrap())?, + "compile" => compile_and_run(exercise.get("path").unwrap().as_str().unwrap())?, + _ => (), } + Ok(()) } else { panic!("Please supply a filename!"); } } + +pub fn compile_and_run(filename: &str) -> Result<(), ()> { + let bar = ProgressBar::new_spinner(); + bar.set_message(format!("Compiling {}...", filename).as_str()); + bar.enable_steady_tick(100); + let compilecmd = Command::new("rustc") + .args(&[filename, "-o", "temp", "--color", "always"]) + .output() + .expect("fail"); + bar.set_message(format!("Running {}...", filename).as_str()); + if compilecmd.status.success() { + let runcmd = Command::new("./temp").output().expect("fail"); + bar.finish_and_clear(); + + if runcmd.status.success() { + println!("{}", String::from_utf8_lossy(&runcmd.stdout)); + let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename); + println!("{}", style(formatstr).green()); + clean(); + Ok(()) + } else { + println!("{}", String::from_utf8_lossy(&runcmd.stdout)); + println!("{}", String::from_utf8_lossy(&runcmd.stderr)); + + let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename); + println!("{}", style(formatstr).red()); + clean(); + Err(()) + } + } else { + bar.finish_and_clear(); + let formatstr = format!( + "{} Compilation of {} failed! Compiler error message:\n", + Emoji("⚠️ ", "!"), + filename + ); + println!("{}", style(formatstr).red()); + println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); + clean(); + Err(()) + } +} diff --git a/src/verify.rs b/src/verify.rs index b6eab366..bc44dfac 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -1,51 +1,20 @@ use crate::util::clean; use console::{style, Emoji}; use indicatif::ProgressBar; +use std::fs; use std::process::Command; +use toml::Value; pub fn verify() -> Result<(), ()> { - compile_only("exercises/variables/variables1.rs")?; - compile_only("exercises/variables/variables2.rs")?; - compile_only("exercises/variables/variables3.rs")?; - compile_only("exercises/variables/variables4.rs")?; - test("exercises/if/if1.rs")?; - compile_only("exercises/functions/functions1.rs")?; - compile_only("exercises/functions/functions2.rs")?; - compile_only("exercises/functions/functions3.rs")?; - compile_only("exercises/functions/functions4.rs")?; - compile_only("exercises/functions/functions5.rs")?; - test("exercises/test1.rs")?; - compile_only("exercises/primitive_types/primitive_types1.rs")?; - compile_only("exercises/primitive_types/primitive_types2.rs")?; - compile_only("exercises/primitive_types/primitive_types3.rs")?; - compile_only("exercises/primitive_types/primitive_types4.rs")?; - compile_only("exercises/primitive_types/primitive_types5.rs")?; - compile_only("exercises/primitive_types/primitive_types6.rs")?; - test("exercises/tests/tests1.rs")?; - test("exercises/tests/tests2.rs")?; - test("exercises/tests/tests3.rs")?; - test("exercises/test2.rs")?; - compile_only("exercises/strings/strings1.rs")?; - compile_only("exercises/strings/strings2.rs")?; - compile_only("exercises/test3.rs")?; - compile_only("exercises/modules/modules1.rs")?; - compile_only("exercises/modules/modules2.rs")?; - compile_only("exercises/macros/macros1.rs")?; - compile_only("exercises/macros/macros2.rs")?; - compile_only("exercises/macros/macros3.rs")?; - compile_only("exercises/macros/macros4.rs")?; - compile_only("exercises/test4.rs")?; - compile_only("exercises/move_semantics/move_semantics1.rs")?; - compile_only("exercises/move_semantics/move_semantics2.rs")?; - compile_only("exercises/move_semantics/move_semantics3.rs")?; - compile_only("exercises/move_semantics/move_semantics4.rs")?; - test("exercises/error_handling/errors1.rs")?; - test("exercises/error_handling/errors2.rs")?; - test("exercises/error_handling/errors3.rs")?; - test("exercises/error_handling/errorsn.rs")?; - compile_only("exercises/error_handling/option1.rs")?; - test("exercises/error_handling/result1.rs")?; - compile_only("exercises/threads/threads1.rs")?; + let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap(); + let tomlvec: &Vec = toml.get("exercises").unwrap().as_array().unwrap(); + for i in tomlvec { + match i.get("mode").unwrap().as_str().unwrap() { + "test" => test(i.get("path").unwrap().as_str().unwrap())?, + "compile" => compile_only(i.get("path").unwrap().as_str().unwrap())?, + _ => (), + } + } Ok(()) }