rustlings/src/exercise.rs

157 lines
4.3 KiB
Rust
Raw Normal View History

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},
process::Command,
2024-04-07 18:36:26 -04:00
};
2024-04-24 19:56:01 -04:00
use crate::{in_official_repo, info_file::Mode, terminal_link::TerminalFileLink, DEBUG_PROFILE};
// TODO
pub const OUTPUT_CAPACITY: usize = 1 << 12;
fn run_command(mut cmd: Command, cmd_description: &str, output: &mut Vec<u8>) -> Result<bool> {
let (mut reader, writer) = os_pipe::pipe().with_context(|| {
format!("Failed to create a pipe to run the command `{cmd_description}``")
})?;
let mut handle = cmd
.stdout(writer.try_clone().with_context(|| {
format!("Failed to clone the pipe writer for the command `{cmd_description}`")
})?)
.stderr(writer)
.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
pub struct Exercise {
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-03-31 10:55:33 -04:00
// The mode of the exercise
pub mode: Mode,
// The hint text associated with the exercise
pub hint: String,
2024-04-13 19:15:43 -04:00
pub done: bool,
}
impl Exercise {
2024-04-24 19:56:01 -04:00
fn run_bin(&self, output: &mut Vec<u8>) -> Result<bool> {
writeln!(output, "{}", "Output".bold().magenta().underlined())?;
let bin_path = format!("target/debug/{}", self.name);
run_command(Command::new(&bin_path), &bin_path, output)
}
fn cargo_cmd(
&self,
command: &str,
args: &[&str],
cmd_description: &str,
output: &mut Vec<u8>,
dev: bool,
) -> 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);
run_command(cmd, cmd_description, output)
}
fn cargo_cmd_with_bin_output(
&self,
command: &str,
args: &[&str],
cmd_description: &str,
output: &mut Vec<u8>,
dev: bool,
) -> Result<bool> {
// Discard the output of `cargo build` because it will be shown again by the Cargo command.
output.clear();
let cargo_cmd_success = self.cargo_cmd(command, args, cmd_description, output, dev)?;
let run_success = self.run_bin(output)?;
Ok(cargo_cmd_success && run_success)
2024-03-31 10:55:33 -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();
let build_success = self.cargo_cmd("build", &[], "cargo build …", output, dev)?;
if !build_success {
return Ok(false);
}
2024-03-31 10:55:33 -04:00
match self.mode {
2024-04-24 19:56:01 -04:00
Mode::Run => self.run_bin(output),
Mode::Test => self.cargo_cmd_with_bin_output(
2024-04-16 15:46:07 -04:00
"test",
&[
"--",
"--color",
"always",
"--nocapture",
"--format",
"pretty",
],
2024-04-24 19:56:01 -04:00
"cargo test …",
output,
dev,
2024-04-16 15:46:07 -04:00
),
2024-04-24 19:56:01 -04:00
Mode::Clippy => self.cargo_cmd_with_bin_output(
2024-03-31 10:55:33 -04:00
"clippy",
2024-04-24 19:56:01 -04:00
&["--", "-D", "warnings"],
"cargo clippy …",
output,
dev,
2024-03-31 10:55:33 -04: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
}
}
impl Display for Exercise {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2024-04-13 20:41:19 -04:00
self.path.fmt(f)
}
}