table_line => table_divider

This commit is contained in:
David Peter 2022-05-15 21:28:35 +02:00
parent 79ca2297bb
commit 066cb732f9
2 changed files with 36 additions and 20 deletions

View File

@ -1,15 +1,26 @@
use crate::export::markup::MarkupExporter;
use super::markup::Alignment;
#[derive(Default)]
pub struct MarkdownExporter {}
impl MarkupExporter for MarkdownExporter {
fn table_row(&self, data: &[&str]) -> String {
format!("| {} |\n", data.join(" | "))
fn table_row(&self, cells: &[&str]) -> String {
format!("| {} |\n", cells.join(" | "))
}
fn table_line(&self, size: usize) -> String {
format!("|:---|{}\n", "---:|".repeat(size - 1))
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
format!(
"|{}\n",
cell_aligmnents
.iter()
.map(|a| match a {
Alignment::Left => ":---|",
Alignment::Right => "---:|",
})
.collect::<String>()
)
}
fn command(&self, cmd: &str) -> String {
@ -22,20 +33,16 @@ impl MarkupExporter for MarkdownExporter {
fn test_markdown_formatter_table_data() {
let formatter = MarkdownExporter::default();
let actual = formatter.table_row(&["a", "b", "c"]);
let expect = "| a | b | c |\n";
assert_eq!(expect, actual);
assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
}
/// Check Markdown-based horizontal line formatting
#[test]
fn test_markdown_formatter_table_line() {
fn test_markdown_formatter_table_divider() {
let formatter = MarkdownExporter::default();
let size = 5;
let actual = formatter.table_line(size);
let expect = "|:---|---:|---:|---:|---:|\n";
let actual = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
let expect = "|:---|---:|:---|\n";
assert_eq!(expect, actual);
}

View File

@ -6,23 +6,32 @@ use crate::util::units::Unit;
use super::Exporter;
use anyhow::{anyhow, Result};
pub enum Alignment {
Left,
Right,
}
pub trait MarkupExporter {
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
let head: [&str; 5] = [
// emit header
let mut table = self.table_row(&[
"Command",
&format!("Mean {}", notation),
&format!("Min {}", notation),
&format!("Max {}", notation),
"Relative",
];
// emit header
let mut table = self.table_row(&head);
]);
// emit horizontal line
table.push_str(&self.table_line(head.len()));
table.push_str(&self.table_divider(&[
Alignment::Left,
Alignment::Right,
Alignment::Right,
Alignment::Right,
Alignment::Right,
]));
for entry in entries {
let measurement = &entry.result;
@ -58,9 +67,9 @@ pub trait MarkupExporter {
table
}
fn table_row(&self, data: &[&str]) -> String;
fn table_row(&self, cells: &[&str]) -> String;
fn table_line(&self, size: usize) -> String;
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String;
fn command(&self, size: &str) -> String;
}