Skip to content

Commit 9eab7d7

Browse files
committed
Tidy up results handling
1 parent b4bae8e commit 9eab7d7

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

fontspector-checkapi/src/status.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clap::ArgEnum;
22
use serde::{Deserialize, Serialize};
33

4-
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Copy, Clone, ArgEnum, Serialize, Deserialize)]
4+
#[derive(
5+
Debug, PartialEq, PartialOrd, Ord, Eq, Copy, Clone, ArgEnum, Serialize, Deserialize, Hash,
6+
)]
57
pub enum StatusCode {
68
Skip,
79
Info,

fontspector-cli/src/main.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
mod args;
55
mod reporters;
66

7-
use crate::reporters::terminal::TerminalReporter;
8-
pub use args::Args;
7+
use args::Args;
98
use clap::Parser;
10-
use fontspector_checkapi::{Check, CheckResult, Context, Plugin, Registry, StatusCode, Testable};
9+
use fontspector_checkapi::{Check, Context, Plugin, Registry, Testable};
1110
use indicatif::ParallelProgressIterator;
1211
use profile_googlefonts::GoogleFonts;
1312
use profile_universal::Universal;
1413
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
15-
use reporters::Reporter;
14+
use reporters::{
15+
organize, summary_results, terminal::TerminalReporter, worst_status, Reporter, RunResults,
16+
};
1617
use serde_json::Map;
17-
use std::collections::HashMap;
1818

1919
/// Filter out checks that don't apply
2020
fn included_excluded(checkname: &str, args: &Args) -> bool {
@@ -124,39 +124,38 @@ fn main() {
124124
})
125125
.collect();
126126

127-
println!("Testing...");
128-
let results: Vec<_> = checkorder
127+
if !args.quiet {
128+
println!(
129+
"Running {:} check{} on {:} file{}",
130+
checkorder.len(),
131+
if checkorder.len() == 1 { "" } else { "s" },
132+
testables.len(),
133+
if testables.len() == 1 { "" } else { "s" }
134+
);
135+
}
136+
137+
let results: RunResults = checkorder
129138
.par_iter()
130139
.progress()
131140
.map(|(sectionname, testable, check, context)| {
132141
(sectionname, testable, check.run_one(testable, context))
133142
})
134143
.collect();
135144

136-
let worst_status = results
137-
.iter()
138-
.map(|(_sectionname, _testable, checkresults)| {
139-
checkresults
140-
.iter()
141-
.map(|r| r.status.code)
142-
.max()
143-
.unwrap_or(StatusCode::Pass)
144-
})
145-
.max()
146-
.unwrap_or(StatusCode::Pass);
145+
let worst_status = worst_status(&results);
147146

148147
// Organise results by testable and sectionname
149-
let mut organised_results: HashMap<&Testable, HashMap<String, Vec<CheckResult>>> =
150-
HashMap::new();
151-
for (sectionname, testable, checkresults) in results {
152-
// let filename = testable.filename.clone();
153-
let section = organised_results.entry(testable).or_default();
154-
let results = section.entry(sectionname.clone()).or_default();
155-
results.extend(checkresults);
156-
}
148+
let organised_results = organize(results);
157149

158150
if !args.quiet {
159151
TerminalReporter {}.report(&organised_results, &args, &registry);
152+
// Summary report
153+
let summary = summary_results(organised_results);
154+
print!("\nSummary:\n ");
155+
for (status, count) in summary.iter() {
156+
print!("{:}: {:} ", status, count);
157+
}
158+
println!();
160159
}
161160
if worst_status >= args.error_code_on {
162161
std::process::exit(1);

fontspector-cli/src/reporters/mod.rs

+45-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
use crate::Args;
2-
use fontspector_checkapi::{CheckResult, Registry, Testable};
2+
use fontspector_checkapi::{CheckResult, Registry, StatusCode, Testable};
33
use std::collections::HashMap;
44

55
pub(crate) mod terminal;
66

7+
pub type RunResults<'a> = Vec<(&'a String, &'a &'a Testable, Vec<CheckResult>)>;
8+
9+
pub fn worst_status(results: &RunResults) -> StatusCode {
10+
results
11+
.iter()
12+
.map(|(_sectionname, _testable, checkresults)| {
13+
checkresults
14+
.iter()
15+
.map(|r| r.status.code)
16+
.max()
17+
.unwrap_or(StatusCode::Pass)
18+
})
19+
.max()
20+
.unwrap_or(StatusCode::Pass)
21+
}
22+
23+
pub type OrganisedResults<'a> = HashMap<&'a Testable, HashMap<String, Vec<CheckResult>>>;
24+
25+
/// Organize the results by testable and section
26+
pub fn organize(results: RunResults) -> OrganisedResults {
27+
let mut organised_results: OrganisedResults = HashMap::new();
28+
for (sectionname, testable, checkresults) in results {
29+
// let filename = testable.filename.clone();
30+
let section = organised_results.entry(testable).or_default();
31+
let results = section.entry(sectionname.clone()).or_default();
32+
results.extend(checkresults);
33+
}
34+
organised_results
35+
}
36+
37+
pub fn summary_results(organised_results: OrganisedResults) -> HashMap<StatusCode, i32> {
38+
let mut summary = HashMap::new();
39+
for (_testable, sectionresults) in organised_results.iter() {
40+
for (_sectionname, results) in sectionresults.iter() {
41+
for result in results.iter() {
42+
let entry = summary.entry(result.status.code).or_insert(0);
43+
*entry += 1;
44+
}
45+
}
46+
}
47+
summary
48+
}
49+
750
pub trait Reporter {
8-
fn report(
9-
&self,
10-
organised_results: &HashMap<&Testable, HashMap<String, Vec<CheckResult>>>,
11-
args: &Args,
12-
registry: &Registry,
13-
);
51+
fn report(&self, organised_results: &OrganisedResults, args: &Args, registry: &Registry);
1452
}

0 commit comments

Comments
 (0)