Skip to content
Myr

mod.rs

This file

Path

src/export/mod.rs

162 lines · 4.9 KiB · Rust

Last changed by

SortOrder fieldDavid Peterbefore adoption038f369

History of this file

Take it

The walk stopped after 200 commits: lines older than ddc733d point at it.

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 1–9why, one step back
1use std::fs::{File, OpenOptions};
2use std::io::Write;34mod asciidoc;5mod csv;6mod json;7mod markdown;8mod markup;9mod orgmode;

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 10–11why, one step back
10#[cfg(test)]
11mod tests;

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 12–19why, one step back
12
13use self::asciidoc::AsciidocExporter;14use self::csv::CsvExporter;15use self::json::JsonExporter;16use self::markdown::MarkdownExporter;17use self::orgmode::OrgmodeExporter;1819use crate::benchmark::benchmark_result::BenchmarkResult;

Add new --sort optionDavid Peterbefore adoptionac617c8

line 20why, one step back
20use crate::options::SortOrder;

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 21–47why, one step back
21use crate::util::units::Unit;
2223use anyhow::{Context, Result};24use clap::ArgMatches;2526/// The desired form of exporter to use for a given file.27#[derive(Clone)]28pub enum ExportType {29 /// Asciidoc Table30 Asciidoc,3132 /// CSV (comma separated values) format33 Csv,3435 /// JSON format36 Json,3738 /// Markdown table39 Markdown,4041 /// Emacs org-mode tables42 Orgmode,43}4445/// Interface for different exporters.46trait Exporter {47 /// Export the given entries in the serialized form.

Add new --sort optionDavid Peterbefore adoptionac617c8

lines 48–53why, one step back
48 fn serialize(
49 &self,50 results: &[BenchmarkResult],51 unit: Option<Unit>,52 sort_order: SortOrder,53 ) -> Result<Vec<u8>>;

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 54–55why, one step back
54}
55

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 56–61why, one step back
56pub enum ExportTarget {
57 File(String),58 Stdout,59}6061struct ExporterWithTarget {

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

line 62why, one step back
62 exporter: Box<dyn Exporter>,

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

line 63why, one step back
63 target: ExportTarget,

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 64–67why, one step back
64}
6566/// Handles the management of multiple file exporters.67pub struct ExportManager {

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 68–69why, one step back
68 exporters: Vec<ExporterWithTarget>,
69 time_unit: Option<Unit>,

SortOrder fieldDavid Peterbefore adoption038f369

line 70why, one step back
70 sort_order: SortOrder,

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 71–75why, one step back
71}
7273impl ExportManager {74 /// Build the ExportManager that will export the results specified75 /// in the given ArgMatches

SortOrder fieldDavid Peterbefore adoption038f369

lines 76–80why, one step back
76 pub fn from_cli_arguments(
77 matches: &ArgMatches,78 time_unit: Option<Unit>,79 sort_order: SortOrder,80 ) -> Result<Self> {

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 81–83why, one step back
81 let mut export_manager = Self {
82 exporters: vec![],83 time_unit,

SortOrder fieldDavid Peterbefore adoption038f369

line 84why, one step back
84 sort_order,

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

line 85why, one step back
85 };

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 86–87why, one step back
86 {
87 let mut add_exporter = |flag, exporttype| -> Result<()> {

add clap deprecation noticesBenjamin Rombautbefore adoption5fcb716

line 88why, one step back
88 if let Some(filename) = matches.get_one::<String>(flag) {

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 89–104why, one step back
89 export_manager.add_exporter(exporttype, filename)?;
90 }91 Ok(())92 };93 add_exporter("export-asciidoc", ExportType::Asciidoc)?;94 add_exporter("export-json", ExportType::Json)?;95 add_exporter("export-csv", ExportType::Csv)?;96 add_exporter("export-markdown", ExportType::Markdown)?;97 add_exporter("export-orgmode", ExportType::Orgmode)?;98 }99 Ok(export_manager)100 }101102 /// Add an additional exporter to the ExportManager103 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {104 let exporter: Box<dyn Exporter> = match export_type {

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 105–109why, one step back
105 ExportType::Asciidoc => Box::<AsciidocExporter>::default(),
106 ExportType::Csv => Box::<CsvExporter>::default(),107 ExportType::Json => Box::<JsonExporter>::default(),108 ExportType::Markdown => Box::<MarkdownExporter>::default(),109 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

line 110why, one step back
110 };

PR for issue #615Humble Penguinbefore adoptionbbe706b

line 111why, one step back
111

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

line 112why, one step back
112 self.exporters.push(ExporterWithTarget {

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

line 113why, one step back
113 exporter,

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 114–115why, one step back
114 target: if filename == "-" {
115 ExportTarget::Stdout

PR for issue #615Humble Penguinbefore adoptionbbe706b

line 116why, one step back
116 } else {

Prevent creation of file named '-'David Peterbefore adoption5f5adda

line 117why, one step back
117 let _ = File::create(filename)

style: simplify string interpolationHamir Mahalbefore adoptionef12632

line 118why, one step back
118 .with_context(|| format!("Could not create export file '{filename}'"))?;

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

line 119why, one step back
119 ExportTarget::File(filename.to_string())

PR for issue #615Humble Penguinbefore adoptionbbe706b

line 120why, one step back
120 },

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 121–125why, one step back
121 });
122123 Ok(())124 }125

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 126–131why, one step back
126 /// Write the given results to all Exporters. The 'intermediate' flag specifies
127 /// whether this is being called while still performing benchmarks, or if this128 /// is the final call after all benchmarks have been finished. In the former case,129 /// results are written to all file targets (to always have them up to date, even130 /// if a benchmark fails). In the latter case, we only print to stdout targets (in131 /// order not to clutter the output of hyperfine with intermediate results).

SortOrder fieldDavid Peterbefore adoption038f369

line 132why, one step back
132 pub fn write_results(&self, results: &[BenchmarkResult], intermediate: bool) -> Result<()> {

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

line 133why, one step back
133 for e in &self.exporters {

SortOrder fieldDavid Peterbefore adoption038f369

lines 134–137why, one step back
134 let content = || {
135 e.exporter136 .serialize(results, self.time_unit, self.sort_order)137 };

Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b

lines 138–151why, one step back
138
139 match e.target {140 ExportTarget::File(ref filename) => {141 if intermediate {142 write_to_file(filename, &content()?)?143 }144 }145 ExportTarget::Stdout => {146 if !intermediate {147 println!();148 println!("{}", String::from_utf8(content()?).unwrap());149 }150 }151 }

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

lines 152–160why, one step back
152 }
153 Ok(())154 }155}156157/// Write the given content to a file with the specified name158fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {159 let mut file = OpenOptions::new().write(true).open(filename)?;160 file.write_all(content)

style: simplify string interpolationHamir Mahalbefore adoptionef12632

line 161why, one step back
161 .with_context(|| format!("Failed to export results to '{filename}'"))

Bump anyhow from 1.0.58 to 1.0.63dependabot[bot]before adoptionddc733d

line 162why, one step back
162}