Skip to content
Myr

mod.rs

This file

Path

src/export/mod.rs

125 lines · 3.8 KiB · Rust

Last changed by

ran cargo fmtHumble Penguinbefore adoptionacf156e

History of this file

Take it

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

PR for issue #615Humble Penguinbefore adoptionbbe706b

line 1why, one step back
1use std::env;

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 2–8why, one step back
2use std::fs::{File, OpenOptions};
3use std::io::Write;45mod asciidoc;6mod csv;7mod json;8mod markdown;

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

line 9why, one step back
9mod markup;

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

line 10why, one step back
10mod orgmode;

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 11–15why, one step back
11
12use self::asciidoc::AsciidocExporter;13use self::csv::CsvExporter;14use self::json::JsonExporter;15use self::markdown::MarkdownExporter;

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

line 16why, one step back
16use self::orgmode::OrgmodeExporter;

Turn show_output into enumDavid Peterbefore adoption0ebd76d

line 17why, one step back
17

Move functionality to schedulerDavid Peterbefore adoptiondcfeede

line 18why, one step back
18use crate::benchmark::benchmark_result::BenchmarkResult;

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 19–37why, one step back
19use 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 back
38
39 /// Emacs org-mode tables40 Orgmode,

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 41–66why, one step back
41}
4243/// Interface for different exporters.44trait Exporter {45 /// Export the given entries in the serialized form.46 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;47}4849struct ExporterWithFilename {50 exporter: Box<dyn Exporter>,51 filename: String,52}5354/// Handles the management of multiple file exporters.55#[derive(Default)]56pub struct ExportManager {57 exporters: Vec<ExporterWithFilename>,58}5960impl ExportManager {61 /// Build the ExportManager that will export the results specified62 /// in the given ArgMatches63 pub fn from_cli_arguments(matches: &ArgMatches) -> Result<Self> {64 let mut export_manager = Self::default();65 {66 let mut add_exporter = |flag, exporttype| -> Result<()> {

add clap deprecation noticesBenjamin Rombautbefore adoption5fcb716

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

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 68–75why, one step back
68 export_manager.add_exporter(exporttype, filename)?;
69 }70 Ok(())71 };72 add_exporter("export-asciidoc", ExportType::Asciidoc)?;73 add_exporter("export-json", ExportType::Json)?;74 add_exporter("export-csv", ExportType::Csv)?;75 add_exporter("export-markdown", ExportType::Markdown)?;

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

line 76why, one step back
76 add_exporter("export-orgmode", ExportType::Orgmode)?;

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 77–90why, one step back
77 }
78 Ok(export_manager)79 }8081 /// Add an additional exporter to the ExportManager82 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {83 let _ = File::create(filename)84 .with_context(|| format!("Could not create export file '{}'", filename))?;8586 let exporter: Box<dyn Exporter> = match export_type {87 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),88 ExportType::Csv => Box::new(CsvExporter::default()),89 ExportType::Json => Box::new(JsonExporter::default()),90 ExportType::Markdown => Box::new(MarkdownExporter::default()),

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

line 91why, one step back
91 ExportType::Orgmode => Box::new(OrgmodeExporter::default()),

Turn show_output into enumDavid Peterbefore adoption0ebd76d

line 92why, one step back
92 };

PR for issue #615Humble Penguinbefore adoptionbbe706b

line 93why, one step back
93

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 94–95why, one step back
94 self.exporters.push(ExporterWithFilename {
95 exporter,

ran cargo fmtHumble Penguinbefore adoptionacf156e

line 96why, one step back
96 filename: if filename == "-" {

PR for issue #615Humble Penguinbefore adoptionbbe706b

lines 97–104why, one step back
97 if env::consts::OS == "windows" {
98 "con:".to_string()99 } else {100 "/dev/stdout".to_string()101 }102 } else {103 filename.to_string()104 },

Turn show_output into enumDavid Peterbefore adoption0ebd76d

lines 105–125why, one step back
105 });
106107 Ok(())108 }109110 /// Write the given results to all Exporters contained within this manager111 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {112 for e in &self.exporters {113 let file_content = e.exporter.serialize(results, unit)?;114 write_to_file(&e.filename, &file_content)?;115 }116 Ok(())117 }118}119120/// Write the given content to a file with the specified name121fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {122 let mut file = OpenOptions::new().write(true).open(filename)?;123 file.write_all(content)124 .with_context(|| format!("Failed to export results to '{}'", filename))125}