-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract exercise struct to encapsulate path logic
- Loading branch information
Showing
6 changed files
with
151 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::fmt::{self, Display, Formatter}; | ||
use std::fs::{remove_file}; | ||
use std::path::{PathBuf}; | ||
use std::process::{self, Command, Output}; | ||
use serde::Deserialize; | ||
|
||
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"]; | ||
|
||
fn temp_file() -> String { | ||
format!("./temp_{}", process::id()) | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum Mode { | ||
Compile, | ||
Test, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct ExerciseList { | ||
pub exercises: Vec<Exercise>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Exercise { | ||
pub path: PathBuf, | ||
pub mode: Mode, | ||
} | ||
|
||
impl Exercise { | ||
pub fn compile(&self) -> Output { | ||
match self.mode { | ||
Mode::Compile => Command::new("rustc") | ||
.args(&[self.path.to_str().unwrap(), "-o", &temp_file()]) | ||
.args(RUSTC_COLOR_ARGS) | ||
.output(), | ||
Mode::Test => Command::new("rustc") | ||
.args(&["--test", self.path.to_str().unwrap(), "-o", &temp_file()]) | ||
.args(RUSTC_COLOR_ARGS) | ||
.output(), | ||
} | ||
.expect("Failed to run 'compile' command.") | ||
} | ||
|
||
pub fn run(&self) -> Output { | ||
Command::new(&temp_file()) | ||
.output() | ||
.expect("Failed to run 'run' command") | ||
} | ||
|
||
pub fn clean(&self) { | ||
let _ignored = remove_file(&temp_file()); | ||
} | ||
} | ||
|
||
impl Display for Exercise { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "{}", self.path.to_str().unwrap()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_clean() { | ||
std::fs::File::create(&temp_file()).unwrap(); | ||
let exercise = Exercise { | ||
path: PathBuf::from("example.rs"), | ||
mode: Mode::Test, | ||
}; | ||
exercise.clean(); | ||
assert!(!std::path::Path::new(&temp_file()).exists()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,52 @@ | ||
use crate::util; | ||
use crate::exercise::{Mode, Exercise}; | ||
use crate::verify::test; | ||
use console::{style, Emoji}; | ||
use indicatif::ProgressBar; | ||
use std::fs; | ||
use toml::Value; | ||
|
||
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> { | ||
if let Some(filename) = matches.value_of("file") { | ||
let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap(); | ||
let tomlvec: &Vec<Value> = 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 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 run(exercise: &Exercise) -> Result<(), ()> { | ||
match exercise.mode { | ||
Mode::Test => test(exercise)?, | ||
Mode::Compile => compile_and_run(exercise)?, | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn compile_and_run(filename: &str) -> Result<(), ()> { | ||
pub fn compile_and_run(exercise: &Exercise) -> Result<(), ()> { | ||
let progress_bar = ProgressBar::new_spinner(); | ||
progress_bar.set_message(format!("Compiling {}...", filename).as_str()); | ||
progress_bar.set_message(format!("Compiling {}...", exercise).as_str()); | ||
progress_bar.enable_steady_tick(100); | ||
|
||
let compilecmd = util::compile_cmd(filename); | ||
progress_bar.set_message(format!("Running {}...", filename).as_str()); | ||
let compilecmd = exercise.compile(); | ||
progress_bar.set_message(format!("Running {}...", exercise).as_str()); | ||
if compilecmd.status.success() { | ||
let runcmd = util::run_cmd(); | ||
let runcmd = exercise.run(); | ||
progress_bar.finish_and_clear(); | ||
|
||
if runcmd.status.success() { | ||
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); | ||
let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename); | ||
let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), exercise); | ||
println!("{}", style(formatstr).green()); | ||
util::clean(); | ||
exercise.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); | ||
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), exercise); | ||
println!("{}", style(formatstr).red()); | ||
util::clean(); | ||
exercise.clean(); | ||
Err(()) | ||
} | ||
} else { | ||
progress_bar.finish_and_clear(); | ||
let formatstr = format!( | ||
"{} Compilation of {} failed! Compiler error message:\n", | ||
Emoji("⚠️ ", "!"), | ||
filename | ||
exercise | ||
); | ||
println!("{}", style(formatstr).red()); | ||
println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); | ||
util::clean(); | ||
exercise.clean(); | ||
Err(()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.