Refactor MarkupExporter to omit intermediate table result creation and simplify MarkupFormatter emitting

* see: https://github.com/sharkdp/hyperfine/pull/490#pullrequestreview-932424652
This commit is contained in:
Philipp Paulweber authored and David Peter committed 2022-05-15 21:37:40 +02:00
1 parent 9dc87ac349
commit 69d38a5f8e
2 files changed
+27 -48

No files matched your search

+1 -7
View File
@@ -37,13 +37,7 @@ impl Exporter for MarkdownExporter {
}
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);
let table = formatter.table_results(&entries.unwrap(), unit);
Ok(table.as_bytes().to_vec())
}
}
+26 -41
View File
@@ -3,45 +3,23 @@ use crate::output::format::format_duration_value;
use crate::util::units::Unit;
pub trait MarkupFormatter {
fn table(&self, data: &[&[&str]]) -> String {
let head: &[&str] = data.first().unwrap();
let tail = &data[1..];
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
let head: [&str; 5] = [
"Command",
&format!("Mean {}", notation),
&format!("Min {}", notation),
&format!("Max {}", notation),
"Relative",
];
// emit header
let table = &mut self.table_data(head);
let mut table = self.table_data(&head);
// emit horizontal line
table.push_str(&self.table_line(head.len()));
// emit data rows
for row in tail {
table.push_str(&self.table_data(row))
}
table.to_string()
}
fn table_data(&self, data: &[&str]) -> String;
fn table_line(&self, size: usize) -> String;
fn command(&self, size: &str) -> String;
fn results(
&self,
entries: &[BenchmarkResultWithRelativeSpeed],
unit: Unit,
) -> Vec<Vec<String>> {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
let mut data: Vec<Vec<_>> = vec![vec![
"Command".to_string(),
format!("Mean {}", notation),
format!("Min {}", notation),
format!("Max {}", notation),
"Relative".to_string(),
]];
for entry in entries {
let measurement = &entry.result;
// prepare data row strings
@@ -63,15 +41,22 @@ pub trait MarkupFormatter {
"".into()
};
// prepare table row entries
data.push(vec![
self.command(&cmd_str),
format!("{}{}", mean_str, stddev_str),
min_str,
max_str,
format!("{}{}", rel_str, rel_stddev_str),
])
let data: [&str; 5] = [
&self.command(&cmd_str),
&format!("{}{}", mean_str, stddev_str),
&min_str,
&max_str,
&format!("{}{}", rel_str, rel_stddev_str),
];
table.push_str(&self.table_data(&data))
}
data
table
}
fn table_data(&self, data: &[&str]) -> String;
fn table_line(&self, size: usize) -> String;
fn command(&self, size: &str) -> String;
}