Implement Exporter for all MarkupExporters

This commit is contained in:
David Peter authored and David Peter committed 2022-05-15 21:37:40 +02:00
1 parent 9a46bcc390
commit 4cdf2d5152
2 files changed
+35 -31

No files matched your search

+20 -1
View File
@@ -1,8 +1,12 @@
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
use crate::output::format::format_duration_value;
use crate::util::units::Unit;
pub trait MarkupFormatter {
use super::{determine_unit_from_results, Exporter};
use anyhow::{anyhow, Result};
pub trait MarkupExporter {
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
@@ -60,3 +64,18 @@ pub trait MarkupFormatter {
fn command(&self, size: &str) -> String;
}
impl<T: MarkupExporter> Exporter for T {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let entries = relative_speed::compute(results);
if entries.is_none() {
return Err(anyhow!(
"Relative speed comparison is not available for markup exporter."
));
}
let table = self.table_results(&entries.unwrap(), unit);
Ok(table.as_bytes().to_vec())
}
}