2024-03-31 10:55:33 -04:00
|
|
|
use anyhow::{Context, Result};
|
2019-04-12 17:48:57 -04:00
|
|
|
use serde::Deserialize;
|
2024-04-07 18:36:26 -04:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Debug, Display, Formatter},
|
2024-04-10 20:51:02 -04:00
|
|
|
fs::{self},
|
2024-04-07 18:36:26 -04:00
|
|
|
path::PathBuf,
|
|
|
|
process::{Command, Output},
|
|
|
|
};
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-04-07 16:43:59 -04:00
|
|
|
use crate::embedded::{WriteStrategy, EMBEDDED_FILES};
|
2024-03-31 10:55:33 -04:00
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// The mode of the exercise.
|
2024-03-31 10:55:33 -04:00
|
|
|
#[derive(Deserialize, Copy, Clone)]
|
2019-04-11 16:41:24 -04:00
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum Mode {
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be compiled as a binary
|
2019-04-11 16:41:24 -04:00
|
|
|
Compile,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be compiled as a test harness
|
2019-04-11 16:41:24 -04:00
|
|
|
Test,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be linted with clippy
|
2020-02-14 09:25:03 -05:00
|
|
|
Clippy,
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-04-10 08:40:49 -04:00
|
|
|
#[serde(deny_unknown_fields)]
|
2024-04-07 17:37:40 -04:00
|
|
|
pub struct InfoFile {
|
2024-04-11 19:24:01 -04:00
|
|
|
// TODO
|
|
|
|
pub welcome_message: Option<String>,
|
|
|
|
pub final_message: Option<String>,
|
2019-04-11 16:41:24 -04:00
|
|
|
pub exercises: Vec<Exercise>,
|
|
|
|
}
|
|
|
|
|
2024-04-07 17:37:40 -04:00
|
|
|
impl InfoFile {
|
2024-03-31 10:55:33 -04:00
|
|
|
pub fn parse() -> Result<Self> {
|
|
|
|
// Read a local `info.toml` if it exists.
|
|
|
|
// Mainly to let the tests work for now.
|
2024-04-11 08:39:19 -04:00
|
|
|
let slf: Self = if let Ok(file_content) = fs::read_to_string("info.toml") {
|
2024-03-31 10:55:33 -04:00
|
|
|
toml_edit::de::from_str(&file_content)
|
|
|
|
} else {
|
2024-04-07 17:57:54 -04:00
|
|
|
toml_edit::de::from_str(include_str!("../info.toml"))
|
2024-03-31 10:55:33 -04:00
|
|
|
}
|
2024-04-11 08:39:19 -04:00
|
|
|
.context("Failed to parse `info.toml`")?;
|
|
|
|
|
|
|
|
if slf.exercises.is_empty() {
|
2024-04-11 19:24:01 -04:00
|
|
|
panic!("{NO_EXERCISES_ERR}");
|
2024-04-11 08:39:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(slf)
|
2024-03-31 10:55:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialized from the `info.toml` file.
|
|
|
|
#[derive(Deserialize)]
|
2024-04-10 08:40:49 -04:00
|
|
|
#[serde(deny_unknown_fields)]
|
2019-04-11 16:41:24 -04:00
|
|
|
pub struct Exercise {
|
2020-06-04 10:31:17 -04:00
|
|
|
// Name of the exercise
|
2019-11-11 09:46:32 -05:00
|
|
|
pub name: String,
|
2020-06-04 10:31:17 -04:00
|
|
|
// The path to the file containing the exercise's source code
|
2019-04-11 16:41:24 -04:00
|
|
|
pub path: PathBuf,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The mode of the exercise
|
2019-04-11 16:41:24 -04:00
|
|
|
pub mode: Mode,
|
2020-06-04 10:31:17 -04:00
|
|
|
// The hint text associated with the exercise
|
2019-11-11 10:51:38 -05:00
|
|
|
pub hint: String,
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Exercise {
|
2024-03-31 10:55:33 -04:00
|
|
|
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
|
2024-03-31 20:11:52 -04:00
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.arg(command);
|
|
|
|
|
|
|
|
// A hack to make `cargo run` work when developing Rustlings.
|
|
|
|
// Use `dev/Cargo.toml` when in the directory of the repository.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
if std::path::Path::new("tests").exists() {
|
|
|
|
cmd.arg("--manifest-path").arg("dev/Cargo.toml");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg("--color")
|
2024-03-31 10:55:33 -04:00
|
|
|
.arg("always")
|
|
|
|
.arg("-q")
|
|
|
|
.arg("--bin")
|
|
|
|
.arg(&self.name)
|
|
|
|
.args(args)
|
2019-04-11 16:41:24 -04:00
|
|
|
.output()
|
2024-03-31 10:55:33 -04:00
|
|
|
.context("Failed to run Cargo")
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
pub fn run(&self) -> Result<Output> {
|
|
|
|
match self.mode {
|
|
|
|
Mode::Compile => self.cargo_cmd("run", &[]),
|
2024-04-04 18:43:36 -04:00
|
|
|
Mode::Test => self.cargo_cmd("test", &["--", "--nocapture", "--format", "pretty"]),
|
2024-03-31 10:55:33 -04:00
|
|
|
Mode::Clippy => self.cargo_cmd(
|
|
|
|
"clippy",
|
|
|
|
&["--", "-D", "warnings", "-D", "clippy::float_cmp"],
|
|
|
|
),
|
2020-02-20 14:11:53 -05:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
|
2024-04-07 16:43:59 -04:00
|
|
|
pub fn reset(&self) -> Result<()> {
|
|
|
|
EMBEDDED_FILES
|
|
|
|
.write_exercise_to_disk(&self.path, WriteStrategy::Overwrite)
|
|
|
|
.with_context(|| format!("Failed to reset the exercise {self}"))
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Exercise {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-04-12 13:07:17 -04:00
|
|
|
Display::fmt(&self.path.display(), f)
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-11 19:24:01 -04:00
|
|
|
|
|
|
|
const NO_EXERCISES_ERR: &str = "There are no exercises yet!
|
|
|
|
If you are developing third-party exercises, add at least one exercise before testing.";
|