mod.rs
This file
Path
src/export/mod.rs
115 lines · 3.5 KiB · Rust
Last changed by
add clap deprecation noticesBenjamin Rombautbefore adoption5fcb716
History of this fileTake it
1use std::fs::{File, OpenOptions};2use std::io::Write;34mod asciidoc;5mod csv;6mod json;7mod markdown;8mod markup;9mod orgmode;1011use self::asciidoc::AsciidocExporter;12use self::csv::CsvExporter;13use self::json::JsonExporter;14use self::markdown::MarkdownExporter;15use self::orgmode::OrgmodeExporter;1617use crate::benchmark::benchmark_result::BenchmarkResult;18use 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,3738 /// Emacs org-mode tables39 Orgmode,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.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<()> {66 if let Some(filename) = matches.get_one::<String>(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)?;75 add_exporter("export-orgmode", ExportType::Orgmode)?;76 }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 _ = File::create(filename)83 .with_context(|| format!("Could not create export file '{}'", filename))?;8485 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()),90 ExportType::Orgmode => Box::new(OrgmodeExporter::default()),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(())107 }108}109110/// Write the given content to a file with the specified name111fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {112 let mut file = OpenOptions::new().write(true).open(filename)?;113 file.write_all(content)114 .with_context(|| format!("Failed to export results to '{}'", filename))115}