evobench_tools/stats_tables/tables/table_view.rs
1//! Trait abstraction for tables with headers and individual rows
2//!
3//! Allowing for independent backends / serialization formats. Has
4//! title row and body rows of strings and formatting instructions.
5
6use std::borrow::Cow;
7
8#[derive(Debug, Clone, Copy)]
9pub enum Unit {
10 /// No unit, e.g. for spacer columns
11 None,
12 /// E.g. factors, could be floats
13 DimensionLess,
14 /// Integers
15 Count,
16 /// From the ViewType the container is parameterized with
17 ViewType(&'static str),
18}
19
20#[derive(Debug, Clone, Copy, Eq, PartialEq)]
21pub enum Highlight {
22 /// Used for spacer columns, i.e. no value is there.
23 Spacer,
24 /// No special formatting, normal number display
25 Neutral,
26 /// "Bad"
27 Red,
28 /// "Good"
29 Green,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum ColumnFormatting {
34 /// Spacercolumn, should have no values
35 Spacer,
36 /// Values are numbers: right-adjusted, and auto-width
37 Number,
38 /// Values are (potentially long) strings, left-adjusted, fixed
39 /// width
40 String {
41 /// In Excel widths. None == automatic.
42 width_chars: Option<f64>,
43 },
44}
45
46pub trait TableViewRow<Context> {
47 /// Column names and unit. Not dyn compatible, must be static
48 /// because it needs to be available for tables in the absense of
49 /// rows. But to accommodate for dynamically decided changes,
50 /// takes a context argument (which could be ()).
51 fn table_view_header(
52 ctx: Context,
53 ) -> Box<dyn AsRef<[(Cow<'static, str>, Unit, ColumnFormatting)]>>;
54
55 /// Write the given row to `out`, matching the columns in the
56 /// `TableViewHeader`. Do *not* clear out inside this method!
57 fn table_view_row(&self, out: &mut Vec<(Cow<str>, Highlight)>);
58}
59
60/// A full table. dyn compatible.
61pub trait TableView {
62 fn table_name(&self) -> Cow<'_, str>;
63
64 /// Column names and unit.
65 fn table_view_header(&self) -> Box<dyn AsRef<[(Cow<'static, str>, Unit, ColumnFormatting)]>>;
66
67 fn table_view_body<'s>(
68 &'s self,
69 ) -> Box<dyn Iterator<Item = Cow<'s, [(Cow<'s, str>, Highlight)]>> + 's>;
70}