mod.rs
This file
Path
src/export/mod.rs
123 lines · 3.8 KiB · Rust
Last changed by
Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
History of this fileTake it
The walk stopped after 200 commits: lines older than b658e20 point at it.
Move exit code function to separate fileDavid Peterbefore adoptionb658e20
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;Move exit code function to separate fileDavid Peterbefore adoptionb658e20
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;Move exit code function to separate fileDavid Peterbefore adoptionb658e20
line 16why, one step back16Move functionality to schedulerDavid Peterbefore adoptiondcfeede
line 17why, one step back17use crate::benchmark::benchmark_result::BenchmarkResult;Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 18–36why, one step back18use crate::util::units::Unit;1920use anyhow::{Context, Result};21use clap::ArgMatches;2223/// 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 back3738 /// Emacs org-mode tables39 Orgmode,Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 40–65why, one step back40}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.54#[derive(Default)]55pub struct ExportManager {56 exporters: Vec<ExporterWithFilename>,57}5859impl ExportManager {60 /// Build the ExportManager that will export the results specified61 /// 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<()> {add clap deprecation noticesBenjamin Rombautbefore adoption5fcb716
line 66why, one step back66 if let Some(filename) = matches.get_one::<String>(flag) {Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 67–74why, one step back67 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 back75 add_exporter("export-orgmode", ExportType::Orgmode)?;Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 76–82why, one step back76 }77 Ok(export_manager)78 }7980 /// Add an additional exporter to the ExportManager81 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {82 let exporter: Box<dyn Exporter> = match export_type {Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 83–87why, one step back83 ExportType::Asciidoc => Box::<AsciidocExporter>::default(),84 ExportType::Csv => Box::<CsvExporter>::default(),85 ExportType::Json => Box::<JsonExporter>::default(),86 ExportType::Markdown => Box::<MarkdownExporter>::default(),87 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),Move exit code function to separate fileDavid Peterbefore adoptionb658e20
line 88why, one step back88 };PR for issue #615Humble Penguinbefore adoptionbbe706b
line 89why, one step back89Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 90–91why, one step back90 self.exporters.push(ExporterWithFilename {91 exporter,ran cargo fmtHumble Penguinbefore adoptionacf156e
line 92why, one step back92 filename: if filename == "-" {used cfg!(windows) instead of env::consts::OSHumble Penguinbefore adoption9d0f678
line 93why, one step back93 if cfg!(windows) {PR for issue #615Humble Penguinbefore adoptionbbe706b
lines 94–98why, one step back94 "con:".to_string()95 } else {96 "/dev/stdout".to_string()97 }98 } else {Prevent creation of file named '-'David Peterbefore adoption5f5adda
lines 99–100why, one step back99 let _ = File::create(filename)100 .with_context(|| format!("Could not create export file '{}'", filename))?;PR for issue #615Humble Penguinbefore adoptionbbe706b
lines 101–102why, one step back101 filename.to_string()102 },Move exit code function to separate fileDavid Peterbefore adoptionb658e20
lines 103–123why, one step back103 });104105 Ok(())106 }107108 /// Write the given results to all Exporters contained within this manager109 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {110 for e in &self.exporters {111 let file_content = e.exporter.serialize(results, unit)?;112 write_to_file(&e.filename, &file_content)?;113 }114 Ok(())115 }116}117118/// Write the given content to a file with the specified name119fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {120 let mut file = OpenOptions::new().write(true).open(filename)?;121 file.write_all(content)122 .with_context(|| format!("Failed to export results to '{}'", filename))123}