Refactor MarkdownExporter emitting using a generic MarkupFormatter trait

* see: https://github.com/sharkdp/hyperfine/pull/490#issuecomment-1076784074
This commit is contained in:
Philipp Paulweber committed 2022-04-07 00:11:15 +02:00
1 parent 5470a08c85
commit 288e9fb4c9
3 files changed
+335 -312

No files matched your search

+68 -29
View File
@@ -1,19 +1,34 @@
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::benchmark::relative_speed;
use crate::export::markup::markup_results;
use crate::export::markup::markup_table;
use crate::export::markup::markup_unit;
use crate::export::markup::MarkupType;
use crate::export::markup::MarkupFormatter;
use crate::util::units::Unit;
use anyhow::{anyhow, Result};
#[derive(Default)]
pub struct MarkdownFormatter;
impl MarkupFormatter for MarkdownFormatter {
fn table_data(&self, data: &[&str]) -> String {
format!("| {} |\n", data.join(" | "))
}
fn table_line(&self, size: usize) -> String {
format!("|:---|{}\n", "---:|".repeat(size - 1))
}
fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd)
}
}
#[derive(Default)]
pub struct MarkdownExporter {}
impl Exporter for MarkdownExporter {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = self.unit(results, unit);
let entries = relative_speed::compute(results);
if entries.is_none() {
return Err(anyhow!(
@@ -21,18 +36,46 @@ impl Exporter for MarkdownExporter {
));
}
let kind = MarkupType::Markdown;
let unit = markup_unit(results, unit);
let data = markup_results(&kind, &entries.unwrap(), unit);
let table = markup_table(&kind, &data);
let formatter = MarkdownFormatter::default();
let data_vvs = formatter.results(&entries.unwrap(), unit);
let data_vas: Vec<Vec<&str>> = data_vvs
.iter()
.map(|list| list.iter().map(AsRef::as_ref).collect::<Vec<&str>>())
.collect();
let data: Vec<&[&str]> = data_vas.iter().map(AsRef::as_ref).collect();
let table = formatter.table(&data);
Ok(table.as_bytes().to_vec())
}
}
/// Check Markdown-based data row formatting
#[test]
fn test_markdown_formatter_table_data() {
let formatter = MarkdownFormatter::default();
let data = vec!["a", "b", "c"];
let actual = formatter.table_data(&data);
let expect = "| a | b | c |\n";
assert_eq!(expect, actual);
}
/// Check Markdown-based horizontal line formatting
#[test]
fn test_markdown_formatter_table_line() {
let formatter = MarkdownFormatter::default();
let size = 5;
let actual = formatter.table_line(size);
let expect = "|:---|---:|---:|---:|---:|\n";
assert_eq!(expect, actual);
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Markdown.
#[cfg(test)]
fn test_table_header(unit_short_name: String) -> String {
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
unit = unit_short_name
@@ -79,17 +122,16 @@ fn test_markdown_format_ms() {
},
];
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
",
test_table_header("ms".to_string())
cfg_test_table_header("ms".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// This (again) demonstrates that the first entry's units (s) are used to set
@@ -128,17 +170,16 @@ fn test_markdown_format_s() {
},
];
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
test_table_header("s".to_string())
cfg_test_table_header("s".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// The given time unit (s) is used to set the units for all entries.
@@ -176,22 +217,21 @@ fn test_markdown_format_time_unit_s() {
},
];
let formatted = String::from_utf8(
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
let expect = format!(
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
",
test_table_header("s".to_string())
cfg_test_table_header("s".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// This (again) demonstrates that the given time unit (ms) is used to set
@@ -230,20 +270,19 @@ fn test_markdown_format_time_unit_ms() {
},
];
let formatted = String::from_utf8(
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
let expect = format!(
"{}\
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
",
test_table_header("ms".to_string())
cfg_test_table_header("ms".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}