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
Provenance
- Commit
4cdf2d5- Tree
4856fcf- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
9a46bcc
2 files changed+35 added−31 removed
modified:src/export/markdown.rs+15 added-30 removed
1-use super::{determine_unit_from_results, Exporter};2-use crate::benchmark::benchmark_result::BenchmarkResult;3-use crate::benchmark::relative_speed;4-use crate::export::markup::MarkupFormatter;1+use crate::export::markup::MarkupExporter;5-use crate::util::units::Unit;2 7-use anyhow::{anyhow, Result};8-3 #[derive(Default)]10-pub struct MarkdownFormatter;4+pub struct MarkdownExporter {}5 12-impl MarkupFormatter for MarkdownFormatter {6+impl MarkupExporter for MarkdownExporter {7 fn table_data(&self, data: &[&str]) -> String {8 format!("| {} |\n", data.join(" | "))9 }10 5 unmodified lines16 format!("`{}`", cmd)17 }18 }19 26-#[derive(Default)]27-pub struct MarkdownExporter {}28-29-impl 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- }38-39- let formatter = MarkdownFormatter::default();40- let table = formatter.table_results(&entries.unwrap(), unit);41- Ok(table.as_bytes().to_vec())42- }43-}44-20 /// Check Markdown-based data row formatting21 #[test]22 fn test_markdown_formatter_table_data() {48- let formatter = MarkdownFormatter::default();23+ let formatter = MarkdownExporter::default();24 let data = vec!["a", "b", "c"];25 26 let actual = formatter.table_data(&data);27 let expect = "| a | b | c |\n";3 unmodified lines31 32 /// Check Markdown-based horizontal line formatting33 #[test]34 fn test_markdown_formatter_table_line() {60- let formatter = MarkdownFormatter::default();35+ let formatter = MarkdownExporter::default();36 let size = 5;37 38 let actual = formatter.table_line(size);39 let expect = "|:---|---:|---:|---:|---:|\n";18 unmodified lines58 /// 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]61 fn 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();66 67 let timing_results = vec![40 unmodified lines108 /// 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]111 fn 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();116 117 let timing_results = vec![39 unmodified lines157 158 /// The given time unit (s) is used to set the units for all entries.159 #[test]160 fn 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();166 167 let timing_results = vec![45 unmodified lines213 /// This (again) demonstrates that the given time unit (ms) is used to set214 /// the units for all entries.215 #[test]216 fn 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();222 223 let timing_results = vec![44 unmodified linesmodified:src/export/markup.rs+20 added-1 removed
1 use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;2+use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};3 use crate::output::format::format_duration_value;4 use crate::util::units::Unit;5 6+use super::{determine_unit_from_results, Exporter};7+use anyhow::{anyhow, Result};8+5-pub trait MarkupFormatter {9+pub 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 lines63 fn table_line(&self, size: usize) -> String;64 65 fn command(&self, size: &str) -> String;66 }67+68+impl<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+ }77+78+ let table = self.table_results(&entries.unwrap(), unit);79+ Ok(table.as_bytes().to_vec())80+ }81+}