2024-04-18 13:41:17 +03:00
|
|
|
use anyhow::{Context, Result};
|
2024-04-14 03:41:19 +03:00
|
|
|
use crossterm::style::{style, StyledContent, Stylize};
|
2024-04-08 01:36:26 +03:00
|
|
|
use std::{
|
2024-04-14 02:15:43 +03:00
|
|
|
fmt::{self, Display, Formatter},
|
2024-04-14 03:41:19 +03:00
|
|
|
fs,
|
2024-04-18 00:37:31 +03:00
|
|
|
path::Path,
|
2024-04-18 13:41:17 +03:00
|
|
|
process::{Command, Output},
|
2024-04-08 01:36:26 +03:00
|
|
|
};
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-04-21 20:26:19 +03:00
|
|
|
use crate::{info_file::Mode, DEBUG_PROFILE};
|
2024-03-31 17:55:33 +03:00
|
|
|
|
2024-04-14 03:41:19 +03:00
|
|
|
pub struct TerminalFileLink<'a> {
|
|
|
|
path: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for TerminalFileLink<'a> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
if let Ok(Some(canonical_path)) = fs::canonicalize(self.path)
|
|
|
|
.as_deref()
|
|
|
|
.map(|path| path.to_str())
|
|
|
|
{
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"\x1b]8;;file://{}\x1b\\{}\x1b]8;;\x1b\\",
|
|
|
|
canonical_path, self.path,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", self.path,)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:41:24 +03:00
|
|
|
pub struct Exercise {
|
2024-04-14 02:15:43 +03:00
|
|
|
// Exercise's unique name
|
|
|
|
pub name: &'static str,
|
|
|
|
// Exercise's path
|
2024-04-14 03:41:19 +03:00
|
|
|
pub path: &'static str,
|
2024-03-31 17:55:33 +03:00
|
|
|
// The mode of the exercise
|
2019-04-11 23:41:24 +03:00
|
|
|
pub mode: Mode,
|
2020-06-04 17:31:17 +03:00
|
|
|
// The hint text associated with the exercise
|
2019-11-11 18:51:38 +03:00
|
|
|
pub hint: String,
|
2024-04-14 02:15:43 +03:00
|
|
|
pub done: bool,
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Exercise {
|
2024-03-31 17:55:33 +03:00
|
|
|
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
|
2024-04-01 03:11:52 +03:00
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.arg(command);
|
|
|
|
|
|
|
|
// A hack to make `cargo run` work when developing Rustlings.
|
2024-04-21 20:26:19 +03:00
|
|
|
if DEBUG_PROFILE && Path::new("tests").exists() {
|
2024-04-01 03:11:52 +03:00
|
|
|
cmd.arg("--manifest-path").arg("dev/Cargo.toml");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg("--color")
|
2024-03-31 17:55:33 +03:00
|
|
|
.arg("always")
|
|
|
|
.arg("-q")
|
|
|
|
.arg("--bin")
|
2024-04-14 02:15:43 +03:00
|
|
|
.arg(self.name)
|
2024-03-31 17:55:33 +03:00
|
|
|
.args(args)
|
2019-04-11 23:41:24 +03:00
|
|
|
.output()
|
2024-03-31 17:55:33 +03:00
|
|
|
.context("Failed to run Cargo")
|
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
pub fn run(&self) -> Result<Output> {
|
|
|
|
match self.mode {
|
2024-04-14 02:15:43 +03:00
|
|
|
Mode::Run => self.cargo_cmd("run", &[]),
|
2024-04-16 22:46:07 +03:00
|
|
|
Mode::Test => self.cargo_cmd(
|
|
|
|
"test",
|
|
|
|
&[
|
|
|
|
"--",
|
|
|
|
"--color",
|
|
|
|
"always",
|
|
|
|
"--nocapture",
|
|
|
|
"--format",
|
|
|
|
"pretty",
|
|
|
|
],
|
|
|
|
),
|
2024-03-31 17:55:33 +03:00
|
|
|
Mode::Clippy => self.cargo_cmd(
|
|
|
|
"clippy",
|
|
|
|
&["--", "-D", "warnings", "-D", "clippy::float_cmp"],
|
|
|
|
),
|
2020-02-20 22:11:53 +03:00
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
2019-11-11 15:38:24 +03:00
|
|
|
|
2024-04-14 03:41:19 +03:00
|
|
|
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
|
|
|
|
style(TerminalFileLink { path: self.path })
|
|
|
|
.underlined()
|
|
|
|
.blue()
|
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Exercise {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-04-14 03:41:19 +03:00
|
|
|
self.path.fmt(f)
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
}
|