2024-04-26 22:14:59 -04:00
|
|
|
|
use anyhow::Result;
|
2024-07-25 10:26:48 -04:00
|
|
|
|
use ratatui::crossterm::style::{style, StyledContent, Stylize};
|
2024-04-07 18:36:26 -04:00
|
|
|
|
use std::{
|
2024-04-13 19:15:43 -04:00
|
|
|
|
fmt::{self, Display, Formatter},
|
2024-04-26 22:14:59 -04:00
|
|
|
|
io::Write,
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
process::Command,
|
2024-04-07 18:36:26 -04:00
|
|
|
|
};
|
2019-04-11 16:41:24 -04:00
|
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
|
use crate::{
|
|
|
|
|
cmd::{run_cmd, CargoCmd},
|
|
|
|
|
in_official_repo,
|
|
|
|
|
terminal_link::TerminalFileLink,
|
|
|
|
|
DEBUG_PROFILE,
|
|
|
|
|
};
|
2024-04-24 19:56:01 -04:00
|
|
|
|
|
2024-05-13 15:40:40 -04:00
|
|
|
|
/// The initial capacity of the output buffer.
|
2024-04-25 08:43:02 -04:00
|
|
|
|
pub const OUTPUT_CAPACITY: usize = 1 << 14;
|
2024-04-24 19:56:01 -04:00
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
// Run an exercise binary and append its output to the `output` buffer.
|
|
|
|
|
// Compilation must be done before calling this method.
|
2024-07-28 14:30:23 -04:00
|
|
|
|
fn run_bin(bin_name: &str, mut output: Option<&mut Vec<u8>>, target_dir: &Path) -> Result<bool> {
|
|
|
|
|
if let Some(output) = output.as_deref_mut() {
|
|
|
|
|
writeln!(output, "{}", "Output".underlined())?;
|
|
|
|
|
}
|
2024-06-01 15:48:15 -04:00
|
|
|
|
|
|
|
|
|
// 7 = "/debug/".len()
|
|
|
|
|
let mut bin_path = PathBuf::with_capacity(target_dir.as_os_str().len() + 7 + bin_name.len());
|
|
|
|
|
bin_path.push(target_dir);
|
|
|
|
|
bin_path.push("debug");
|
|
|
|
|
bin_path.push(bin_name);
|
|
|
|
|
|
2024-07-28 14:30:23 -04:00
|
|
|
|
let success = run_cmd(
|
|
|
|
|
Command::new(&bin_path),
|
|
|
|
|
&bin_path.to_string_lossy(),
|
|
|
|
|
output.as_deref_mut(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
if let Some(output) = output {
|
|
|
|
|
if !success {
|
|
|
|
|
// This output is important to show the user that something went wrong.
|
|
|
|
|
// Otherwise, calling something like `exit(1)` in an exercise without further output
|
|
|
|
|
// leaves the user confused about why the exercise isn't done yet.
|
|
|
|
|
writeln!(
|
|
|
|
|
output,
|
|
|
|
|
"{}",
|
|
|
|
|
"The exercise didn't run successfully (nonzero exit code)"
|
|
|
|
|
.bold()
|
|
|
|
|
.red(),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
2024-06-01 15:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 16:02:45 -04:00
|
|
|
|
/// See `info_file::ExerciseInfo`
|
2019-04-11 16:41:24 -04:00
|
|
|
|
pub struct Exercise {
|
2024-04-23 13:18:25 -04:00
|
|
|
|
pub dir: Option<&'static str>,
|
2024-04-13 19:15:43 -04:00
|
|
|
|
pub name: &'static str,
|
2024-05-13 15:36:20 -04:00
|
|
|
|
/// Path of the exercise file starting with the `exercises/` directory.
|
2024-04-13 20:41:19 -04:00
|
|
|
|
pub path: &'static str,
|
2024-04-24 21:25:45 -04:00
|
|
|
|
pub test: bool,
|
|
|
|
|
pub strict_clippy: bool,
|
2019-11-11 10:51:38 -05:00
|
|
|
|
pub hint: String,
|
2024-04-13 19:15:43 -04:00
|
|
|
|
pub done: bool,
|
2019-04-11 16:41:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Exercise {
|
2024-06-01 15:48:15 -04:00
|
|
|
|
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
|
|
|
|
|
style(TerminalFileLink(self.path)).underlined().blue()
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
impl Display for Exercise {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
|
self.path.fmt(f)
|
2024-04-24 19:56:01 -04:00
|
|
|
|
}
|
2024-06-01 15:48:15 -04:00
|
|
|
|
}
|
2024-04-24 19:56:01 -04:00
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
pub trait RunnableExercise {
|
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
fn strict_clippy(&self) -> bool;
|
|
|
|
|
fn test(&self) -> bool;
|
|
|
|
|
|
|
|
|
|
// Compile, check and run the exercise or its solution (depending on `bin_name´).
|
|
|
|
|
// The output is written to the `output` buffer after clearing it.
|
2024-07-28 14:30:23 -04:00
|
|
|
|
fn run(
|
|
|
|
|
&self,
|
|
|
|
|
bin_name: &str,
|
|
|
|
|
mut output: Option<&mut Vec<u8>>,
|
|
|
|
|
target_dir: &Path,
|
|
|
|
|
) -> Result<bool> {
|
|
|
|
|
if let Some(output) = output.as_deref_mut() {
|
|
|
|
|
output.clear();
|
|
|
|
|
}
|
2024-04-24 19:56:01 -04:00
|
|
|
|
|
|
|
|
|
// Developing the official Rustlings.
|
|
|
|
|
let dev = DEBUG_PROFILE && in_official_repo();
|
|
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
|
let build_success = CargoCmd {
|
|
|
|
|
subcommand: "build",
|
|
|
|
|
args: &[],
|
2024-06-01 15:48:15 -04:00
|
|
|
|
bin_name,
|
2024-04-26 22:14:59 -04:00
|
|
|
|
description: "cargo build …",
|
2024-08-01 05:28:26 -04:00
|
|
|
|
hide_warnings: output.is_none(),
|
2024-04-26 22:14:59 -04:00
|
|
|
|
target_dir,
|
2024-07-28 14:30:23 -04:00
|
|
|
|
output: output.as_deref_mut(),
|
2024-04-26 22:14:59 -04:00
|
|
|
|
dev,
|
|
|
|
|
}
|
|
|
|
|
.run()?;
|
2024-04-24 19:56:01 -04:00
|
|
|
|
if !build_success {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 15:36:20 -04:00
|
|
|
|
// Discard the output of `cargo build` because it will be shown again by Clippy.
|
2024-07-28 14:30:23 -04:00
|
|
|
|
if let Some(output) = output.as_deref_mut() {
|
|
|
|
|
output.clear();
|
|
|
|
|
}
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
2024-05-13 15:36:20 -04:00
|
|
|
|
// `--profile test` is required to also check code with `[cfg(test)]`.
|
2024-06-01 15:48:15 -04:00
|
|
|
|
let clippy_args: &[&str] = if self.strict_clippy() {
|
2024-04-25 10:08:07 -04:00
|
|
|
|
&["--profile", "test", "--", "-D", "warnings"]
|
2024-04-24 21:25:45 -04:00
|
|
|
|
} else {
|
2024-04-25 10:08:07 -04:00
|
|
|
|
&["--profile", "test"]
|
2024-04-24 21:25:45 -04:00
|
|
|
|
};
|
2024-04-26 22:14:59 -04:00
|
|
|
|
let clippy_success = CargoCmd {
|
|
|
|
|
subcommand: "clippy",
|
|
|
|
|
args: clippy_args,
|
2024-06-01 15:48:15 -04:00
|
|
|
|
bin_name,
|
2024-04-26 22:14:59 -04:00
|
|
|
|
description: "cargo clippy …",
|
|
|
|
|
hide_warnings: false,
|
|
|
|
|
target_dir,
|
2024-07-28 14:30:23 -04:00
|
|
|
|
output: output.as_deref_mut(),
|
2024-04-26 22:14:59 -04:00
|
|
|
|
dev,
|
|
|
|
|
}
|
|
|
|
|
.run()?;
|
2024-04-24 21:25:45 -04:00
|
|
|
|
if !clippy_success {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
if !self.test() {
|
2024-07-28 14:30:23 -04:00
|
|
|
|
return run_bin(bin_name, output.as_deref_mut(), target_dir);
|
2020-02-20 14:11:53 -05:00
|
|
|
|
}
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
|
let test_success = CargoCmd {
|
|
|
|
|
subcommand: "test",
|
2024-04-29 20:43:51 -04:00
|
|
|
|
args: &["--", "--color", "always", "--show-output"],
|
2024-06-01 15:48:15 -04:00
|
|
|
|
bin_name,
|
2024-04-26 22:14:59 -04:00
|
|
|
|
description: "cargo test …",
|
|
|
|
|
// Hide warnings because they are shown by Clippy.
|
|
|
|
|
hide_warnings: true,
|
|
|
|
|
target_dir,
|
2024-07-28 14:30:23 -04:00
|
|
|
|
output: output.as_deref_mut(),
|
2024-04-24 21:25:45 -04:00
|
|
|
|
dev,
|
2024-04-26 22:14:59 -04:00
|
|
|
|
}
|
|
|
|
|
.run()?;
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
2024-07-28 14:30:23 -04:00
|
|
|
|
let run_success = run_bin(bin_name, output.as_deref_mut(), target_dir)?;
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
|
|
|
|
Ok(test_success && run_success)
|
2019-04-11 16:41:24 -04:00
|
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
/// Compile, check and run the exercise.
|
|
|
|
|
/// The output is written to the `output` buffer after clearing it.
|
|
|
|
|
#[inline]
|
2024-07-28 14:30:23 -04:00
|
|
|
|
fn run_exercise(&self, output: Option<&mut Vec<u8>>, target_dir: &Path) -> Result<bool> {
|
2024-06-01 15:48:15 -04:00
|
|
|
|
self.run(self.name(), output, target_dir)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compile, check and run the exercise's solution.
|
|
|
|
|
/// The output is written to the `output` buffer after clearing it.
|
2024-07-28 14:30:23 -04:00
|
|
|
|
fn run_solution(&self, output: Option<&mut Vec<u8>>, target_dir: &Path) -> Result<bool> {
|
2024-06-01 15:48:15 -04:00
|
|
|
|
let name = self.name();
|
2024-08-01 05:26:30 -04:00
|
|
|
|
let mut bin_name = String::with_capacity(name.len() + 4);
|
2024-06-01 15:48:15 -04:00
|
|
|
|
bin_name.push_str(name);
|
|
|
|
|
bin_name.push_str("_sol");
|
|
|
|
|
|
|
|
|
|
self.run(&bin_name, output, target_dir)
|
2024-04-13 20:41:19 -04:00
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 15:48:15 -04:00
|
|
|
|
impl RunnableExercise for Exercise {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
self.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn strict_clippy(&self) -> bool {
|
|
|
|
|
self.strict_clippy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn test(&self) -> bool {
|
|
|
|
|
self.test
|
2019-04-11 16:41:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|