Skip to content
Myr

mod.rs

This file

Path

src/export/mod.rs

115 lines · 3.5 KiB · Rust

Last changed by

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

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–7why, one step back
4mod asciidoc;
5mod csv;6mod json;7mod markdown;

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

line 8why, one step back
8mod markup;

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

line 9why, one step back
9mod orgmode;

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 10–14why, one step back
10
11use 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 back
15use self::orgmode::OrgmodeExporter;

Flat directory/module structureDavid Peterbefore adoption30c381f

line 16why, one step back
16

Move functionality to schedulerDavid Peterbefore adoptiondcfeede

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

Move a few modules to utilDavid Peterbefore adoption5c634c9

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

Flat directory/module structureDavid Peterbefore adoption30c381f

line 19why, one step back
19

Unified error handlingDavid Peterbefore adoption2040810

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

Move build_export_manager to ExportManagerDavid Peterbefore adoption33f8d03

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

Unified error handlingDavid Peterbefore adoption2040810

line 22why, one step back
22

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 23–36why, one step back
23/// The desired form of exporter to use for a given file.
24#[derive(Clone)]25pub enum ExportType {26 /// Asciidoc Table27 Asciidoc,2829 /// CSV (comma separated values) format30 Csv,3132 /// JSON format33 Json,3435 /// Markdown table36 Markdown,

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

lines 37–39why, one step back
37
38 /// Emacs org-mode tables39 Orgmode,

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 40–53why, one step back
40}
4142/// Interface for different exporters.43trait Exporter {44 /// Export the given entries in the serialized form.45 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;46}4748struct ExporterWithFilename {49 exporter: Box<dyn Exporter>,50 filename: String,51}5253/// Handles the management of multiple file exporters.

Fix merge conflictDavid Peterbefore adoption8a4e791

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 55–58why, one step back
55pub struct ExportManager {
56 exporters: Vec<ExporterWithFilename>,57}58

Fix clippy warningsrhysdbefore adoptionf2bfedd

line 59why, one step back
59impl ExportManager {

Move build_export_manager to ExportManagerDavid Peterbefore adoption33f8d03

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

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

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

Move build_export_manager to ExportManagerDavid Peterbefore adoption33f8d03

lines 76–79why, one step back
76 }
77 Ok(export_manager)78 }79

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Unified error handlingDavid Peterbefore adoption2040810

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

Fix format callDavid Peterbefore adoptiond7e9692

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 84–89why, one step back
84
85 let exporter: Box<dyn Exporter> = match export_type {86 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),87 ExportType::Csv => Box::new(CsvExporter::default()),88 ExportType::Json => Box::new(JsonExporter::default()),89 ExportType::Markdown => Box::new(MarkdownExporter::default()),

Export of Emacs org-mode table supportPhilipp Paulweberbefore adoption2443383

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 91–106why, one step back
91 };
92 self.exporters.push(ExporterWithFilename {93 exporter,94 filename: filename.to_string(),95 });9697 Ok(())98 }99100 /// Write the given results to all Exporters contained within this manager101 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {102 for e in &self.exporters {103 let file_content = e.exporter.serialize(results, unit)?;104 write_to_file(&e.filename, &file_content)?;105 }106 Ok(())

Fix clippy warnings from testsHeikki Hellgrenbefore adoptionf592155

lines 107–109why, one step back
107 }
108}109

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Unified error handlingDavid Peterbefore adoption2040810

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

Flat directory/module structureDavid Peterbefore adoption30c381f

line 115why, one step back
115}