use super::labels::LabelMap;
use super::metrics::Metrics;
use super::model::{OutputColumn, ReportResult, Trend, Verdict};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RowFacts {
pub differs: bool,
pub verdict: Option<Verdict>,
pub trend: Option<Trend>,
}
impl RowFacts {
pub fn of(result: &ReportResult, r: usize) -> RowFacts {
let Some(row) = result.rows.get(r) else {
return RowFacts::default();
};
let cell = |name: &str| row.cells.get(name).map(String::as_str);
RowFacts {
differs: cell(super::compare::RESULT_COLUMN).is_some_and(|v| {
!v.trim().is_empty()
&& v != super::compare::MATCH
&& v != super::compare::WAITING_BASELINE
}),
verdict: cell(super::compare::CORRECT_COLUMN).and_then(|v| match v {
v if v == Verdict::Correct.as_str() => Some(Verdict::Correct),
v if v == Verdict::Incorrect.as_str() => Some(Verdict::Incorrect),
v if v == Verdict::Untested.as_str() => Some(Verdict::Untested),
_ => None,
}),
trend: result.row_trend(r).or_else(|| {
cell(super::compare::TREND_COLUMN).and_then(|v| match v {
v if v == Trend::Fixed.as_str() => Some(Trend::Fixed),
v if v == Trend::Regressed.as_str() => Some(Trend::Regressed),
_ => None,
})
}),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RowFilter {
All,
Differ,
Correct,
Incorrect,
Regressed,
Fixed,
Waiting,
MatrixCell {
column: String,
truth: String,
answer: String,
},
}
impl RowFilter {
pub fn label(&self) -> String {
match self {
RowFilter::All => "All".to_string(),
RowFilter::Differ => "Differences".to_string(),
RowFilter::Correct => "Correct".to_string(),
RowFilter::Incorrect => "Incorrect".to_string(),
RowFilter::Regressed => "Regressions".to_string(),
RowFilter::Fixed => "Improvements".to_string(),
RowFilter::Waiting => "Waiting".to_string(),
RowFilter::MatrixCell { truth, answer, .. } => format!("{truth} → {answer}"),
}
}
pub fn available(result: &ReportResult) -> Vec<RowFilter> {
let has = |c: &str| result.column_order.iter().any(|k| k == c);
let mut out = vec![RowFilter::All];
let facts: Vec<RowFacts> = (0..result.rows.len())
.map(|r| RowFacts::of(result, r))
.collect();
if has(super::compare::RESULT_COLUMN) || facts.iter().any(|f| f.differs) {
out.push(RowFilter::Differ);
}
let scored = has(super::compare::CORRECT_COLUMN);
if scored || facts.iter().any(|f| f.verdict == Some(Verdict::Correct)) {
out.push(RowFilter::Correct);
}
if scored || facts.iter().any(|f| f.verdict == Some(Verdict::Incorrect)) {
out.push(RowFilter::Incorrect);
}
let trended = has(super::compare::TREND_COLUMN);
if trended || facts.iter().any(|f| f.trend == Some(Trend::Regressed)) {
out.push(RowFilter::Regressed);
}
if trended || facts.iter().any(|f| f.trend == Some(Trend::Fixed)) {
out.push(RowFilter::Fixed);
}
out
}
pub fn matches(
&self,
result: &ReportResult,
columns: &[OutputColumn],
labels: &LabelMap,
r: usize,
) -> bool {
match self {
RowFilter::All => true,
RowFilter::Differ => RowFacts::of(result, r).differs,
RowFilter::Correct => RowFacts::of(result, r).verdict == Some(Verdict::Correct),
RowFilter::Incorrect => RowFacts::of(result, r).verdict == Some(Verdict::Incorrect),
RowFilter::Regressed => RowFacts::of(result, r).trend == Some(Trend::Regressed),
RowFilter::Fixed => RowFacts::of(result, r).trend == Some(Trend::Fixed),
RowFilter::Waiting => false,
RowFilter::MatrixCell {
column,
truth,
answer,
} => {
let key = (r, column.clone());
let Some(expected) = result.truths.get(&key) else {
return false;
};
let Some(row) = result.rows.get(r) else {
return false;
};
let Some(col) = columns.iter().find(|c| &c.header == column) else {
return false;
};
labels.label_of(expected) == *truth
&& labels.label_of(&col.value(row, &result.no_match_marker)) == *answer
}
}
}
}
pub fn all_filters(result: &ReportResult, metrics: Option<&Metrics>) -> (Vec<RowFilter>, usize) {
let mut filters = RowFilter::available(result);
let buttons = filters.len();
if let Some(metrics) = metrics {
for m in &metrics.columns {
let Some(matrix) = &m.matrix else { continue };
for (t, truth) in matrix.axis.iter().enumerate() {
for (p, answer) in matrix.axis.iter().enumerate() {
if matrix.counts[t][p] > 0 {
filters.push(RowFilter::MatrixCell {
column: m.header.clone(),
truth: truth.clone(),
answer: answer.clone(),
});
}
}
}
}
}
(filters, buttons)
}
#[cfg_attr(not(feature = "gui"), allow(dead_code))]
pub fn visible_rows(
result: &ReportResult,
columns: &[OutputColumn],
labels: &LabelMap,
filter: &RowFilter,
text: &str,
) -> Vec<usize> {
let needle = text.trim().to_lowercase();
(0..result.rows.len())
.filter(|r| !result.pending.contains(r))
.filter(|&r| filter.matches(result, columns, labels, r))
.filter(|&r| {
needle.is_empty()
|| result.rows.get(r).is_some_and(|row| {
columns.iter().any(|c| {
c.value(row, &result.no_match_marker)
.to_lowercase()
.contains(&needle)
})
})
})
.collect()
}
pub fn waiting_rows(live: &ReportResult) -> Vec<usize> {
use crate::report::model::RowRole;
let pending: std::collections::HashSet<(&[String], Option<&str>)> = live
.pending
.iter()
.filter_map(|&r| live.rows.get(r))
.filter(|row| matches!(row.role, RowRole::Baseline | RowRole::Candidate))
.map(|row| (row.key.as_slice(), row.comparison.as_deref()))
.collect();
(0..live.rows.len())
.filter(|r| !live.pending.contains(r))
.filter(|&r| {
let row = &live.rows[r];
matches!(row.role, RowRole::Baseline | RowRole::Candidate)
&& pending.contains(&(row.key.as_slice(), row.comparison.as_deref()))
})
.collect()
}
#[cfg_attr(not(feature = "gui"), allow(dead_code))]
pub fn live_visible_rows(
live: &ReportResult,
live_columns: &[OutputColumn],
projected: &ReportResult,
projected_columns: &[OutputColumn],
labels: &LabelMap,
filter: &RowFilter,
text: &str,
) -> Vec<usize> {
if *filter == RowFilter::All && text.trim().is_empty() {
return (0..live.rows.len()).collect();
}
let by_text: std::collections::HashSet<usize> =
visible_rows(live, live_columns, labels, &RowFilter::All, text)
.into_iter()
.collect();
if *filter == RowFilter::All {
return (0..live.rows.len())
.filter(|r| by_text.contains(r))
.collect();
}
if *filter == RowFilter::Waiting {
return waiting_rows(live)
.into_iter()
.filter(|r| by_text.contains(r))
.collect();
}
let picked = visible_rows(projected, projected_columns, labels, filter, "");
let paths: std::collections::HashSet<&[(usize, usize)]> = picked
.iter()
.map(|&r| projected.rows[r].path.as_slice())
.collect();
let sides: std::collections::HashSet<(&[String], Option<&str>)> = picked
.iter()
.map(|&r| &projected.rows[r])
.filter(|row| row.role == crate::report::model::RowRole::Candidate)
.map(|row| (row.key.as_slice(), row.comparison.as_deref()))
.collect();
(0..live.rows.len())
.filter(|r| by_text.contains(r))
.filter(|&r| {
let row = &live.rows[r];
paths.contains(row.path.as_slice())
|| (row.role == crate::report::model::RowRole::Baseline
&& sides.contains(&(row.key.as_slice(), row.comparison.as_deref())))
})
.collect()
}
#[cfg_attr(not(feature = "gui"), allow(dead_code))]
pub fn sort_rows(
result: &ReportResult,
column: &OutputColumn,
descending: bool,
rows: &mut [usize],
) {
use std::cmp::Ordering;
enum Key {
Number(f64),
Text(String),
Blank,
}
let key = |r: usize| -> Key {
let Some(row) = result.rows.get(r) else {
return Key::Blank;
};
let v = if result.pending.contains(&r) {
column.pending_value(row, "", &result.planned_columns)
} else {
column.value(row, &result.no_match_marker)
};
if v.trim().is_empty() || v == result.no_match_marker {
Key::Blank
} else if let Some(n) = crate::report::writer::parse_report_number(&v) {
Key::Number(n)
} else {
Key::Text(v.to_lowercase())
}
};
let mut keyed: Vec<(Key, usize)> = rows.iter().map(|&r| (key(r), r)).collect();
keyed.sort_by(|(a, _), (b, _)| {
let ord = match (a, b) {
(Key::Blank, Key::Blank) => return Ordering::Equal,
(Key::Blank, _) => return Ordering::Greater,
(_, Key::Blank) => return Ordering::Less,
(Key::Number(x), Key::Number(y)) => x.total_cmp(y),
(Key::Number(_), Key::Text(_)) => Ordering::Less,
(Key::Text(_), Key::Number(_)) => Ordering::Greater,
(Key::Text(x), Key::Text(y)) => natural_cmp(x, y),
};
if descending { ord.reverse() } else { ord }
});
for (slot, (_, r)) in rows.iter_mut().zip(keyed) {
*slot = r;
}
}
fn natural_cmp(a: &str, b: &str) -> std::cmp::Ordering {
use std::cmp::Ordering;
let (mut a, mut b) = (a, b);
loop {
match (a.chars().next(), b.chars().next()) {
(None, None) => return Ordering::Equal,
(None, Some(_)) => return Ordering::Less,
(Some(_), None) => return Ordering::Greater,
(Some(x), Some(y)) if x.is_ascii_digit() && y.is_ascii_digit() => {
let da = a.len() - a.trim_start_matches(|c: char| c.is_ascii_digit()).len();
let db = b.len() - b.trim_start_matches(|c: char| c.is_ascii_digit()).len();
let (na, nb) = (&a[..da], &b[..db]);
let (ta, tb) = (na.trim_start_matches('0'), nb.trim_start_matches('0'));
let ord = ta.len().cmp(&tb.len()).then_with(|| ta.cmp(tb));
if ord != Ordering::Equal {
return ord;
}
a = &a[da..];
b = &b[db..];
}
(Some(x), Some(y)) => {
if x != y {
return x.cmp(&y);
}
a = &a[x.len_utf8()..];
b = &b[y.len_utf8()..];
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sort_rows_orders_numbers_numerically_and_sinks_blanks() {
let result = ReportResult {
rows: vec![
row(&[("Time", "100")]),
row(&[("Time", "")]),
row(&[("Time", "9")]),
row(&[("Time", "timeout")]),
row(&[("Time", "1000")]),
],
..Default::default()
};
let mut rows: Vec<usize> = (0..5).collect();
sort_rows(&result, &col("Time"), false, &mut rows);
assert_eq!(rows, vec![2, 0, 4, 3, 1]);
sort_rows(&result, &col("Time"), true, &mut rows);
assert_eq!(rows, vec![3, 4, 0, 2, 1], "blank still last");
}
#[test]
fn sort_rows_sinks_rows_still_on_their_way() {
let mut result = ReportResult {
rows: vec![
row(&[("Time", "0")]),
row(&[("Time", "250")]),
row(&[("Time", "120")]),
],
..Default::default()
};
result.pending.insert(0);
let mut rows: Vec<usize> = (0..3).collect();
sort_rows(&result, &col("Time"), false, &mut rows);
assert_eq!(rows, vec![2, 1, 0]);
}
#[test]
fn sort_rows_orders_paths_naturally_and_keeps_ties_stable() {
let result = ReportResult {
rows: vec![
row(&[("FILE", "img/img10.jpg")]),
row(&[("FILE", "IMG/img2.jpg")]),
row(&[("FILE", "img/a.jpg")]),
row(&[("FILE", "img/img2.jpg")]),
],
..Default::default()
};
let mut rows: Vec<usize> = (0..4).collect();
sort_rows(&result, &col("FILE"), false, &mut rows);
assert_eq!(rows, vec![2, 1, 3, 0]);
}
use crate::report::compare::{CORRECT_COLUMN, MATCH, RESULT_COLUMN, TREND_COLUMN};
use crate::report::model::ReportRow;
fn row(cells: &[(&str, &str)]) -> ReportRow {
ReportRow {
cells: cells
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
..Default::default()
}
}
fn col(header: &str) -> OutputColumn {
OutputColumn {
header: header.to_string(),
sources: vec![header.to_string()],
stats: Vec::new(),
image: None,
truth: None,
detail: false,
}
}
fn fixture() -> ReportResult {
ReportResult {
column_order: vec![
RESULT_COLUMN.into(),
CORRECT_COLUMN.into(),
TREND_COLUMN.into(),
"Name".into(),
"Verdict".into(),
],
rows: vec![
row(&[
(RESULT_COLUMN, MATCH),
(CORRECT_COLUMN, "correct"),
(TREND_COLUMN, "unchanged"),
("Name", "alpha"),
("Verdict", "Low Risk"),
]),
row(&[
(RESULT_COLUMN, "Verdict: a≠b"),
(CORRECT_COLUMN, "correct"),
(TREND_COLUMN, "fixed"),
("Name", "beta"),
("Verdict", "Low Risk"),
]),
row(&[
(RESULT_COLUMN, "Verdict: a≠b"),
(CORRECT_COLUMN, "incorrect"),
(TREND_COLUMN, "regressed"),
("Name", "gamma"),
("Verdict", "High Risk"),
]),
row(&[
(RESULT_COLUMN, "Verdict: a≠b"),
(CORRECT_COLUMN, "incorrect"),
(TREND_COLUMN, "unchanged"),
("Name", "delta"),
("Verdict", "High Risk"),
]),
],
trends: [
((0, "Verdict".to_string()), Trend::Unchanged),
((1, "Verdict".to_string()), Trend::Fixed),
((2, "Verdict".to_string()), Trend::Regressed),
((3, "Verdict".to_string()), Trend::StillWrong),
]
.into_iter()
.collect(),
..Default::default()
}
}
#[test]
fn a_still_wrong_row_is_told_apart_from_a_still_right_one() {
let res = fixture();
assert_eq!(RowFacts::of(&res, 0).trend, Some(Trend::Unchanged));
assert_eq!(RowFacts::of(&res, 3).trend, Some(Trend::StillWrong));
assert_eq!(
res.rows[0].cells.get(TREND_COLUMN),
res.rows[3].cells.get(TREND_COLUMN),
"even though the column says the same thing about both"
);
}
fn columns() -> Vec<OutputColumn> {
vec![col("Name"), col("Verdict")]
}
#[test]
fn each_filter_selects_exactly_its_class() {
let res = fixture();
let cols = columns();
let labels = LabelMap::parse(&[]);
let pick = |f: RowFilter| visible_rows(&res, &cols, &labels, &f, "");
assert_eq!(pick(RowFilter::All), vec![0, 1, 2, 3]);
assert_eq!(
pick(RowFilter::Differ),
vec![1, 2, 3],
"the matched row is not a difference"
);
assert_eq!(pick(RowFilter::Correct), vec![0, 1], "the two right rows");
assert_eq!(
pick(RowFilter::Incorrect),
vec![2, 3],
"both wrong rows, whether or not the wrongness is new"
);
assert_eq!(
pick(RowFilter::Regressed),
vec![2],
"`still wrong` is failing, but it is not a regression"
);
assert_eq!(
pick(RowFilter::Fixed),
vec![1],
"and the row that came right is the improvement, on its own"
);
}
#[test]
fn an_untested_row_is_not_incorrect() {
let mut res = fixture();
res.rows.push(row(&[
(RESULT_COLUMN, MATCH),
(CORRECT_COLUMN, "untested"),
("Name", "epsilon"),
]));
let cols = columns();
let labels = LabelMap::parse(&[]);
assert_eq!(
visible_rows(&res, &cols, &labels, &RowFilter::Incorrect, ""),
vec![2, 3]
);
}
#[test]
fn the_text_filter_searches_the_shown_columns_and_combines_with_the_class() {
let res = fixture();
let cols = columns();
let labels = LabelMap::parse(&[]);
assert_eq!(
visible_rows(&res, &cols, &labels, &RowFilter::All, "GAM"),
vec![2],
"case-insensitive substring of a shown value"
);
assert_eq!(
visible_rows(&res, &cols, &labels, &RowFilter::Incorrect, "delta"),
vec![3],
"the text and the class narrow together"
);
assert!(
visible_rows(&res, &cols, &labels, &RowFilter::All, "unchanged").is_empty(),
"a value only in a column the caller isn't showing is not searched"
);
}
#[test]
fn a_matrix_cell_selects_the_rows_it_counted_through_the_label_classes() {
let mut res = fixture();
let labels = LabelMap::parse(&[
"Pass = pass, real, low risk",
"Fail = fail, fake, high risk",
]);
for (r, truth) in [(0, "real"), (1, "real"), (2, "real"), (3, "fake")] {
res.truths.insert((r, "Verdict".to_string()), truth.into());
}
let cols = columns();
let cell = |truth: &str, answer: &str| {
visible_rows(
&res,
&cols,
&labels,
&RowFilter::MatrixCell {
column: "Verdict".into(),
truth: truth.into(),
answer: answer.into(),
},
"",
)
};
assert_eq!(
cell("Pass", "Pass"),
vec![0, 1],
"`real` and `Low Risk` are both the Pass class"
);
assert_eq!(cell("Pass", "Fail"), vec![2], "the off-diagonal cell");
assert_eq!(cell("Fail", "Fail"), vec![3]);
assert!(cell("Fail", "Pass").is_empty());
}
#[test]
fn only_the_filters_a_report_can_answer_are_offered() {
assert_eq!(
RowFilter::available(&fixture()),
vec![
RowFilter::All,
RowFilter::Differ,
RowFilter::Correct,
RowFilter::Incorrect,
RowFilter::Regressed,
RowFilter::Fixed
]
);
let plain = ReportResult {
rows: vec![row(&[("Name", "a")])],
..Default::default()
};
assert_eq!(RowFilter::available(&plain), vec![RowFilter::All]);
}
#[test]
fn a_run_that_only_fixed_things_is_offered_both_movement_filters() {
let mut res = fixture();
res.rows.remove(2);
res.trends = [
((0, "Verdict".to_string()), Trend::Unchanged),
((1, "Verdict".to_string()), Trend::Fixed),
((2, "Verdict".to_string()), Trend::StillWrong),
]
.into_iter()
.collect();
let offered = RowFilter::available(&res);
assert!(
offered.contains(&RowFilter::Fixed),
"something came right, so the filter that shows it is there: {offered:?}"
);
assert!(
offered.contains(&RowFilter::Regressed),
"nothing broke, and the button says so by showing nothing: {offered:?}"
);
}
#[test]
fn rows_that_have_not_run_yet_are_never_shown() {
let mut res = fixture();
res.pending.insert(2);
let cols = columns();
let labels = LabelMap::parse(&[]);
assert_eq!(
visible_rows(&res, &cols, &labels, &RowFilter::All, ""),
vec![0, 1, 3]
);
}
}