From 74fab994e2133cc40718abe923645922785c2a57 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 28 Jul 2024 20:30:23 +0200 Subject: Make the output optional --- src/exercise.rs | 67 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 25 deletions(-) (limited to 'src/exercise.rs') diff --git a/src/exercise.rs b/src/exercise.rs index 960eec0..5cb434b 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -19,8 +19,10 @@ pub const OUTPUT_CAPACITY: usize = 1 << 14; // Run an exercise binary and append its output to the `output` buffer. // Compilation must be done before calling this method. -fn run_bin(bin_name: &str, output: &mut Vec, target_dir: &Path) -> Result { - writeln!(output, "{}", "Output".underlined())?; +fn run_bin(bin_name: &str, mut output: Option<&mut Vec>, target_dir: &Path) -> Result { + if let Some(output) = output.as_deref_mut() { + writeln!(output, "{}", "Output".underlined())?; + } // 7 = "/debug/".len() let mut bin_path = PathBuf::with_capacity(target_dir.as_os_str().len() + 7 + bin_name.len()); @@ -28,19 +30,25 @@ fn run_bin(bin_name: &str, output: &mut Vec, target_dir: &Path) -> Result, target_dir: &Path) -> Result { - output.clear(); + fn run( + &self, + bin_name: &str, + mut output: Option<&mut Vec>, + target_dir: &Path, + ) -> Result { + if let Some(output) = output.as_deref_mut() { + output.clear(); + } // Developing the official Rustlings. let dev = DEBUG_PROFILE && in_official_repo(); @@ -90,7 +105,7 @@ pub trait RunnableExercise { description: "cargo build …", hide_warnings: false, target_dir, - output, + output: output.as_deref_mut(), dev, } .run()?; @@ -99,7 +114,9 @@ pub trait RunnableExercise { } // Discard the output of `cargo build` because it will be shown again by Clippy. - output.clear(); + if let Some(output) = output.as_deref_mut() { + output.clear(); + } // `--profile test` is required to also check code with `[cfg(test)]`. let clippy_args: &[&str] = if self.strict_clippy() { @@ -114,7 +131,7 @@ pub trait RunnableExercise { description: "cargo clippy …", hide_warnings: false, target_dir, - output, + output: output.as_deref_mut(), dev, } .run()?; @@ -123,7 +140,7 @@ pub trait RunnableExercise { } if !self.test() { - return run_bin(bin_name, output, target_dir); + return run_bin(bin_name, output.as_deref_mut(), target_dir); } let test_success = CargoCmd { @@ -134,12 +151,12 @@ pub trait RunnableExercise { // Hide warnings because they are shown by Clippy. hide_warnings: true, target_dir, - output, + output: output.as_deref_mut(), dev, } .run()?; - let run_success = run_bin(bin_name, output, target_dir)?; + let run_success = run_bin(bin_name, output.as_deref_mut(), target_dir)?; Ok(test_success && run_success) } @@ -147,13 +164,13 @@ pub trait RunnableExercise { /// Compile, check and run the exercise. /// The output is written to the `output` buffer after clearing it. #[inline] - fn run_exercise(&self, output: &mut Vec, target_dir: &Path) -> Result { + fn run_exercise(&self, output: Option<&mut Vec>, target_dir: &Path) -> Result { self.run(self.name(), output, target_dir) } /// Compile, check and run the exercise's solution. /// The output is written to the `output` buffer after clearing it. - fn run_solution(&self, output: &mut Vec, target_dir: &Path) -> Result { + fn run_solution(&self, output: Option<&mut Vec>, target_dir: &Path) -> Result { let name = self.name(); let mut bin_name = String::with_capacity(name.len()); bin_name.push_str(name); -- cgit v1.2.3