1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
use anyhow::{Context, Result};
use crossterm::style::{style, StyledContent, Stylize};
use std::{
fmt::{self, Display, Formatter},
io::{Read, Write},
process::Command,
};
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())
}
pub struct Exercise {
pub dir: Option<&'static str>,
// Exercise's unique name
pub name: &'static str,
// Exercise's path
pub path: &'static str,
// The mode of the exercise
pub mode: Mode,
// The hint text associated with the exercise
pub hint: String,
pub done: bool,
}
impl Exercise {
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> {
let mut cmd = Command::new("cargo");
cmd.arg(command);
// A hack to make `cargo run` work when developing Rustlings.
if dev {
cmd.arg("--manifest-path")
.arg("dev/Cargo.toml")
.arg("--target-dir")
.arg("target");
}
cmd.arg("--color")
.arg("always")
.arg("-q")
.arg("--bin")
.arg(self.name)
.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)
}
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);
}
match self.mode {
Mode::Run => self.run_bin(output),
Mode::Test => self.cargo_cmd_with_bin_output(
"test",
&[
"--",
"--color",
"always",
"--nocapture",
"--format",
"pretty",
],
"cargo test …",
output,
dev,
),
Mode::Clippy => self.cargo_cmd_with_bin_output(
"clippy",
&["--", "-D", "warnings"],
"cargo clippy …",
output,
dev,
),
}
}
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
style(TerminalFileLink(self.path)).underlined().blue()
}
}
impl Display for Exercise {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.path.fmt(f)
}
}
|