rustlings/src/exercise.rs

143 lines
4.1 KiB
Rust
Raw Normal View History

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},
io::Write,
path::{Path, PathBuf},
process::Command,
2024-04-07 18:36:26 -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
pub struct Exercise {
2024-05-13 15:36:20 -04:00
/// Directory name.
pub dir: Option<&'static str>,
2024-05-13 15:36:20 -04:00
/// Exercise's unique name.
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,
pub test: bool,
pub strict_clippy: bool,
pub hint: String,
2024-04-13 19:15:43 -04:00
pub done: bool,
}
impl Exercise {
2024-05-13 15:36:20 -04:00
// Run the exercise's binary and append its output to the `output` buffer.
// Compilation should be done before calling this method.
fn run_bin(&self, output: &mut Vec<u8>, target_dir: &Path) -> Result<bool> {
writeln!(output, "{}", "Output".underlined())?;
2024-04-24 19:56:01 -04:00
2024-05-13 15:36:20 -04:00
// 7 = "/debug/".len()
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)?;
if !success {
2024-05-13 15:36:20 -04:00
// 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()
2024-05-13 15:36:20 -04:00
.red(),
)?;
}
Ok(success)
2024-04-24 19:56:01 -04:00
}
2024-05-13 15:36:20 -04:00
/// Compile, check and run the exercise.
/// The output is written to the `output` buffer after clearing it.
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();
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-05-13 15:36:20 -04:00
// Discard the output of `cargo build` because it will be shown again by Clippy.
output.clear();
2024-05-13 15:36:20 -04:00
// `--profile test` is required to also check code with `[cfg(test)]`.
let clippy_args: &[&str] = if self.strict_clippy {
2024-04-25 10:08:07 -04:00
&["--profile", "test", "--", "-D", "warnings"]
} else {
2024-04-25 10:08:07 -04:00
&["--profile", "test"]
};
let clippy_success = CargoCmd {
subcommand: "clippy",
args: clippy_args,
exercise_name: self.name,
description: "cargo clippy …",
hide_warnings: false,
target_dir,
output,
dev,
}
.run()?;
if !clippy_success {
return Ok(false);
}
if !self.test {
return self.run_bin(output, target_dir);
}
let test_success = CargoCmd {
subcommand: "test",
args: &["--", "--color", "always", "--show-output"],
exercise_name: self.name,
description: "cargo test …",
// Hide warnings because they are shown by Clippy.
hide_warnings: true,
target_dir,
output,
dev,
}
.run()?;
let run_success = self.run_bin(output, target_dir)?;
Ok(test_success && run_success)
}
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)
}
}