qlty_coverage/
print.rs

1use crate::publish::Report;
2use anyhow::Result;
3use cli_table::{
4    format::{Border, HorizontalLine, Justify, Separator, VerticalLine},
5    print_stdout, Cell, Style, Table,
6};
7use qlty_types::tests::v1::{CoverageSummary, FileCoverage};
8use serde_json::{self};
9
10pub fn print_report_as_text(report: &Report) -> Result<()> {
11    let mut total = CoverageSummary::default();
12
13    let mut rows: Vec<_> = report
14        .file_coverages
15        .clone()
16        .into_iter()
17        .map(|file_coverage| {
18            total += *file_coverage.summary.as_ref().unwrap();
19
20            vec![
21                file_coverage.path.cell(),
22                file_coverage
23                    .summary
24                    .as_ref()
25                    .unwrap()
26                    .covered
27                    .to_string()
28                    .cell()
29                    .justify(Justify::Right),
30                file_coverage
31                    .summary
32                    .as_ref()
33                    .unwrap()
34                    .missed
35                    .to_string()
36                    .cell()
37                    .justify(Justify::Right),
38                (file_coverage.summary.as_ref().unwrap().percent() as u32)
39                    .cell()
40                    .justify(Justify::Right),
41            ]
42        })
43        .collect();
44
45    rows.push(vec![
46        "TOTAL".cell().bold(true),
47        total
48            .covered
49            .to_string()
50            .cell()
51            .bold(true)
52            .justify(Justify::Right),
53        total
54            .missed
55            .to_string()
56            .cell()
57            .bold(true)
58            .justify(Justify::Right),
59        (total.percent() as u32)
60            .cell()
61            .bold(true)
62            .justify(Justify::Right),
63    ]);
64
65    let table = rows
66        .table()
67        .title(vec![
68            "name".cell(),
69            "covered".cell().justify(Justify::Right),
70            "missed".cell().justify(Justify::Right),
71            "%".cell().justify(Justify::Right),
72        ])
73        .border(Border::builder().build())
74        .separator(
75            Separator::builder()
76                .title(Some(HorizontalLine::default()))
77                .column(Some(VerticalLine::default()))
78                .build(),
79        );
80
81    print_stdout(table)?;
82    Ok(())
83}
84
85pub fn print_file_coverages_as_text(file_coverages: &Vec<FileCoverage>) -> Result<()> {
86    let rows: Vec<_> = file_coverages
87        .clone()
88        .into_iter()
89        .map(|file_coverage| vec![file_coverage.path.cell()])
90        .collect();
91
92    let table = rows
93        .table()
94        .title(vec!["name".cell()])
95        .border(Border::builder().build())
96        .separator(
97            Separator::builder()
98                .title(Some(HorizontalLine::default()))
99                .column(Some(VerticalLine::default()))
100                .build(),
101        );
102
103    print_stdout(table)?;
104    Ok(())
105}
106
107pub fn print_report_as_json(report: &Report) -> Result<()> {
108    println!("{}", serde_json::to_string_pretty(report)?);
109    Ok(())
110}
111
112pub fn print_file_coverages_as_json(file_coverages: &Vec<FileCoverage>) -> Result<()> {
113    println!("{}", serde_json::to_string_pretty(file_coverages)?);
114    Ok(())
115}