2019-04-12 17:48:57 -04:00
|
|
|
use serde::Deserialize;
|
2019-04-11 16:41:24 -04:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2019-05-22 07:50:23 -04:00
|
|
|
use std::fs::remove_file;
|
|
|
|
use std::path::PathBuf;
|
2019-04-11 16:41:24 -04:00
|
|
|
use std::process::{self, Command, Output};
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:48:57 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use std::fs::File;
|
2019-05-22 07:50:23 -04:00
|
|
|
use std::path::Path;
|
2019-04-12 17:48:57 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clean() {
|
|
|
|
File::create(&temp_file()).unwrap();
|
|
|
|
let exercise = Exercise {
|
|
|
|
path: PathBuf::from("example.rs"),
|
|
|
|
mode: Mode::Test,
|
|
|
|
};
|
|
|
|
exercise.clean();
|
|
|
|
assert!(!Path::new(&temp_file()).exists());
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|