oxedyne/fe2o3/fe2o3_text/tests/annealer_corpus/rustlings_exercise.rs
6.8 KiB, 1 run
created by r1870400018:11816, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use anyhow::Result; |
| 2 | use crossterm::{ |
| 3 | QueueableCommand, |
| 4 | style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, |
| 5 | }; |
| 6 | use std::io::{self, StdoutLock, Write}; |
| 7 | |
| 8 | use crate::{ |
| 9 | cmd::CmdRunner, |
| 10 | term::{self, CountedWrite, file_path, terminal_file_link, write_ansi}, |
| 11 | }; |
| 12 | |
| 13 | /// The initial capacity of the output buffer. |
| 14 | pub const OUTPUT_CAPACITY: usize = 1 << 14; |
| 15 | |
| 16 | pub fn solution_link_line( |
| 17 | stdout: &mut StdoutLock, |
| 18 | solution_path: &str, |
| 19 | emit_file_links: bool, |
| 20 | ) -> io::Result<()> { |
| 21 | stdout.queue(SetAttribute(Attribute::Bold))?; |
| 22 | stdout.write_all(b"Solution")?; |
| 23 | stdout.queue(ResetColor)?; |
| 24 | stdout.write_all(b" for comparison: ")?; |
| 25 | file_path(stdout, Color::Cyan, |writer| { |
| 26 | if emit_file_links && let Some(canonical_path) = term::canonicalize(solution_path) { |
| 27 | terminal_file_link(writer, solution_path, &canonical_path) |
| 28 | } else { |
| 29 | writer.stdout().write_all(solution_path.as_bytes()) |
| 30 | } |
| 31 | })?; |
| 32 | stdout.write_all(b"\n") |
| 33 | } |
| 34 | |
| 35 | // Run an exercise binary and append its output to the `output` buffer. |
| 36 | // Compilation must be done before calling this method. |
| 37 | fn run_bin( |
| 38 | bin_name: &str, |
| 39 | mut output: Option<&mut Vec<u8>>, |
| 40 | cmd_runner: &CmdRunner, |
| 41 | ) -> Result<bool> { |
| 42 | if let Some(output) = output.as_deref_mut() { |
| 43 | write_ansi(output, SetAttribute(Attribute::Underlined)); |
| 44 | output.extend_from_slice(b"Output"); |
| 45 | write_ansi(output, ResetColor); |
| 46 | output.push(b'\n'); |
| 47 | } |
| 48 | |
| 49 | let success = cmd_runner.run_debug_bin(bin_name, output.as_deref_mut())?; |
| 50 | |
| 51 | if let Some(output) = output |
| 52 | && !success |
| 53 | { |
| 54 | // This output is important to show the user that something went wrong. |
| 55 | // Otherwise, calling something like `exit(1)` in an exercise without further output |
| 56 | // leaves the user confused about why the exercise isn't done yet. |
| 57 | write_ansi(output, SetAttribute(Attribute::Bold)); |
| 58 | write_ansi(output, SetForegroundColor(Color::Red)); |
| 59 | output.extend_from_slice(b"The exercise didn't run successfully (nonzero exit code)"); |
| 60 | write_ansi(output, ResetColor); |
| 61 | output.push(b'\n'); |
| 62 | } |
| 63 | |
| 64 | Ok(success) |
| 65 | } |
| 66 | |
| 67 | /// See `info_file::ExerciseInfo` |
| 68 | pub struct Exercise { |
| 69 | pub name: &'static str, |
| 70 | pub dir: Option<&'static str>, |
| 71 | /// Path of the exercise file starting with the `exercises/` directory. |
| 72 | pub path: &'static str, |
| 73 | pub canonical_path: Option<String>, |
| 74 | pub test: bool, |
| 75 | pub strict_clippy: bool, |
| 76 | pub hint: &'static str, |
| 77 | pub done: bool, |
| 78 | } |
| 79 | |
| 80 | impl Exercise { |
| 81 | pub fn terminal_file_link<'a>( |
| 82 | &self, |
| 83 | writer: &mut impl CountedWrite<'a>, |
| 84 | emit_file_links: bool, |
| 85 | ) -> io::Result<()> { |
| 86 | file_path(writer, Color::Blue, |writer| { |
| 87 | if emit_file_links && let Some(canonical_path) = self.canonical_path.as_deref() { |
| 88 | terminal_file_link(writer, self.path, canonical_path) |
| 89 | } else { |
| 90 | writer.write_str(self.path) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub trait RunnableExercise { |
| 97 | fn name(&self) -> &str; |
| 98 | fn dir(&self) -> Option<&str>; |
| 99 | fn strict_clippy(&self) -> bool; |
| 100 | fn test(&self) -> bool; |
| 101 | |
| 102 | // Compile, check and run the exercise or its solution (depending on `bin_name´). |
| 103 | // The output is written to the `output` buffer after clearing it. |
| 104 | fn run<const FORCE_STRICT_CLIPPY: bool>( |
| 105 | &self, |
| 106 | bin_name: &str, |
| 107 | mut output: Option<&mut Vec<u8>>, |
| 108 | cmd_runner: &CmdRunner, |
| 109 | ) -> Result<bool> { |
| 110 | if let Some(output) = output.as_deref_mut() { |
| 111 | output.clear(); |
| 112 | } |
| 113 | |
| 114 | let build_success = cmd_runner |
| 115 | .cargo("build", bin_name, output.as_deref_mut()) |
| 116 | .run("cargo build …")?; |
| 117 | if !build_success { |
| 118 | return Ok(false); |
| 119 | } |
| 120 | |
| 121 | // Discard the compiler output because it will be shown again by `cargo test` or Clippy. |
| 122 | if let Some(output) = output.as_deref_mut() { |
| 123 | output.clear(); |
| 124 | } |
| 125 | |
| 126 | if self.test() { |
| 127 | let output_is_some = output.is_some(); |
| 128 | let mut test_cmd = cmd_runner.cargo("test", bin_name, output.as_deref_mut()); |
| 129 | if output_is_some { |
| 130 | test_cmd.args(["--", "--color", "always", "--format", "pretty"]); |
| 131 | } |
| 132 | let test_success = test_cmd.run("cargo test …")?; |
| 133 | if !test_success { |
| 134 | run_bin(bin_name, output, cmd_runner)?; |
| 135 | return Ok(false); |
| 136 | } |
| 137 | |
| 138 | // Discard the compiler output because it will be shown again by Clippy. |
| 139 | if let Some(output) = output.as_deref_mut() { |
| 140 | output.clear(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | let mut clippy_cmd = cmd_runner.cargo("clippy", bin_name, output.as_deref_mut()); |
| 145 | |
| 146 | // `--profile test` is required to also check code with `#[cfg(test)]`. |
| 147 | if FORCE_STRICT_CLIPPY || self.strict_clippy() { |
| 148 | clippy_cmd.args(["--profile", "test", "--", "-D", "warnings"]); |
| 149 | } else { |
| 150 | clippy_cmd.args(["--profile", "test"]); |
| 151 | } |
| 152 | |
| 153 | let clippy_success = clippy_cmd.run("cargo clippy …")?; |
| 154 | let run_success = run_bin(bin_name, output, cmd_runner)?; |
| 155 | |
| 156 | Ok(clippy_success && run_success) |
| 157 | } |
| 158 | |
| 159 | /// Compile, check and run the exercise. |
| 160 | /// The output is written to the `output` buffer after clearing it. |
| 161 | fn run_exercise(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> { |
| 162 | self.run::<false>(self.name(), output, cmd_runner) |
| 163 | } |
| 164 | |
| 165 | /// Compile, check and run the exercise's solution. |
| 166 | /// The output is written to the `output` buffer after clearing it. |
| 167 | fn run_solution(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> { |
| 168 | let name = self.name(); |
| 169 | let mut bin_name = String::with_capacity(name.len() + 4); |
| 170 | bin_name.push_str(name); |
| 171 | bin_name.push_str("_sol"); |
| 172 | |
| 173 | self.run::<true>(&bin_name, output, cmd_runner) |
| 174 | } |
| 175 | |
| 176 | fn sol_path(&self) -> String { |
| 177 | let name = self.name(); |
| 178 | |
| 179 | let mut path = if let Some(dir) = self.dir() { |
| 180 | // 14 = 10 + 1 + 3 |
| 181 | // solutions/ + / + .rs |
| 182 | let mut path = String::with_capacity(14 + dir.len() + name.len()); |
| 183 | path.push_str("solutions/"); |
| 184 | path.push_str(dir); |
| 185 | path.push('/'); |
| 186 | path |
| 187 | } else { |
| 188 | // 13 = 10 + 3 |
| 189 | // solutions/ + .rs |
| 190 | let mut path = String::with_capacity(13 + name.len()); |
| 191 | path.push_str("solutions/"); |
| 192 | path |
| 193 | }; |
| 194 | |
| 195 | path.push_str(name); |
| 196 | path.push_str(".rs"); |
| 197 | |
| 198 | path |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl RunnableExercise for Exercise { |
| 203 | fn name(&self) -> &str { |
| 204 | self.name |
| 205 | } |
| 206 | |
| 207 | fn dir(&self) -> Option<&str> { |
| 208 | self.dir |
| 209 | } |
| 210 | |
| 211 | fn strict_clippy(&self) -> bool { |
| 212 | self.strict_clippy |
| 213 | } |
| 214 | |
| 215 | fn test(&self) -> bool { |
| 216 | self.test |
| 217 | } |
| 218 | } |