table_line => table_divider

This commit is contained in:
David Peter authored and David Peter committed 2022-05-15 21:37:40 +02:00
1 parent 5cd2821266
commit 8c05fd030d
2 files changed
+36 -20

No files matched your search

+19 -12
View File
@@ -1,15 +1,26 @@
use crate::export::markup::MarkupExporter; use crate::export::markup::MarkupExporter;
use super::markup::Alignment;
#[derive(Default)] #[derive(Default)]
pub struct MarkdownExporter {} pub struct MarkdownExporter {}
impl MarkupExporter for MarkdownExporter { impl MarkupExporter for MarkdownExporter {
fn table_row(&self, data: &[&str]) -> String { fn table_row(&self, cells: &[&str]) -> String {
format!("| {} |\n", data.join(" | ")) format!("| {} |\n", cells.join(" | "))
} }
fn table_line(&self, size: usize) -> String { fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
format!("|:---|{}\n", "---:|".repeat(size - 1)) format!(
"|{}\n",
cell_aligmnents
.iter()
.map(|a| match a {
Alignment::Left => ":---|",
Alignment::Right => "---:|",
})
.collect::<String>()
)
} }
fn command(&self, cmd: &str) -> String { fn command(&self, cmd: &str) -> String {
@@ -22,20 +33,16 @@ impl MarkupExporter for MarkdownExporter {
fn test_markdown_formatter_table_data() { fn test_markdown_formatter_table_data() {
let formatter = MarkdownExporter::default(); let formatter = MarkdownExporter::default();
let actual = formatter.table_row(&["a", "b", "c"]); assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
let expect = "| a | b | c |\n";
assert_eq!(expect, actual);
} }
/// Check Markdown-based horizontal line formatting /// Check Markdown-based horizontal line formatting
#[test] #[test]
fn test_markdown_formatter_table_line() { fn test_markdown_formatter_table_divider() {
let formatter = MarkdownExporter::default(); let formatter = MarkdownExporter::default();
let size = 5;
let actual = formatter.table_line(size); let actual = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
let expect = "|:---|---:|---:|---:|---:|\n"; let expect = "|:---|---:|:---|\n";
assert_eq!(expect, actual); assert_eq!(expect, actual);
} }
+17 -8
View File
@@ -6,23 +6,32 @@ use crate::util::units::Unit;
use super::Exporter; use super::Exporter;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
pub enum Alignment {
Left,
Right,
}
pub trait MarkupExporter { pub trait MarkupExporter {
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String { fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings // prepare table header strings
let notation = format!("[{}]", unit.short_name()); let notation = format!("[{}]", unit.short_name());
let head: [&str; 5] = [
// emit header
let mut table = self.table_row(&[
"Command", "Command",
&format!("Mean {}", notation), &format!("Mean {}", notation),
&format!("Min {}", notation), &format!("Min {}", notation),
&format!("Max {}", notation), &format!("Max {}", notation),
"Relative", "Relative",
]; ]);
// emit header
let mut table = self.table_row(&head);
// emit horizontal line // 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 { for entry in entries {
let measurement = &entry.result; let measurement = &entry.result;
@@ -58,9 +67,9 @@ pub trait MarkupExporter {
table 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; fn command(&self, size: &str) -> String;
} }