exam/exams/
rustfmt.rs

1use std::process::Command;
2
3use crate::Exam;
4
5use super::{from_io_err, from_output};
6
7pub struct RustfmtExam;
8
9impl Exam for RustfmtExam {
10    fn apply(&mut self) -> Result<(), crate::ExamFailure> {
11        let output = Command::new("cargo")
12            .arg("fmt")
13            .arg("--")
14            .arg("--check")
15            .output()
16            .map_err(from_io_err)?;
17
18        if output.status.success() {
19            Ok(())
20        } else {
21            // We don't really need to handle it, as it is a best-effort attempt.
22            let _ = Command::new("cargo").arg("fmt").spawn();
23            Err(from_output(RustFmtError::Unformatted, output))
24        }
25    }
26}
27
28#[derive(Debug, thiserror::Error)]
29pub enum RustFmtError {
30    #[error("rustfmt command wasn't found. Perhaps you could install it? {0}")]
31    NotInstalled(#[from] std::io::Error),
32    #[error("unformatted code was found")]
33    Unformatted,
34}