diff options
author | David Peter <mail@david-peter.de> | 2022-05-15 21:28:35 +0200 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2022-05-15 21:37:40 +0200 |
commit | 8c05fd030d36bff1784b2a46de25e12a6991702d (patch) | |
tree | 2c708032466da65c88d9cd1a13fb16151b02dc6f | |
parent | 5cd28212663f71d3546f231bd6fec14c8cd60990 (diff) |
table_line => table_divider
-rw-r--r-- | src/export/markdown.rs | 31 | ||||
-rw-r--r-- | src/export/markup.rs | 25 |
2 files changed, 36 insertions, 20 deletions
diff --git a/src/export/markdown.rs b/src/export/markdown.rs index c85401e..7447715 100644 --- a/src/export/markdown.rs +++ b/src/export/markdown.rs @@ -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); } diff --git a/src/export/markup.rs b/src/export/markup.rs index 8f6e1c4..ac18bba 100644 --- a/src/export/markup.rs +++ b/src/export/markup.rs @@ -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; } |