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
Provenance
- Commit
9b5dd60- Tree
0db30b9- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
4b8cc34
2 files changed+31 added−47 removed
modified:src/hyperfine/export/mod.rs+23 added-17 removed
59 unmodified lines60 /// The desired form of exporter to use for a given file.61 #[derive(Clone)]62 pub enum ExportType {63 /// CSV (comma separated values) format64- Csv(String),64+ Csv,65 66 /// JSON format67- Json(String),67+ Json,68 }69 70 /// Interface for different exporters.71 trait Exporter {72 /// Export the given entries in the serialized form.73 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;74 }75 76+struct ExporterWithFilename {77+ exporter: Box<Exporter>,78+ filename: String,79+}80+81 /// Handles the management of multiple file exporters.82 pub struct ExportManager {78- exporters: Vec<ExportType>,83+ exporters: Vec<ExporterWithFilename>,84 }85 86 impl ExportManager {87 /// Create a new ExportManager3 unmodified lines91 }92 }93 94 /// 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 }105 106 /// 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- };101-102- 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 }115 116 /// Write the given content to a file with the specified name110-fn write_to_file(filename: &str, content: String) -> Result<()> {117+fn 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 lines180 OutputStyleOption::Basic181 },182 };183 184- 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+ }191 192 // 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 lines206 let res = run(&commands, &options);207 208 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 }215-216-struct ExportTargetList<'a> {217- json_file: Option<&'a str>,218- csv_file: Option<&'a str>,219-}220-221-fn create_exporter(targets: ExportTargetList) -> Option<ExportManager> {222- if targets.json_file.is_none() && targets.csv_file.is_none() {223- return None;224- }225-226- let mut export_manager = ExportManager::new();227-228- if let Some(filename) = targets.json_file {229- export_manager.add_exporter(ExportType::Json(filename.to_string()));230- }231-232- if let Some(filename) = targets.csv_file {233- export_manager.add_exporter(ExportType::Csv(filename.to_string()));234- }235- Some(export_manager)236-}