Refactor Markdown table emitting to generic Markup function
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Philipp Paulweber · committed by David Peter · parent 3b1fc32 · Browse files · 2 files·+45 added−38 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+45 added−38 removed
- other:+45 added−38 removed
Provenance
- Commit
50cf7de- Tree
785fb2b- Author
Philipp Paulweber <p.paulweber@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
3b1fc32
2 files changed+45 added−38 removed
modified:src/export/markdown.rs+7 added-22 removed
1 use super::Exporter;2 use crate::benchmark::benchmark_result::BenchmarkResult;3 use crate::benchmark::relative_speed;4-use crate::export::markup::markup_results_unit;5-use crate::export::markup::markup_table_data;6-use crate::export::markup::markup_table_line;7-use crate::export::markup::MarkupType;4 use crate::export::markup::markup_results;5+use crate::export::markup::markup_table;6+use crate::export::markup::markup_unit;7+use crate::export::markup::MarkupType;8 use crate::util::units::Unit;9 10 use anyhow::{anyhow, Result};11 1 unmodified line13 pub struct MarkdownExporter {}14 15 impl Exporter for MarkdownExporter {16 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {18- let unit = markup_results_unit(results, unit);19-17 let entries = relative_speed::compute(results);18 if entries.is_none() {19 return Err(anyhow!(20 "Relative speed comparison is not available for Markdown export."21 ));22 }23 24 let kind = MarkupType::Markdown;28- let data = markup_results( &kind, &entries.unwrap(), unit );29- let head: &Vec<String> = data.first().unwrap();30- let tail: &[Vec<String>] = &data[1..];31-32- // emit header33- let mut table = markup_table_data(&kind, head);34-35- // emit horizontal line36- table.push_str(&markup_table_line(&kind, head.len()));37-38- // emit data rows39- for row in tail {40- table.push_str(&markup_table_data(&kind, row))41- }42-25+ let unit = markup_unit(results, unit);26+ let data = markup_results(&kind, &entries.unwrap(), unit);27+ let table = markup_table(&kind, &data);28 Ok(table.as_bytes().to_vec())29 }30 }31 32 /// Test helper function to create unit-based header and horizontal line48-/// independently from the markup functionality.33+/// independently from the markup functionality for Markdown.34 #[cfg(test)]35 fn test_table_header(unit_short_name: String) -> String {36 format!(37 "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",212 unmodified linesmodified:src/export/markup.rs+38 added-16 removed
1 use crate::benchmark::benchmark_result::BenchmarkResult;2+use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;3 use crate::output::format::format_duration_value;4 use crate::util::units::Unit;4-use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;5 6 #[derive(Clone)]7 pub enum MarkupType {8 /// Markdown table9 Markdown,10 }11 12+pub fn markup_table(kind: &MarkupType, data: &[Vec<String>]) -> String {13+ let head: &Vec<String> = data.first().unwrap();14+ let tail: &[Vec<String>] = &data[1..];15+16+ // emit header17+ let mut table = markup_table_data(&kind, head);18+19+ // emit horizontal line20+ table.push_str(&markup_table_line(&kind, head.len()));21+22+ // emit data rows23+ for row in tail {24+ table.push_str(&markup_table_data(&kind, row))25+ }26+27+ return table;28+}29+12-pub fn markup_table_data(kind: &MarkupType, data: &Vec<String>) -> String {30+fn markup_table_data(kind: &MarkupType, data: &Vec<String>) -> String {31 return match kind {32 MarkupType::Markdown => format!("| {} |\n", data.join(" | ")),33 };34 }35 18-pub fn markup_table_line(kind: &MarkupType, size: usize) -> String {36+fn markup_table_line(kind: &MarkupType, size: usize) -> String {37 return match kind {38 MarkupType::Markdown => format!("|:---|{}\n", "---:|".repeat(size - 1)),39 };40 }41 24-pub fn markup_results(kind: &MarkupType, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> Vec<Vec<String>> {42+pub fn markup_results(43+ kind: &MarkupType,44+ entries: &[BenchmarkResultWithRelativeSpeed],45+ unit: Unit,46+) -> Vec<Vec<String>> {47 // prepare table header strings48 let notation = format!("[{}]", unit.short_name());49 let mut data: Vec<Vec<_>> = vec![vec![50 format!("Command"),25 unmodified lines76 };77 // prepare table row entries78 data.push(vec![79 match kind {58- MarkupType::Markdown => format!( "`{}`", cmd_str ),80+ MarkupType::Markdown => format!("`{}`", cmd_str),81 },82 format!("{}{}", mean_str, stddev_str),83 format!("{}", min_str),84 format!("{}", max_str),3 unmodified lines88 89 return data;90 }91 70-pub fn markup_results_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {92+pub fn markup_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {93 return if let Some(unit) = unit {94 // Use the given unit for all entries.95 unit96 } else if let Some(first_result) = results.first() {30 unmodified lines127 }128 129 /// Check unit resolving for timing results and given unit 's'130 #[test]109-fn test_markup_table_results_unit_given_s() {131+fn test_markup_table_unit_given_s() {132 use std::collections::BTreeMap;133 let results = vec![134 BenchmarkResult {135 command: String::from("sleep 2"),23 unmodified lines159 },160 ];161 let unit = Some(Unit::Second);162 141- let markup_actual = markup_results_unit(&results, unit);163+ let markup_actual = markup_unit(&results, unit);164 let markup_expected = Unit::Second;165 166 assert_eq!(markup_expected, markup_actual);167 }168 169 /// Check unit resolving for timing results and given unit 'ms'170 #[test]149-fn test_markup_table_results_unit_given_ms() {171+fn test_markup_table_unit_given_ms() {172 use std::collections::BTreeMap;173 let results = vec![174 BenchmarkResult {175 command: String::from("sleep 2"),23 unmodified lines199 },200 ];201 let unit = Some(Unit::MilliSecond);202 181- let markup_actual = markup_results_unit(&results, unit);203+ let markup_actual = markup_unit(&results, unit);204 let markup_expected = Unit::MilliSecond;205 206 assert_eq!(markup_expected, markup_actual);207 }208 209 /// Check unit resolving for timing results using the first result entry as 's'210 #[test]189-fn test_markup_table_results_unit_first_s() {211+fn test_markup_table_unit_first_s() {212 use std::collections::BTreeMap;213 let results = vec![214 BenchmarkResult {215 command: String::from("sleep 2"),23 unmodified lines239 },240 ];241 let unit = None;242 221- let markup_actual = markup_results_unit(&results, unit);243+ let markup_actual = markup_unit(&results, unit);244 let markup_expected = Unit::Second;245 246 assert_eq!(markup_expected, markup_actual);247 }248 249 /// Check unit resolving for timing results using the first result entry as 'ms'250 #[test]229-fn test_markup_table_results_unit_first_ms() {251+fn test_markup_table_unit_first_ms() {252 use std::collections::BTreeMap;253 let results = vec![254 BenchmarkResult {255 command: String::from("sleep 0.1"),23 unmodified lines279 },280 ];281 let unit = None;282 261- let markup_actual = markup_results_unit(&results, unit);283+ let markup_actual = markup_unit(&results, unit);284 let markup_expected = Unit::MilliSecond;285 286 assert_eq!(markup_expected, markup_actual);287 }288 289 /// Check unit resolving for not timing results and no given unit defaulting to 's'290 #[test]269-fn test_markup_table_results_unit_default_s() {291+fn test_markup_table_unit_default_s() {292 let results: Vec<BenchmarkResult> = vec![];293 let unit = None;294 273- let markup_actual = markup_results_unit(&results, unit);295+ let markup_actual = markup_unit(&results, unit);296 let markup_expected = Unit::Second;297 298 assert_eq!(markup_expected, markup_actual);299 }