2024-04-18 06:41:17 -04:00
|
|
|
use anyhow::{Context, 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-24 19:56:01 -04:00
|
|
|
io::{Read, Write},
|
2024-04-24 21:25:45 -04:00
|
|
|
process::{Command, Stdio},
|
2024-04-07 18:36:26 -04:00
|
|
|
};
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
use crate::{in_official_repo, 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
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
fn run_command(
|
|
|
|
mut cmd: Command,
|
|
|
|
cmd_description: &str,
|
|
|
|
output: &mut Vec<u8>,
|
|
|
|
stderr: bool,
|
|
|
|
) -> Result<bool> {
|
2024-04-24 19:56:01 -04:00
|
|
|
let (mut reader, writer) = os_pipe::pipe().with_context(|| {
|
|
|
|
format!("Failed to create a pipe to run the command `{cmd_description}``")
|
|
|
|
})?;
|
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
let (stdout, stderr) = if stderr {
|
|
|
|
(
|
|
|
|
Stdio::from(writer.try_clone().with_context(|| {
|
|
|
|
format!("Failed to clone the pipe writer for the command `{cmd_description}`")
|
|
|
|
})?),
|
|
|
|
Stdio::from(writer),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(Stdio::from(writer), Stdio::null())
|
|
|
|
};
|
|
|
|
|
2024-04-24 19:56:01 -04:00
|
|
|
let mut handle = cmd
|
2024-04-24 21:25:45 -04:00
|
|
|
.stdout(stdout)
|
|
|
|
.stderr(stderr)
|
2024-04-24 19:56:01 -04:00
|
|
|
.spawn()
|
|
|
|
.with_context(|| format!("Failed to run the command `{cmd_description}`"))?;
|
|
|
|
|
|
|
|
// Prevent pipe deadlock.
|
|
|
|
drop(cmd);
|
|
|
|
|
|
|
|
reader
|
|
|
|
.read_to_end(output)
|
|
|
|
.with_context(|| format!("Failed to read the output of the command `{cmd_description}`"))?;
|
|
|
|
|
|
|
|
output.push(b'\n');
|
|
|
|
|
|
|
|
handle
|
|
|
|
.wait()
|
|
|
|
.with_context(|| format!("Failed to wait on the command `{cmd_description}` to exit"))
|
|
|
|
.map(|status| status.success())
|
|
|
|
}
|
2024-04-13 20:41:19 -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-24 19:56:01 -04:00
|
|
|
fn run_bin(&self, output: &mut Vec<u8>) -> Result<bool> {
|
2024-04-24 21:25:45 -04:00
|
|
|
writeln!(output, "{}", "Output".underlined())?;
|
2024-04-24 19:56:01 -04:00
|
|
|
|
|
|
|
let bin_path = format!("target/debug/{}", self.name);
|
2024-04-24 21:25:45 -04:00
|
|
|
let success = run_command(Command::new(&bin_path), &bin_path, output, true)?;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn cargo_cmd(
|
|
|
|
&self,
|
|
|
|
command: &str,
|
|
|
|
args: &[&str],
|
|
|
|
cmd_description: &str,
|
|
|
|
output: &mut Vec<u8>,
|
|
|
|
dev: bool,
|
2024-04-24 21:25:45 -04:00
|
|
|
stderr: bool,
|
2024-04-24 19:56:01 -04:00
|
|
|
) -> Result<bool> {
|
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.
|
2024-04-24 19:56:01 -04:00
|
|
|
if dev {
|
|
|
|
cmd.arg("--manifest-path")
|
|
|
|
.arg("dev/Cargo.toml")
|
|
|
|
.arg("--target-dir")
|
|
|
|
.arg("target");
|
2024-03-31 20:11:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg("--color")
|
2024-03-31 10:55:33 -04:00
|
|
|
.arg("always")
|
|
|
|
.arg("-q")
|
|
|
|
.arg("--bin")
|
2024-04-13 19:15:43 -04:00
|
|
|
.arg(self.name)
|
2024-04-24 19:56:01 -04:00
|
|
|
.args(args);
|
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
run_command(cmd, cmd_description, output, stderr)
|
2024-03-31 10:55:33 -04:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-04-24 19:56:01 -04:00
|
|
|
pub fn run(&self, output: &mut Vec<u8>) -> Result<bool> {
|
|
|
|
output.clear();
|
|
|
|
|
|
|
|
// Developing the official Rustlings.
|
|
|
|
let dev = DEBUG_PROFILE && in_official_repo();
|
|
|
|
|
2024-04-24 21:25:45 -04:00
|
|
|
let build_success = self.cargo_cmd("build", &[], "cargo build …", output, dev, true)?;
|
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
|
|
|
};
|
|
|
|
let clippy_success =
|
|
|
|
self.cargo_cmd("clippy", clippy_args, "cargo clippy …", output, dev, true)?;
|
|
|
|
if !clippy_success {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.test {
|
|
|
|
return self.run_bin(output);
|
2020-02-20 14:11:53 -05:00
|
|
|
}
|
2024-04-24 21:25:45 -04:00
|
|
|
|
|
|
|
let test_success = self.cargo_cmd(
|
|
|
|
"test",
|
|
|
|
&[
|
|
|
|
"--",
|
|
|
|
"--color",
|
|
|
|
"always",
|
|
|
|
"--nocapture",
|
|
|
|
"--format",
|
|
|
|
"pretty",
|
|
|
|
],
|
|
|
|
"cargo test …",
|
|
|
|
output,
|
|
|
|
dev,
|
|
|
|
// Hide warnings because they are shown by Clippy.
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let run_success = self.run_bin(output)?;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|