Skip to content
Myr

mod.rs

This file

Path

src/export/mod.rs

107 lines · 3.3 KiB · Rust

Last changed by

Move functionality to schedulerDavid Peterbefore adoptiondcfeede

History of this file

Take it

Re-order importsDavid Peterbefore adoption9df6174

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

Unified error handlingDavid Peterbefore adoption2040810

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

Re-order importsDavid Peterbefore adoption9df6174

line 3why, one step back
3

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 4–13why, one step back
4mod asciidoc;
5mod csv;6mod json;7mod markdown;89use self::asciidoc::AsciidocExporter;10use self::csv::CsvExporter;11use self::json::JsonExporter;12use self::markdown::MarkdownExporter;13

Move functionality to schedulerDavid Peterbefore adoptiondcfeede

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

Move a few modules to utilDavid Peterbefore adoption5c634c9

line 15why, one step back
15use crate::util::units::Unit;

Flat directory/module structureDavid Peterbefore adoption30c381f

line 16why, one step back
16

Unified error handlingDavid Peterbefore adoption2040810

line 17why, one step back
17use anyhow::{Context, Result};

Move build_export_manager to ExportManagerDavid Peterbefore adoption33f8d03

line 18why, one step back
18use clap::ArgMatches;

Unified error handlingDavid Peterbefore adoption2040810

line 19why, one step back
19

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 20–47why, one step back
20/// The desired form of exporter to use for a given file.
21#[derive(Clone)]22pub enum ExportType {23 /// Asciidoc Table24 Asciidoc,2526 /// CSV (comma separated values) format27 Csv,2829 /// JSON format30 Json,3132 /// Markdown table33 Markdown,34}3536/// Interface for different exporters.37trait Exporter {38 /// Export the given entries in the serialized form.39 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;40}4142struct ExporterWithFilename {43 exporter: Box<dyn Exporter>,44 filename: String,45}4647/// Handles the management of multiple file exporters.

Fix merge conflictDavid Peterbefore adoption8a4e791

line 48why, one step back
48#[derive(Default)]

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 49–52why, one step back
49pub struct ExportManager {
50 exporters: Vec<ExporterWithFilename>,51}52

Fix clippy warningsrhysdbefore adoptionf2bfedd

line 53why, one step back
53impl ExportManager {

Move build_export_manager to ExportManagerDavid Peterbefore adoption33f8d03

lines 54–72why, one step back
54 /// Build the ExportManager that will export the results specified
55 /// in the given ArgMatches56 pub fn from_cli_arguments(matches: &ArgMatches) -> Result<Self> {57 let mut export_manager = Self::default();58 {59 let mut add_exporter = |flag, exporttype| -> Result<()> {60 if let Some(filename) = matches.value_of(flag) {61 export_manager.add_exporter(exporttype, filename)?;62 }63 Ok(())64 };65 add_exporter("export-asciidoc", ExportType::Asciidoc)?;66 add_exporter("export-json", ExportType::Json)?;67 add_exporter("export-csv", ExportType::Csv)?;68 add_exporter("export-markdown", ExportType::Markdown)?;69 }70 Ok(export_manager)71 }72

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 73–74why, one step back
73 /// Add an additional exporter to the ExportManager
74 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {

Unified error handlingDavid Peterbefore adoption2040810

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

Fix format callDavid Peterbefore adoptiond7e9692

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 77–98why, one step back
77
78 let exporter: Box<dyn Exporter> = match export_type {79 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),80 ExportType::Csv => Box::new(CsvExporter::default()),81 ExportType::Json => Box::new(JsonExporter::default()),82 ExportType::Markdown => Box::new(MarkdownExporter::default()),83 };84 self.exporters.push(ExporterWithFilename {85 exporter,86 filename: filename.to_string(),87 });8889 Ok(())90 }9192 /// Write the given results to all Exporters contained within this manager93 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {94 for e in &self.exporters {95 let file_content = e.exporter.serialize(results, unit)?;96 write_to_file(&e.filename, &file_content)?;97 }98 Ok(())

Fix clippy warnings from testsHeikki Hellgrenbefore adoptionf592155

lines 99–101why, one step back
99 }
100}101

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 102–105why, one step back
102/// Write the given content to a file with the specified name
103fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {104 let mut file = OpenOptions::new().write(true).open(filename)?;105 file.write_all(content)

Unified error handlingDavid Peterbefore adoption2040810

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

Flat directory/module structureDavid Peterbefore adoption30c381f

line 107why, one step back
107}