summaryrefslogtreecommitdiffstats
path: root/mqtt-tester/src/report.rs
blob: 8ebc3c2f52dd2ffc5fec4bd593ab601b18451455 (plain)
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
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use std::io::Write;

#[derive(Debug, PartialEq, Eq)]
pub enum ReportResult {
    Success,
    Failure,
    #[allow(unused)]
    Inconclusive,
}

pub struct Report {
    pub name: String,
    pub description: String,
    pub normative_statement_number: String,
    pub result: ReportResult,
    pub output: Option<Vec<u8>>,
}

impl std::fmt::Debug for Report {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Report")
            .field("name", &self.name)
            .field("description", &self.description)
            .field(
                "normative_statement_number",
                &self.normative_statement_number,
            )
            .field("result", &self.result)
            .field(
                "output",
                &self.output.as_ref().map(|out| String::from_utf8_lossy(out)),
            )
            .finish()
    }
}

pub fn print_report(report: &Report, mut writer: impl Write) -> Result<(), std::io::Error> {
    use ansi_term::Colour::{Blue, Green, Red, Yellow};
    write!(writer, "{} ... ", Blue.paint(&report.name))?;

    match report.result {
        ReportResult::Success => write!(writer, "{}", Green.paint("ok"))?,
        ReportResult::Failure => {
            writeln!(writer, "{}", Red.paint("failed"))?;
            writeln!(writer, "  {}", report.description)?;
            if let Some(output) = report.output.as_ref() {
                writeln!(
                    writer,
                    "{}",
                    textwrap::indent(&String::from_utf8_lossy(output), "> ")
                )?;
            } else {
                write!(writer, "  {}", Red.paint("No extra output"))?;
            }
        }
        ReportResult::Inconclusive => write!(writer, "{}", Yellow.paint("inconclusive"))?,
    }

    writeln!(writer)?;
    Ok(())
}