mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Refactored ExporterManager
This commit is contained in:
1 parent
4b8cc34815
commit
9b5dd60a92
2 files changed
+31
-47
No files matched your search
+23
-17
@@ -61,10 +61,10 @@ impl ExportEntry {
|
||||
#[derive(Clone)]
|
||||
pub enum ExportType {
|
||||
/// CSV (comma separated values) format
|
||||
Csv(String),
|
||||
Csv,
|
||||
|
||||
/// JSON format
|
||||
Json(String),
|
||||
Json,
|
||||
}
|
||||
|
||||
/// Interface for different exporters.
|
||||
@@ -73,9 +73,14 @@ trait Exporter {
|
||||
fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;
|
||||
}
|
||||
|
||||
struct ExporterWithFilename {
|
||||
exporter: Box<Exporter>,
|
||||
filename: String,
|
||||
}
|
||||
|
||||
/// Handles the management of multiple file exporters.
|
||||
pub struct ExportManager {
|
||||
exporters: Vec<ExportType>,
|
||||
exporters: Vec<ExporterWithFilename>,
|
||||
}
|
||||
|
||||
impl ExportManager {
|
||||
@@ -87,28 +92,29 @@ impl ExportManager {
|
||||
}
|
||||
|
||||
/// Add an additional exporter to the ExportManager
|
||||
pub fn add_exporter(&mut self, for_type: ExportType) {
|
||||
self.exporters.push(for_type);
|
||||
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) {
|
||||
let exporter: Box<Exporter> = match export_type {
|
||||
ExportType::Csv => Box::new(CsvExporter::new()),
|
||||
ExportType::Json => Box::new(JsonExporter::new()),
|
||||
};
|
||||
self.exporters.push(ExporterWithFilename {
|
||||
exporter,
|
||||
filename: filename.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
/// Write the given results to all Exporters contained within this manager
|
||||
pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> {
|
||||
for exp in &self.exporters {
|
||||
let (exporter, filename): (Box<Exporter>, &str) = match exp {
|
||||
&ExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file),
|
||||
&ExportType::Json(ref file) => (Box::from(JsonExporter::new()), file),
|
||||
};
|
||||
|
||||
let file_content = exporter.serialize(&to_write)?;
|
||||
write_to_file(filename, file_content)?;
|
||||
pub fn write_results(&self, results: Vec<ExportEntry>) -> Result<()> {
|
||||
for e in &self.exporters {
|
||||
let file_content = e.exporter.serialize(&results)?;
|
||||
write_to_file(&e.filename, file_content)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the given content to a file with the specified name
|
||||
fn write_to_file(filename: &str, content: String) -> Result<()> {
|
||||
fn write_to_file(filename: &String, content: String) -> Result<()> {
|
||||
let mut file = File::create(filename)?;
|
||||
file.write_all(content.as_bytes())?;
|
||||
Ok(())
|
||||
file.write_all(content.as_bytes())
|
||||
}
|
||||
+8
-30
@@ -181,11 +181,13 @@ fn main() {
|
||||
},
|
||||
};
|
||||
|
||||
let export_targets = ExportTargetList {
|
||||
json_file: matches.value_of("export-json"),
|
||||
csv_file: matches.value_of("export-csv"),
|
||||
};
|
||||
let export_manager = create_exporter(export_targets);
|
||||
let mut export_manager = ExportManager::new();
|
||||
if let Some(filename) = matches.value_of("export-json") {
|
||||
export_manager.add_exporter(ExportType::Json, filename);
|
||||
}
|
||||
if let Some(filename) = matches.value_of("export-csv") {
|
||||
export_manager.add_exporter(ExportType::Csv, filename);
|
||||
}
|
||||
|
||||
// We default Windows to NoColor if full had been specified.
|
||||
if cfg!(windows) && options.output_style == OutputStyleOption::Full {
|
||||
@@ -205,32 +207,8 @@ fn main() {
|
||||
|
||||
match res {
|
||||
Ok(timing_results) => {
|
||||
if let Some(mut exporter) = export_manager {
|
||||
exporter.write_results(timing_results).unwrap();
|
||||
}
|
||||
export_manager.write_results(timing_results).unwrap();
|
||||
}
|
||||
Err(e) => error(e.description()),
|
||||
}
|
||||
}
|
||||
|
||||
struct ExportTargetList<'a> {
|
||||
json_file: Option<&'a str>,
|
||||
csv_file: Option<&'a str>,
|
||||
}
|
||||
|
||||
fn create_exporter(targets: ExportTargetList) -> Option<ExportManager> {
|
||||
if targets.json_file.is_none() && targets.csv_file.is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut export_manager = ExportManager::new();
|
||||
|
||||
if let Some(filename) = targets.json_file {
|
||||
export_manager.add_exporter(ExportType::Json(filename.to_string()));
|
||||
}
|
||||
|
||||
if let Some(filename) = targets.csv_file {
|
||||
export_manager.add_exporter(ExportType::Csv(filename.to_string()));
|
||||
}
|
||||
Some(export_manager)
|
||||
}
|
||||
Reference in new issue
Block a user