Refactor Markdown table contents generation 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 23d9fe2 · Browse files · 2 files·+50 added−42 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+50 added−42 removed
- other:+50 added−42 removed
Provenance
- Commit
3b1fc32- Tree
ec5b73e- Author
Philipp Paulweber <p.paulweber@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
23d9fe2
2 files changed+50 added−42 removed
modified:src/export/markdown.rs+3 added-42 removed
3 unmodified lines4 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;8-use crate::output::format::format_duration_value;8+use crate::export::markup::markup_results;9 use crate::util::units::Unit;10 11 use anyhow::{anyhow, Result};12 10 unmodified lines23 "Relative speed comparison is not available for Markdown export."24 ));25 }26 27- // prepare table header strings28- let notation = format!("[{}]", unit.short_name());29- let mut data: Vec<Vec<_>> = vec![vec![30- format!("Command"),31- format!("Mean {}", notation),32- format!("Min {}", notation),33- format!("Max {}", notation),34- format!("Relative"),35- ]];36-37- for entry in entries.unwrap() {38- let measurement = &entry.result;39- // prepare data row strings40- let cmd_str = measurement.command.replace("|", "\\|");41- let mean_str = format_duration_value(measurement.mean, Some(unit)).0;42- let stddev_str = if let Some(stddev) = measurement.stddev {43- format!(" ± {}", format_duration_value(stddev, Some(unit)).0)44- } else {45- "".into()46- };47- let min_str = format_duration_value(measurement.min, Some(unit)).0;48- let max_str = format_duration_value(measurement.max, Some(unit)).0;49- let rel_str = format!("{:.2}", entry.relative_speed);50- let rel_stddev_str = if entry.is_fastest {51- "".into()52- } else if let Some(stddev) = entry.relative_speed_stddev {53- format!(" ± {:.2}", stddev)54- } else {55- "".into()56- };57- // prepare table row entries58- data.push(vec![59- format!("`{}`", cmd_str),60- format!("{}{}", mean_str, stddev_str),61- format!("{}", min_str),62- format!("{}", max_str),63- format!("{}{}", rel_str, rel_stddev_str),64- ])65- }66-27+ 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..];69- let kind = MarkupType::Markdown;31 32 // emit header33 let mut table = markup_table_data(&kind, head);34 230 unmodified linesmodified:src/export/markup.rs+47 added
1 use crate::benchmark::benchmark_result::BenchmarkResult;2 use crate::output::format::format_duration_value;3 use crate::util::units::Unit;4+use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;5 6 #[derive(Clone)]7 pub enum MarkupType {8 /// Markdown table11 unmodified lines20 MarkupType::Markdown => format!("|:---|{}\n", "---:|".repeat(size - 1)),21 };22 }23 24+pub fn markup_results(kind: &MarkupType, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> Vec<Vec<String>> {25+ // prepare table header strings26+ let notation = format!("[{}]", unit.short_name());27+ let mut data: Vec<Vec<_>> = vec![vec![28+ format!("Command"),29+ format!("Mean {}", notation),30+ format!("Min {}", notation),31+ format!("Max {}", notation),32+ format!("Relative"),33+ ]];34+35+ for entry in entries {36+ let measurement = &entry.result;37+ // prepare data row strings38+ let cmd_str = measurement.command.replace("|", "\\|");39+ let mean_str = format_duration_value(measurement.mean, Some(unit)).0;40+ let stddev_str = if let Some(stddev) = measurement.stddev {41+ format!(" ± {}", format_duration_value(stddev, Some(unit)).0)42+ } else {43+ "".into()44+ };45+ let min_str = format_duration_value(measurement.min, Some(unit)).0;46+ let max_str = format_duration_value(measurement.max, Some(unit)).0;47+ let rel_str = format!("{:.2}", entry.relative_speed);48+ let rel_stddev_str = if entry.is_fastest {49+ "".into()50+ } else if let Some(stddev) = entry.relative_speed_stddev {51+ format!(" ± {:.2}", stddev)52+ } else {53+ "".into()54+ };55+ // prepare table row entries56+ data.push(vec![57+ match kind {58+ MarkupType::Markdown => format!( "`{}`", cmd_str ),59+ },60+ format!("{}{}", mean_str, stddev_str),61+ format!("{}", min_str),62+ format!("{}", max_str),63+ format!("{}{}", rel_str, rel_stddev_str),64+ ])65+ }66+67+ return data;68+}69+70 pub fn markup_results_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {71 return if let Some(unit) = unit {72 // Use the given unit for all entries.73 unit204 unmodified lines