Refactor MarkupExporter to omit intermediate table result creation and simplify MarkupFormatter emitting
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* see: https://github.com/sharkdp/hyperfine/pull/490#pullrequestreview-932424652
authored by Philipp Paulweber · committed by David Peter · parent 9dc87ac · Browse files · 2 files·+27 added−48 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+27 added−48 removed
- other:+27 added−48 removed
Provenance
- Commit
69d38a5- Tree
eb834f7- Author
Philipp Paulweber <p.paulweber@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
9dc87ac
2 files changed+27 added−48 removed
modified:src/export/markdown.rs+1 added-7 removed
35 unmodified lines36 ));37 }38 39 let formatter = MarkdownFormatter::default();40- let data_vvs = formatter.results(&entries.unwrap(), unit);41- let data_vas: Vec<Vec<&str>> = data_vvs42- .iter()43- .map(|list| list.iter().map(AsRef::as_ref).collect::<Vec<&str>>())44- .collect();45- let data: Vec<&[&str]> = data_vas.iter().map(AsRef::as_ref).collect();46- let table = formatter.table(&data);40+ let table = formatter.table_results(&entries.unwrap(), unit);41 Ok(table.as_bytes().to_vec())42 }43 }44 238 unmodified linesmodified:src/export/markup.rs+26 added-41 removed
1 unmodified line2 use crate::output::format::format_duration_value;3 use crate::util::units::Unit;4 5 pub trait MarkupFormatter {6- fn table(&self, data: &[&[&str]]) -> String {7- let head: &[&str] = data.first().unwrap();8- let tail = &data[1..];6+ fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {7+ // prepare table header strings8+ let notation = format!("[{}]", unit.short_name());9+ let head: [&str; 5] = [10+ "Command",11+ &format!("Mean {}", notation),12+ &format!("Min {}", notation),13+ &format!("Max {}", notation),14+ "Relative",15+ ];16 17 // emit header11- let table = &mut self.table_data(head);18+ let mut table = self.table_data(&head);19 20 // emit horizontal line21 table.push_str(&self.table_line(head.len()));22 16- // emit data rows17- for row in tail {18- table.push_str(&self.table_data(row))19- }20-21- table.to_string()22- }23-24- fn table_data(&self, data: &[&str]) -> String;25-26- fn table_line(&self, size: usize) -> String;27-28- fn command(&self, size: &str) -> String;29-30- fn results(31- &self,32- entries: &[BenchmarkResultWithRelativeSpeed],33- unit: Unit,34- ) -> Vec<Vec<String>> {35- // prepare table header strings36- let notation = format!("[{}]", unit.short_name());37- let mut data: Vec<Vec<_>> = vec![vec![38- "Command".to_string(),39- format!("Mean {}", notation),40- format!("Min {}", notation),41- format!("Max {}", notation),42- "Relative".to_string(),43- ]];44-23 for entry in entries {24 let measurement = &entry.result;25 // prepare data row strings26 let cmd_str = measurement.command.replace('|', "\\|");13 unmodified lines40 } else {41 "".into()42 };43 // prepare table row entries66- data.push(vec![67- self.command(&cmd_str),68- format!("{}{}", mean_str, stddev_str),69- min_str,70- max_str,71- format!("{}{}", rel_str, rel_stddev_str),72- ])44+ let data: [&str; 5] = [45+ &self.command(&cmd_str),46+ &format!("{}{}", mean_str, stddev_str),47+ &min_str,48+ &max_str,49+ &format!("{}{}", rel_str, rel_stddev_str),50+ ];51+ table.push_str(&self.table_data(&data))52 }53 75- data54+ table55 }56+57+ fn table_data(&self, data: &[&str]) -> String;58+59+ fn table_line(&self, size: usize) -> String;60+61+ fn command(&self, size: &str) -> String;62 }