Skip to content
Myr

Implement Exporter for all MarkupExporters

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent 9a46bcc · Browse files · 2 files+35 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+35 added−31 removed

  • other:+35 added−31 removed

2 files+35 added−31 removed

modified:src/export/markdown.rs+15 added-30 removed
1use super::{determine_unit_from_results, Exporter};2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::benchmark::relative_speed;4use crate::export::markup::MarkupFormatter;1use crate::export::markup::MarkupExporter;5use crate::util::units::Unit;27use anyhow::{anyhow, Result};83#[derive(Default)]10pub struct MarkdownFormatter;4pub struct MarkdownExporter {}512impl MarkupFormatter for MarkdownFormatter {6impl MarkupExporter for MarkdownExporter {7 fn table_data(&self, data: &[&str]) -> String {8 format!("| {} |\n", data.join(" | "))9 }10
5 unmodified lines
16 format!("`{}`", cmd)17 }18}1926#[derive(Default)]27pub struct MarkdownExporter {}2829impl Exporter for MarkdownExporter {30 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {31 let unit = unit.unwrap_or_else(|| determine_unit_from_results(&results));32 let entries = relative_speed::compute(results);33 if entries.is_none() {34 return Err(anyhow!(35 "Relative speed comparison is not available for Markdown export."36 ));37 }3839 let formatter = MarkdownFormatter::default();40 let table = formatter.table_results(&entries.unwrap(), unit);41 Ok(table.as_bytes().to_vec())42 }43}4420/// Check Markdown-based data row formatting21#[test]22fn test_markdown_formatter_table_data() {48 let formatter = MarkdownFormatter::default();23 let formatter = MarkdownExporter::default();24 let data = vec!["a", "b", "c"];2526 let actual = formatter.table_data(&data);27 let expect = "| a | b | c |\n";
3 unmodified lines
3132/// Check Markdown-based horizontal line formatting33#[test]34fn test_markdown_formatter_table_line() {60 let formatter = MarkdownFormatter::default();35 let formatter = MarkdownExporter::default();36 let size = 5;3738 let actual = formatter.table_line(size);39 let expect = "|:---|---:|---:|---:|---:|\n";
18 unmodified lines
58/// This also demonstrates that the first entry's units (ms) are used to set59/// the units for all entries when the time unit is not given.60#[test]61fn test_markdown_format_ms() {62 use super::Exporter;63 use crate::benchmark::benchmark_result::BenchmarkResult;64 use std::collections::BTreeMap;65 let exporter = MarkdownExporter::default();6667 let timing_results = vec![
40 unmodified lines
108/// This (again) demonstrates that the first entry's units (s) are used to set109/// the units for all entries when the time unit is not given.110#[test]111fn test_markdown_format_s() {112 use super::Exporter;113 use crate::benchmark::benchmark_result::BenchmarkResult;114 use std::collections::BTreeMap;115 let exporter = MarkdownExporter::default();116117 let timing_results = vec![
39 unmodified lines
157158/// The given time unit (s) is used to set the units for all entries.159#[test]160fn test_markdown_format_time_unit_s() {161 use super::Exporter;162 use crate::benchmark::benchmark_result::BenchmarkResult;163 use crate::util::units::Unit;164 use std::collections::BTreeMap;165 let exporter = MarkdownExporter::default();166167 let timing_results = vec![
45 unmodified lines
213/// This (again) demonstrates that the given time unit (ms) is used to set214/// the units for all entries.215#[test]216fn test_markdown_format_time_unit_ms() {217 use super::Exporter;218 use crate::benchmark::benchmark_result::BenchmarkResult;219 use crate::util::units::Unit;220 use std::collections::BTreeMap;221 let exporter = MarkdownExporter::default();222223 let timing_results = vec![
44 unmodified lines
modified:src/export/markup.rs+20 added-1 removed
1use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;2use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};3use crate::output::format::format_duration_value;4use crate::util::units::Unit;56use super::{determine_unit_from_results, Exporter};7use anyhow::{anyhow, Result};85pub trait MarkupFormatter {9pub trait MarkupExporter {10 fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {11 // prepare table header strings12 let notation = format!("[{}]", unit.short_name());13 let head: [&str; 5] = [
49 unmodified lines
63 fn table_line(&self, size: usize) -> String;6465 fn command(&self, size: &str) -> String;66}6768impl<T: MarkupExporter> Exporter for T {69 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {70 let unit = unit.unwrap_or_else(|| determine_unit_from_results(&results));71 let entries = relative_speed::compute(results);72 if entries.is_none() {73 return Err(anyhow!(74 "Relative speed comparison is not available for markup exporter."75 ));76 }7778 let table = self.table_results(&entries.unwrap(), unit);79 Ok(table.as_bytes().to_vec())80 }81}