Skip to content
Myr

Refactored ExporterManager

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by David Peter · parent 4b8cc34 · Browse files · 2 files+31 added−47 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+31 added−47 removed

  • other:+31 added−47 removed

2 files+31 added−47 removed

modified:src/hyperfine/export/mod.rs+23 added-17 removed
59 unmodified lines
60/// The desired form of exporter to use for a given file.61#[derive(Clone)]62pub enum ExportType {63 /// CSV (comma separated values) format64 Csv(String),64 Csv,6566 /// JSON format67 Json(String),67 Json,68}6970/// Interface for different exporters.71trait Exporter {72 /// Export the given entries in the serialized form.73 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;74}7576struct ExporterWithFilename {77 exporter: Box<Exporter>,78 filename: String,79}8081/// Handles the management of multiple file exporters.82pub struct ExportManager {78 exporters: Vec<ExportType>,83 exporters: Vec<ExporterWithFilename>,84}8586impl ExportManager {87 /// Create a new ExportManager
3 unmodified lines
91 }92 }9394 /// Add an additional exporter to the ExportManager90 pub fn add_exporter(&mut self, for_type: ExportType) {91 self.exporters.push(for_type);95 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) {96 let exporter: Box<Exporter> = match export_type {97 ExportType::Csv => Box::new(CsvExporter::new()),98 ExportType::Json => Box::new(JsonExporter::new()),99 };100 self.exporters.push(ExporterWithFilename {101 exporter,102 filename: filename.to_string(),103 });104 }105106 /// Write the given results to all Exporters contained within this manager95 pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> {96 for exp in &self.exporters {97 let (exporter, filename): (Box<Exporter>, &str) = match exp {98 &ExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file),99 &ExportType::Json(ref file) => (Box::from(JsonExporter::new()), file),100 };101102 let file_content = exporter.serialize(&to_write)?;103 write_to_file(filename, file_content)?;107 pub fn write_results(&self, results: Vec<ExportEntry>) -> Result<()> {108 for e in &self.exporters {109 let file_content = e.exporter.serialize(&results)?;110 write_to_file(&e.filename, file_content)?;111 }112 Ok(())113 }114}115116/// Write the given content to a file with the specified name110fn write_to_file(filename: &str, content: String) -> Result<()> {117fn write_to_file(filename: &String, content: String) -> Result<()> {118 let mut file = File::create(filename)?;112 file.write_all(content.as_bytes())?;113 Ok(())119 file.write_all(content.as_bytes())120}
modified:src/main.rs+8 added-30 removed
179 unmodified lines
180 OutputStyleOption::Basic181 },182 };183184 let export_targets = ExportTargetList {185 json_file: matches.value_of("export-json"),186 csv_file: matches.value_of("export-csv"),187 };188 let export_manager = create_exporter(export_targets);184 let mut export_manager = ExportManager::new();185 if let Some(filename) = matches.value_of("export-json") {186 export_manager.add_exporter(ExportType::Json, filename);187 }188 if let Some(filename) = matches.value_of("export-csv") {189 export_manager.add_exporter(ExportType::Csv, filename);190 }191192 // We default Windows to NoColor if full had been specified.193 if cfg!(windows) && options.output_style == OutputStyleOption::Full {194 options.output_style = OutputStyleOption::NoColor;
11 unmodified lines
206 let res = run(&commands, &options);207208 match res {209 Ok(timing_results) => {208 if let Some(mut exporter) = export_manager {209 exporter.write_results(timing_results).unwrap();210 export_manager.write_results(timing_results).unwrap();210 }211 }212 Err(e) => error(e.description()),213 }214}215216struct ExportTargetList<'a> {217 json_file: Option<&'a str>,218 csv_file: Option<&'a str>,219}220221fn create_exporter(targets: ExportTargetList) -> Option<ExportManager> {222 if targets.json_file.is_none() && targets.csv_file.is_none() {223 return None;224 }225226 let mut export_manager = ExportManager::new();227228 if let Some(filename) = targets.json_file {229 export_manager.add_exporter(ExportType::Json(filename.to_string()));230 }231232 if let Some(filename) = targets.csv_file {233 export_manager.add_exporter(ExportType::Csv(filename.to_string()));234 }235 Some(export_manager)236}