mod.rs
This file
Path
src/export/mod.rs
157 lines · 4.8 KiB · Rust
Last changed by
Add new --sort optionDavid Peterbefore adoptionac617c8
History of this fileTake it
The walk stopped after 200 commits: lines older than 308bf5c point at it.
Extract common functionalityDavid Peterbefore adoption308bf5c
lines 1–7why, one step back1use std::fs::{File, OpenOptions};2use std::io::Write;34mod asciidoc;5mod csv;6mod json;7mod markdown;Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2
line 8why, one step back8mod markup;Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383
line 9why, one step back9mod orgmode;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 10–14why, one step back1011use self::asciidoc::AsciidocExporter;12use self::csv::CsvExporter;13use self::json::JsonExporter;14use self::markdown::MarkdownExporter;Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383
line 15why, one step back15use self::orgmode::OrgmodeExporter;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 16–17why, one step back1617use crate::benchmark::benchmark_result::BenchmarkResult;Add new --sort optionDavid Peterbefore adoptionac617c8
line 18why, one step back18use crate::options::SortOrder;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 19–37why, one step back19use crate::util::units::Unit;2021use anyhow::{Context, Result};22use clap::ArgMatches;2324/// The desired form of exporter to use for a given file.25#[derive(Clone)]26pub enum ExportType {27 /// Asciidoc Table28 Asciidoc,2930 /// CSV (comma separated values) format31 Csv,3233 /// JSON format34 Json,3536 /// Markdown table37 Markdown,Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383
lines 38–40why, one step back3839 /// Emacs org-mode tables40 Orgmode,Extract common functionalityDavid Peterbefore adoption308bf5c
lines 41–45why, one step back41}4243/// Interface for different exporters.44trait Exporter {45 /// Export the given entries in the serialized form.Add new --sort optionDavid Peterbefore adoptionac617c8
lines 46–51why, one step back46 fn serialize(47 &self,48 results: &[BenchmarkResult],49 unit: Option<Unit>,50 sort_order: SortOrder,51 ) -> Result<Vec<u8>>;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 52–53why, one step back52}53Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 54–59why, one step back54pub enum ExportTarget {55 File(String),56 Stdout,57}5859struct ExporterWithTarget {Extract common functionalityDavid Peterbefore adoption308bf5c
line 60why, one step back60 exporter: Box<dyn Exporter>,Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
line 61why, one step back61 target: ExportTarget,Extract common functionalityDavid Peterbefore adoption308bf5c
lines 62–66why, one step back62}6364/// Handles the management of multiple file exporters.65#[derive(Default)]66pub struct ExportManager {Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 67–68why, one step back67 exporters: Vec<ExporterWithTarget>,68 time_unit: Option<Unit>,Extract common functionalityDavid Peterbefore adoption308bf5c
lines 69–73why, one step back69}7071impl ExportManager {72 /// Build the ExportManager that will export the results specified73 /// in the given ArgMatchesDo not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 74–78why, one step back74 pub fn from_cli_arguments(matches: &ArgMatches, time_unit: Option<Unit>) -> Result<Self> {75 let mut export_manager = Self {76 exporters: vec![],77 time_unit,78 };Extract common functionalityDavid Peterbefore adoption308bf5c
lines 79–80why, one step back79 {80 let mut add_exporter = |flag, exporttype| -> Result<()> {add clap deprecation noticesBenjamin Rombautbefore adoption5fcb716
line 81why, one step back81 if let Some(filename) = matches.get_one::<String>(flag) {Extract common functionalityDavid Peterbefore adoption308bf5c
lines 82–89why, one step back82 export_manager.add_exporter(exporttype, filename)?;83 }84 Ok(())85 };86 add_exporter("export-asciidoc", ExportType::Asciidoc)?;87 add_exporter("export-json", ExportType::Json)?;88 add_exporter("export-csv", ExportType::Csv)?;89 add_exporter("export-markdown", ExportType::Markdown)?;Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383
line 90why, one step back90 add_exporter("export-orgmode", ExportType::Orgmode)?;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 91–97why, one step back91 }92 Ok(export_manager)93 }9495 /// Add an additional exporter to the ExportManager96 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {97 let exporter: Box<dyn Exporter> = match export_type {Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 98–102why, one step back98 ExportType::Asciidoc => Box::<AsciidocExporter>::default(),99 ExportType::Csv => Box::<CsvExporter>::default(),100 ExportType::Json => Box::<JsonExporter>::default(),101 ExportType::Markdown => Box::<MarkdownExporter>::default(),102 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),Extract common functionalityDavid Peterbefore adoption308bf5c
line 103why, one step back103 };PR for issue #615Humble Penguinbefore adoptionbbe706b
line 104why, one step back104Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
line 105why, one step back105 self.exporters.push(ExporterWithTarget {Extract common functionalityDavid Peterbefore adoption308bf5c
line 106why, one step back106 exporter,Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 107–108why, one step back107 target: if filename == "-" {108 ExportTarget::StdoutPR for issue #615Humble Penguinbefore adoptionbbe706b
line 109why, one step back109 } else {Prevent creation of file named '-'David Peterbefore adoption5f5adda
lines 110–111why, one step back110 let _ = File::create(filename)111 .with_context(|| format!("Could not create export file '{}'", filename))?;Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
line 112why, one step back112 ExportTarget::File(filename.to_string())PR for issue #615Humble Penguinbefore adoptionbbe706b
line 113why, one step back113 },Extract common functionalityDavid Peterbefore adoption308bf5c
lines 114–118why, one step back114 });115116 Ok(())117 }118Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 119–124why, one step back119 /// Write the given results to all Exporters. The 'intermediate' flag specifies120 /// whether this is being called while still performing benchmarks, or if this121 /// is the final call after all benchmarks have been finished. In the former case,122 /// results are written to all file targets (to always have them up to date, even123 /// if a benchmark fails). In the latter case, we only print to stdout targets (in124 /// order not to clutter the output of hyperfine with intermediate results).Add new --sort optionDavid Peterbefore adoptionac617c8
lines 125–130why, one step back125 pub fn write_results(126 &self,127 results: &[BenchmarkResult],128 sort_order: SortOrder,129 intermediate: bool,130 ) -> Result<()> {Extract common functionalityDavid Peterbefore adoption308bf5c
line 131why, one step back131 for e in &self.exporters {Add new --sort optionDavid Peterbefore adoptionac617c8
line 132why, one step back132 let content = || e.exporter.serialize(results, self.time_unit, sort_order);Do not export intermediate results to stdout, closes #640David Peterbefore adoption2793d7b
lines 133–146why, one step back133134 match e.target {135 ExportTarget::File(ref filename) => {136 if intermediate {137 write_to_file(filename, &content()?)?138 }139 }140 ExportTarget::Stdout => {141 if !intermediate {142 println!();143 println!("{}", String::from_utf8(content()?).unwrap());144 }145 }146 }Extract common functionalityDavid Peterbefore adoption308bf5c
lines 147–157why, one step back147 }148 Ok(())149 }150}151152/// Write the given content to a file with the specified name153fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {154 let mut file = OpenOptions::new().write(true).open(filename)?;155 file.write_all(content)156 .with_context(|| format!("Failed to export results to '{}'", filename))157}