2024-04-26 22:14:59 -04:00
|
|
|
use anyhow::Result;
|
2024-04-13 20:41:19 -04:00
|
|
|
use 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,
|
2024-04-29 19:39:31 -04:00
|
|
|
info_file::ExerciseInfo,
|
2024-04-26 22:14:59 -04:00
|
|
|
terminal_link::TerminalFileLink,
|
|
|
|
DEBUG_PROFILE,
|
|
|
|
};
|
2024-04-24 19:56:01 -04:00
|
|
|
|
2024-04-25 08:43:02 -04:00
|
|
|
pub const OUTPUT_CAPACITY: usize = 1 << 14;
|
2024-04-24 19:56:01 -04:00
|
|
|
|
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
|
|
|
// Exercise's unique name
|
|
|
|
pub name: &'static str,
|
|
|
|
// Exercise's path
|
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,
|
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,
|
2024-04-13 19:15:43 -04:00
|
|
|
pub done: bool,
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Exercise {
|
2024-04-26 22:14:59 -04:00
|
|
|
fn run_bin(&self, output: &mut Vec<u8>, target_dir: &Path) -> Result<bool> {
|
2024-04-24 21:25:45 -04:00
|
|
|
writeln!(output, "{}", "Output".underlined())?;
|
2024-04-24 19:56:01 -04:00
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
let mut bin_path =
|
|
|
|
PathBuf::with_capacity(target_dir.as_os_str().len() + 7 + self.name.len());
|
|
|
|
bin_path.push(target_dir);
|
|
|
|
bin_path.push("debug");
|
|
|
|
bin_path.push(self.name);
|
|
|
|
|
|
|
|
let success = run_cmd(Command::new(&bin_path), &bin_path.to_string_lossy(), output)?;
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
|
|
if !success {
|
|
|
|
writeln!(
|
|
|
|
output,
|
|
|
|
"{}",
|
|
|
|
"The exercise didn't run successfully (nonzero exit code)"
|
|
|
|
.bold()
|
|
|
|
.red()
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(success)
|
2024-04-24 19:56:01 -04:00
|
|
|
}
|
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
pub fn run(&self, output: &mut Vec<u8>, target_dir: &Path) -> Result<bool> {
|
2024-04-24 19:56:01 -04:00
|
|
|
output.clear();
|
|
|
|
|
|
|
|
// 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: &[],
|
|
|
|
exercise_name: self.name,
|
|
|
|
description: "cargo build …",
|
|
|
|
hide_warnings: false,
|
|
|
|
target_dir,
|
|
|
|
output,
|
|
|
|
dev,
|
|
|
|
}
|
|
|
|
.run()?;
|
2024-04-24 19:56:01 -04:00
|
|
|
if !build_success {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
// Discard the output of `cargo build` because it will be shown again by the Cargo command.
|
|
|
|
output.clear();
|
|
|
|
|
|
|
|
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,
|
|
|
|
exercise_name: self.name,
|
|
|
|
description: "cargo clippy …",
|
|
|
|
hide_warnings: false,
|
|
|
|
target_dir,
|
|
|
|
output,
|
|
|
|
dev,
|
|
|
|
}
|
|
|
|
.run()?;
|
2024-04-24 21:25:45 -04:00
|
|
|
if !clippy_success {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.test {
|
2024-04-26 22:14:59 -04:00
|
|
|
return self.run_bin(output, 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",
|
|
|
|
args: &[
|
2024-04-24 21:25:45 -04:00
|
|
|
"--",
|
|
|
|
"--color",
|
|
|
|
"always",
|
2024-04-26 22:15:16 -04:00
|
|
|
"--show-output",
|
2024-04-24 21:25:45 -04:00
|
|
|
"--format",
|
|
|
|
"pretty",
|
|
|
|
],
|
2024-04-26 22:14:59 -04:00
|
|
|
exercise_name: self.name,
|
|
|
|
description: "cargo test …",
|
|
|
|
// Hide warnings because they are shown by Clippy.
|
|
|
|
hide_warnings: true,
|
|
|
|
target_dir,
|
2024-04-24 21:25:45 -04:00
|
|
|
output,
|
|
|
|
dev,
|
2024-04-26 22:14:59 -04:00
|
|
|
}
|
|
|
|
.run()?;
|
2024-04-24 21:25:45 -04:00
|
|
|
|
2024-04-26 22:14:59 -04:00
|
|
|
let run_success = self.run_bin(output, 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-04-13 20:41:19 -04:00
|
|
|
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
|
2024-04-23 20:52:30 -04:00
|
|
|
style(TerminalFileLink(self.path)).underlined().blue()
|
2024-04-13 20:41:19 -04:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
2024-04-29 19:39:31 -04:00
|
|
|
impl From<ExerciseInfo> for Exercise {
|
|
|
|
fn from(mut exercise_info: ExerciseInfo) -> Self {
|
|
|
|
// Leaking to be able to borrow in the watch mode `Table`.
|
|
|
|
// Leaking is not a problem because the `AppState` instance lives until
|
|
|
|
// the end of the program.
|
|
|
|
let path = exercise_info.path().leak();
|
|
|
|
|
|
|
|
exercise_info.name.shrink_to_fit();
|
|
|
|
let name = exercise_info.name.leak();
|
|
|
|
let dir = exercise_info.dir.map(|mut dir| {
|
|
|
|
dir.shrink_to_fit();
|
|
|
|
&*dir.leak()
|
|
|
|
});
|
|
|
|
|
|
|
|
let hint = exercise_info.hint.trim().to_owned();
|
|
|
|
|
|
|
|
Exercise {
|
|
|
|
dir,
|
|
|
|
name,
|
|
|
|
path,
|
|
|
|
test: exercise_info.test,
|
|
|
|
strict_clippy: exercise_info.strict_clippy,
|
|
|
|
hint,
|
|
|
|
done: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:41:24 -04:00
|
|
|
impl Display for Exercise {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-04-13 20:41:19 -04:00
|
|
|
self.path.fmt(f)
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
}
|