-
-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to export JSON, change file handling
No longer serialize into strings, instead use Vec<u8> as it is what we're writing to the files. Fixup comments
- Loading branch information
1 parent
ce79d81
commit c59110d
Showing
6 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
use super::{ExportEntry, ResultExporter}; | ||
|
||
use csv::WriterBuilder; | ||
use std::io::Result; | ||
use std::io::{Error, ErrorKind, Result}; | ||
|
||
pub struct CsvExporter { | ||
out_file: String, | ||
} | ||
pub struct CsvExporter {} | ||
|
||
impl ResultExporter for CsvExporter { | ||
fn write(&self, results: &Vec<ExportEntry>) -> Result<()> { | ||
let mut writer = WriterBuilder::new().from_path(&self.out_file)?; | ||
fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> { | ||
let mut writer = WriterBuilder::new().from_writer(vec![]); | ||
for res in results { | ||
writer.serialize(res)?; | ||
} | ||
Ok(()) | ||
|
||
if let Ok(inner) = writer.into_inner() { | ||
return Ok(inner); | ||
} | ||
Err(Error::new(ErrorKind::Other, "Error serializing to CSV")) | ||
} | ||
} | ||
|
||
impl CsvExporter { | ||
pub fn new(file_name: String) -> Self { | ||
CsvExporter { | ||
out_file: file_name, | ||
} | ||
pub fn new() -> Self { | ||
CsvExporter {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use super::{ExportEntry, ResultExporter}; | ||
|
||
use std::io::{Error, ErrorKind, Result}; | ||
|
||
use serde_json::to_vec_pretty; | ||
|
||
#[derive(Serialize, Debug)] | ||
struct HyperfineSummary<'a> { | ||
results: &'a Vec<ExportEntry>, | ||
} | ||
|
||
pub struct JsonExporter {} | ||
|
||
impl ResultExporter for JsonExporter { | ||
fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> { | ||
let serialized = to_vec_pretty(&HyperfineSummary { results }); | ||
|
||
match serialized { | ||
Ok(file_content) => Ok(file_content), | ||
Err(e) => Err(Error::new(ErrorKind::Other, format!("{:?}", e))), | ||
} | ||
} | ||
} | ||
|
||
impl JsonExporter { | ||
pub fn new() -> Self { | ||
JsonExporter {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters