Skip to content
Myr

mod.rs

This file

Path

src/export/mod.rs

168 lines · 5.2 KiB · Rust

Last changed by

Merge pull request #970 from sharkdp/codex/truncate-export-rewritesDavid Peterbefore adoption4aa36c8

History of this file

Take it

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

Fix pipeline!David Peterbefore adoptionca9e29a

line 1why, one step back
1use std::fs::{File, OpenOptions};

Handle broken pipe errors on stdout gracefully (#932)jkbefore adoptionc0b3834

line 2why, one step back
2use std::io::{self, Write};

Fix pipeline!David Peterbefore adoptionca9e29a

lines 3–9why, one step back
3
4mod 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;

Fix pipeline!David Peterbefore adoptionca9e29a

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;

Handle broken pipe errors on stdout gracefully (#932)jkbefore adoptionc0b3834

line 21why, one step back
21use crate::output::console_writeln;

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 22why, one step back
22use crate::quantity::TimeUnit;

Fix pipeline!David Peterbefore adoptionca9e29a

lines 23–48why, one step back
23
24use anyhow::{Context, Result};25use clap::ArgMatches;2627/// The desired form of exporter to use for a given file.28#[derive(Clone)]29pub enum ExportType {30 /// Asciidoc Table31 Asciidoc,3233 /// CSV (comma separated values) format34 Csv,3536 /// JSON format37 Json,3839 /// Markdown table40 Markdown,4142 /// Emacs org-mode tables43 Orgmode,44}4546/// Interface for different exporters.47trait Exporter {48 /// Export the given entries in the serialized form.

Add new --sort optionDavid Peterbefore adoptionac617c8

lines 49–51why, one step back
49 fn serialize(
50 &self,51 results: &[BenchmarkResult],

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 52why, one step back
52 unit: Option<TimeUnit>,

Add new --sort optionDavid Peterbefore adoptionac617c8

lines 53–54why, one step back
53 sort_order: SortOrder,
54 ) -> Result<Vec<u8>>;

Fix pipeline!David Peterbefore adoptionca9e29a

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

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

lines 57–62why, one step back
57pub enum ExportTarget {
58 File(String),59 Stdout,60}6162struct ExporterWithTarget {

Fix pipeline!David Peterbefore adoptionca9e29a

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

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

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

Fix pipeline!David Peterbefore adoptionca9e29a

lines 65–68why, one step back
65}
6667/// Handles the management of multiple file exporters.68pub struct ExportManager {

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

line 69why, one step back
69 exporters: Vec<ExporterWithTarget>,

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 70why, one step back
70 time_unit: Option<TimeUnit>,

SortOrder fieldDavid Peterbefore adoption038f369

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

Fix pipeline!David Peterbefore adoptionca9e29a

lines 72–76why, one step back
72}
7374impl ExportManager {75 /// Build the ExportManager that will export the results specified76 /// in the given ArgMatches

SortOrder fieldDavid Peterbefore adoption038f369

lines 77–78why, one step back
77 pub fn from_cli_arguments(
78 matches: &ArgMatches,

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 79why, one step back
79 time_unit: Option<TimeUnit>,

SortOrder fieldDavid Peterbefore adoption038f369

lines 80–81why, one step back
80 sort_order: SortOrder,
81 ) -> Result<Self> {

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

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

SortOrder fieldDavid Peterbefore adoption038f369

line 85why, one step back
85 sort_order,

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

line 86why, one step back
86 };

Fix pipeline!David Peterbefore adoptionca9e29a

lines 87–112why, one step back
87 {
88 let mut add_exporter = |flag, exporttype| -> Result<()> {89 if let Some(filename) = matches.get_one::<String>(flag) {90 export_manager.add_exporter(exporttype, filename)?;91 }92 Ok(())93 };94 add_exporter("export-asciidoc", ExportType::Asciidoc)?;95 add_exporter("export-json", ExportType::Json)?;96 add_exporter("export-csv", ExportType::Csv)?;97 add_exporter("export-markdown", ExportType::Markdown)?;98 add_exporter("export-orgmode", ExportType::Orgmode)?;99 }100 Ok(export_manager)101 }102103 /// Add an additional exporter to the ExportManager104 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {105 let exporter: Box<dyn Exporter> = match export_type {106 ExportType::Asciidoc => Box::<AsciidocExporter>::default(),107 ExportType::Csv => Box::<CsvExporter>::default(),108 ExportType::Json => Box::<JsonExporter>::default(),109 ExportType::Markdown => Box::<MarkdownExporter>::default(),110 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),111 };112

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

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

Fix pipeline!David Peterbefore adoptionca9e29a

line 114why, one step back
114 exporter,

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

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

Fix pipeline!David Peterbefore adoptionca9e29a

lines 117–118why, one step back
117 } else {
118 let _ = File::create(filename)

style: simplify string interpolationHamir Mahalbefore adoptionef12632

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

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

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

Fix pipeline!David Peterbefore adoptionca9e29a

lines 121–126why, one step back
121 },
122 });123124 Ok(())125 }126

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

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

SortOrder fieldDavid Peterbefore adoption038f369

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

Fix pipeline!David Peterbefore adoptionca9e29a

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

SortOrder fieldDavid Peterbefore adoption038f369

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

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

lines 139–147why, one step back
139
140 match e.target {141 ExportTarget::File(ref filename) => {142 if intermediate {143 write_to_file(filename, &content()?)?144 }145 }146 ExportTarget::Stdout => {147 if !intermediate {

Handle broken pipe errors on stdout gracefully (#932)jkbefore adoptionc0b3834

lines 148–150why, one step back
148 let mut stdout = io::stdout().lock();
149 console_writeln!(stdout)?;150 console_writeln!(stdout, "{}", String::from_utf8(content()?).unwrap())?;

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

lines 151–153why, one step back
151 }
152 }153 }

Fix pipeline!David Peterbefore adoptionca9e29a

lines 154–160why, one step back
154 }
155 Ok(())156 }157}158159/// Write the given content to a file with the specified name160fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {

Merge pull request #970 from sharkdp/codex/truncate-export-rewritesDavid Peterbefore adoption4aa36c8

lines 161–165why, one step back
161 let mut file = OpenOptions::new()
162 .write(true)163 // Avoid leaving trailing bytes when the new export is shorter.164 .truncate(true)165 .open(filename)?;

Fix pipeline!David Peterbefore adoptionca9e29a

line 166why, one step back
166 file.write_all(content)

style: simplify string interpolationHamir Mahalbefore adoptionef12632

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

Fix pipeline!David Peterbefore adoptionca9e29a

line 168why, one step back
168}