Move Exporter::unit => determine_unit_from_results
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 69d38a5 · Browse files · 2 files·+20 added−25 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+20 added−25 removed
- other:+20 added−25 removed
Provenance
- Commit
8741d05- Tree
d63d0c3- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
69d38a5
2 files changed+20 added−25 removed
modified:src/export/markdown.rs+2 added-2 removed
1-use super::Exporter;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;5 use crate::util::units::Unit;21 unmodified lines27 pub struct MarkdownExporter {}28 29 impl Exporter for MarkdownExporter {30 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {31- let unit = self.unit(results, unit);31+ let unit = determine_unit_from_results(results, unit);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."247 unmodified linesmodified:src/export/mod.rs+18 added-23 removed
17 unmodified lines18 19 use anyhow::{Context, Result};20 use clap::ArgMatches;21 22+pub fn determine_unit_from_results(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {23+ return if let Some(unit) = unit {24+ // Use the given unit for all entries.25+ unit26+ } else if let Some(first_result) = results.first() {27+ // Use the first BenchmarkResult entry to determine the unit for all entries.28+ format_duration_value(first_result.mean, None).129+ } else {30+ // Default to `Second`.31+ Unit::Second32+ };33+}34+35 /// The desired form of exporter to use for a given file.36 #[derive(Clone)]37 pub enum ExportType {38 /// Asciidoc Table12 unmodified lines51 /// Interface for different exporters.52 trait Exporter {53 /// Export the given entries in the serialized form.54 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;42-43- fn unit(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {44- return if let Some(unit) = unit {45- // Use the given unit for all entries.46- unit47- } else if let Some(first_result) = results.first() {48- // Use the first BenchmarkResult entry to determine the unit for all entries.49- format_duration_value(first_result.mean, None).150- } else {51- // Default to `Second`.52- Unit::Second53- };54- }55 }56 57 struct ExporterWithFilename {58 exporter: Box<dyn Exporter>,110 unmodified lines169 },170 ];171 let unit = Some(Unit::Second);172 173- let exporter = TestExporter::default();174- let markup_actual = exporter.unit(&results, unit);173+ let markup_actual = determine_unit_from_results(&results, unit);174 let markup_expected = Unit::Second;175 176 assert_eq!(markup_expected, markup_actual);177 }31 unmodified lines209 },210 ];211 let unit = Some(Unit::MilliSecond);212 214- let exporter = TestExporter::default();215- let markup_actual = exporter.unit(&results, unit);213+ let markup_actual = determine_unit_from_results(&results, unit);214 let markup_expected = Unit::MilliSecond;215 216 assert_eq!(markup_expected, markup_actual);217 }31 unmodified lines249 },250 ];251 let unit = None;252 255- let exporter = TestExporter::default();256- let markup_actual = exporter.unit(&results, unit);253+ let markup_actual = determine_unit_from_results(&results, unit);254 let markup_expected = Unit::Second;255 256 assert_eq!(markup_expected, markup_actual);257 }31 unmodified lines289 },290 ];291 let unit = None;292 296- let exporter = TestExporter::default();297- let markup_actual = exporter.unit(&results, unit);293+ let markup_actual = determine_unit_from_results(&results, unit);294 let markup_expected = Unit::MilliSecond;295 296 assert_eq!(markup_expected, markup_actual);297 }3 unmodified lines301 fn test_markup_table_unit_default_s() {302 let results: Vec<BenchmarkResult> = vec![];303 let unit = None;304 309- let exporter = TestExporter::default();310- let markup_actual = exporter.unit(&results, unit);305+ let markup_actual = determine_unit_from_results(&results, unit);306 let markup_expected = Unit::Second;307 308 assert_eq!(markup_expected, markup_actual);309 }