2024-04-27 05:14:59 +03:00
|
|
|
use anyhow::Result;
|
2024-08-26 00:53:50 +03:00
|
|
|
use crossterm::{
|
|
|
|
style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
|
|
|
|
QueueableCommand,
|
2024-04-08 01:36:26 +03:00
|
|
|
};
|
2024-08-26 00:53:50 +03:00
|
|
|
use std::io::{self, StdoutLock, Write};
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
use crate::{
|
|
|
|
cmd::CmdRunner,
|
2024-09-04 03:19:45 +03:00
|
|
|
term::{self, terminal_file_link, write_ansi, CountedWrite},
|
2024-08-26 00:53:50 +03:00
|
|
|
};
|
2024-04-25 02:56:01 +03:00
|
|
|
|
2024-05-13 22:40:40 +03:00
|
|
|
/// The initial capacity of the output buffer.
|
2024-04-25 15:43:02 +03:00
|
|
|
pub const OUTPUT_CAPACITY: usize = 1 << 14;
|
2024-04-25 02:56:01 +03:00
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
pub fn solution_link_line(stdout: &mut StdoutLock, solution_path: &str) -> io::Result<()> {
|
|
|
|
stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
stdout.write_all(b"Solution")?;
|
|
|
|
stdout.queue(ResetColor)?;
|
|
|
|
stdout.write_all(b" for comparison: ")?;
|
2024-09-04 03:19:45 +03:00
|
|
|
if let Some(canonical_path) = term::canonicalize(solution_path) {
|
|
|
|
terminal_file_link(stdout, solution_path, &canonical_path, Color::Cyan)?;
|
|
|
|
} else {
|
|
|
|
stdout.write_all(solution_path.as_bytes())?;
|
|
|
|
}
|
2024-08-26 00:53:50 +03:00
|
|
|
stdout.write_all(b"\n")
|
|
|
|
}
|
|
|
|
|
2024-06-01 22:48:15 +03:00
|
|
|
// Run an exercise binary and append its output to the `output` buffer.
|
|
|
|
// Compilation must be done before calling this method.
|
2024-08-01 16:23:54 +03:00
|
|
|
fn run_bin(
|
|
|
|
bin_name: &str,
|
|
|
|
mut output: Option<&mut Vec<u8>>,
|
|
|
|
cmd_runner: &CmdRunner,
|
|
|
|
) -> Result<bool> {
|
2024-07-28 21:30:23 +03:00
|
|
|
if let Some(output) = output.as_deref_mut() {
|
2024-08-26 00:53:50 +03:00
|
|
|
write_ansi(output, SetAttribute(Attribute::Underlined));
|
2024-08-26 01:24:39 +03:00
|
|
|
output.extend_from_slice(b"Output");
|
2024-08-26 00:53:50 +03:00
|
|
|
write_ansi(output, ResetColor);
|
2024-08-26 01:24:39 +03:00
|
|
|
output.push(b'\n');
|
2024-07-28 21:30:23 +03:00
|
|
|
}
|
2024-06-01 22:48:15 +03:00
|
|
|
|
2024-08-01 16:23:54 +03:00
|
|
|
let success = cmd_runner.run_debug_bin(bin_name, output.as_deref_mut())?;
|
2024-07-28 21:30:23 +03:00
|
|
|
|
|
|
|
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.
|
2024-08-26 00:53:50 +03:00
|
|
|
write_ansi(output, SetAttribute(Attribute::Bold));
|
|
|
|
write_ansi(output, SetForegroundColor(Color::Red));
|
2024-08-26 01:24:39 +03:00
|
|
|
output.extend_from_slice(b"The exercise didn't run successfully (nonzero exit code)");
|
2024-08-26 00:53:50 +03:00
|
|
|
write_ansi(output, ResetColor);
|
2024-08-26 01:24:39 +03:00
|
|
|
output.push(b'\n');
|
2024-07-28 21:30:23 +03:00
|
|
|
}
|
2024-06-01 22:48:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
|
2024-05-13 23:02:45 +03:00
|
|
|
/// See `info_file::ExerciseInfo`
|
2019-04-11 23:41:24 +03:00
|
|
|
pub struct Exercise {
|
2024-04-23 20:18:25 +03:00
|
|
|
pub dir: Option<&'static str>,
|
2024-04-14 02:15:43 +03:00
|
|
|
pub name: &'static str,
|
2024-05-13 22:36:20 +03:00
|
|
|
/// Path of the exercise file starting with the `exercises/` directory.
|
2024-04-14 03:41:19 +03:00
|
|
|
pub path: &'static str,
|
2024-09-04 03:19:45 +03:00
|
|
|
pub canonical_path: Option<String>,
|
2024-04-25 04:25:45 +03:00
|
|
|
pub test: bool,
|
|
|
|
pub strict_clippy: bool,
|
2024-08-02 17:28:05 +03:00
|
|
|
pub hint: &'static str,
|
2024-04-14 02:15:43 +03:00
|
|
|
pub done: bool,
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 03:19:45 +03:00
|
|
|
impl Exercise {
|
|
|
|
pub fn terminal_file_link<'a>(&self, writer: &mut impl CountedWrite<'a>) -> io::Result<()> {
|
|
|
|
if let Some(canonical_path) = self.canonical_path.as_deref() {
|
|
|
|
return terminal_file_link(writer, self.path, canonical_path, Color::Blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_str(self.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-01 22:48:15 +03:00
|
|
|
pub trait RunnableExercise {
|
|
|
|
fn name(&self) -> &str;
|
2024-08-28 02:10:19 +03:00
|
|
|
fn dir(&self) -> Option<&str>;
|
2024-06-01 22:48:15 +03:00
|
|
|
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-08-22 15:25:14 +03:00
|
|
|
fn run<const FORCE_STRICT_CLIPPY: bool>(
|
2024-07-28 21:30:23 +03:00
|
|
|
&self,
|
|
|
|
bin_name: &str,
|
|
|
|
mut output: Option<&mut Vec<u8>>,
|
2024-08-01 16:23:54 +03:00
|
|
|
cmd_runner: &CmdRunner,
|
2024-07-28 21:30:23 +03:00
|
|
|
) -> Result<bool> {
|
2024-08-08 00:35:50 +03:00
|
|
|
if let Some(output) = output.as_deref_mut() {
|
2024-07-28 21:30:23 +03:00
|
|
|
output.clear();
|
2024-04-27 05:14:59 +03:00
|
|
|
}
|
2024-08-08 00:35:50 +03:00
|
|
|
|
|
|
|
let build_success = cmd_runner
|
|
|
|
.cargo("build", bin_name, output.as_deref_mut())
|
|
|
|
.run("cargo build …")?;
|
2024-04-25 02:56:01 +03:00
|
|
|
if !build_success {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:35:50 +03:00
|
|
|
// Discard the compiler output because it will be shown again by `cargo test` or Clippy.
|
2024-07-28 21:30:23 +03:00
|
|
|
if let Some(output) = output.as_deref_mut() {
|
|
|
|
output.clear();
|
|
|
|
}
|
2024-04-25 04:25:45 +03:00
|
|
|
|
2024-08-08 00:35:50 +03:00
|
|
|
if self.test() {
|
|
|
|
let output_is_some = output.is_some();
|
|
|
|
let mut test_cmd = cmd_runner.cargo("test", bin_name, output.as_deref_mut());
|
|
|
|
if output_is_some {
|
2024-08-20 14:08:15 +03:00
|
|
|
test_cmd.args(["--", "--color", "always", "--format", "pretty"]);
|
2024-08-08 00:35:50 +03:00
|
|
|
}
|
|
|
|
let test_success = test_cmd.run("cargo test …")?;
|
|
|
|
if !test_success {
|
|
|
|
run_bin(bin_name, output, cmd_runner)?;
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard the compiler output because it will be shown again by Clippy.
|
|
|
|
if let Some(output) = output.as_deref_mut() {
|
|
|
|
output.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 16:23:54 +03:00
|
|
|
let mut clippy_cmd = cmd_runner.cargo("clippy", bin_name, output.as_deref_mut());
|
|
|
|
|
2024-09-12 15:10:50 +03:00
|
|
|
// `--profile test` is required to also check code with `#[cfg(test)]`.
|
2024-08-22 15:25:14 +03:00
|
|
|
if FORCE_STRICT_CLIPPY || self.strict_clippy() {
|
2024-08-01 16:23:54 +03:00
|
|
|
clippy_cmd.args(["--profile", "test", "--", "-D", "warnings"]);
|
2024-04-25 04:25:45 +03:00
|
|
|
} else {
|
2024-08-01 16:23:54 +03:00
|
|
|
clippy_cmd.args(["--profile", "test"]);
|
2024-04-27 05:14:59 +03:00
|
|
|
}
|
2024-08-01 16:23:54 +03:00
|
|
|
|
|
|
|
let clippy_success = clippy_cmd.run("cargo clippy …")?;
|
|
|
|
let run_success = run_bin(bin_name, output, cmd_runner)?;
|
2024-04-25 04:25:45 +03:00
|
|
|
|
2024-08-08 00:35:50 +03:00
|
|
|
Ok(clippy_success && run_success)
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
2019-11-11 15:38:24 +03:00
|
|
|
|
2024-06-01 22:48:15 +03:00
|
|
|
/// Compile, check and run the exercise.
|
|
|
|
/// The output is written to the `output` buffer after clearing it.
|
|
|
|
#[inline]
|
2024-08-01 16:23:54 +03:00
|
|
|
fn run_exercise(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> {
|
2024-08-22 15:25:14 +03:00
|
|
|
self.run::<false>(self.name(), output, cmd_runner)
|
2024-06-01 22:48:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile, check and run the exercise's solution.
|
|
|
|
/// The output is written to the `output` buffer after clearing it.
|
2024-08-01 16:23:54 +03:00
|
|
|
fn run_solution(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> {
|
2024-06-01 22:48:15 +03:00
|
|
|
let name = self.name();
|
2024-08-01 12:26:30 +03:00
|
|
|
let mut bin_name = String::with_capacity(name.len() + 4);
|
2024-06-01 22:48:15 +03:00
|
|
|
bin_name.push_str(name);
|
|
|
|
bin_name.push_str("_sol");
|
|
|
|
|
2024-08-22 15:25:14 +03:00
|
|
|
self.run::<true>(&bin_name, output, cmd_runner)
|
2024-04-14 03:41:19 +03:00
|
|
|
}
|
2024-08-28 02:10:19 +03:00
|
|
|
|
|
|
|
fn sol_path(&self) -> String {
|
|
|
|
let name = self.name();
|
|
|
|
|
|
|
|
let mut path = if let Some(dir) = self.dir() {
|
|
|
|
// 14 = 10 + 1 + 3
|
|
|
|
// solutions/ + / + .rs
|
|
|
|
let mut path = String::with_capacity(14 + dir.len() + name.len());
|
|
|
|
path.push_str("solutions/");
|
|
|
|
path.push_str(dir);
|
|
|
|
path.push('/');
|
|
|
|
path
|
|
|
|
} else {
|
|
|
|
// 13 = 10 + 3
|
|
|
|
// solutions/ + .rs
|
|
|
|
let mut path = String::with_capacity(13 + name.len());
|
|
|
|
path.push_str("solutions/");
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
path.push_str(name);
|
|
|
|
path.push_str(".rs");
|
|
|
|
|
|
|
|
path
|
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
|
2024-06-01 22:48:15 +03:00
|
|
|
impl RunnableExercise for Exercise {
|
|
|
|
#[inline]
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
|
2024-08-28 02:10:19 +03:00
|
|
|
#[inline]
|
|
|
|
fn dir(&self) -> Option<&str> {
|
|
|
|
self.dir
|
|
|
|
}
|
|
|
|
|
2024-06-01 22:48:15 +03:00
|
|
|
#[inline]
|
|
|
|
fn strict_clippy(&self) -> bool {
|
|
|
|
self.strict_clippy
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn test(&self) -> bool {
|
|
|
|
self.test
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
}
|