Skip to content
Myr

Use String instead of bytes, change error handling

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 bd81fe6 · Browse files · 3 files+18 added−15 removed
This commit

State

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

Diff stats

3 files+18 added−15 removed

  • other:+18 added−15 removed

3 files+18 added−15 removed

modified:src/hyperfine/export/csv.rs+6 added-5 removed
4 unmodified lines
56pub struct CsvExporter {}78impl Exporter for CsvExporter {9 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {9 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String> {10 let mut writer = WriterBuilder::new().from_writer(vec![]);11 for res in results {12 writer.serialize(res)?;13 }1415 if let Ok(inner) = writer.into_inner() {16 return Ok(inner);17 }18 Err(Error::new(ErrorKind::Other, "Error serializing to CSV"))15 writer16 .into_inner()17 .ok()18 .and_then(|bytes| String::from_utf8(bytes).ok())19 .ok_or(Error::new(ErrorKind::Other, "Error serializing to CSV"))20 }21}2223impl CsvExporter {
4 unmodified lines
modified:src/hyperfine/export/json.rs+9 added-7 removed
1use super::{ExportEntry, Exporter};23use std::io::{Error, ErrorKind, Result};45use serde_json::to_vec_pretty;5use serde_json::to_string_pretty;67#[derive(Serialize, Debug)]8struct HyperfineSummary<'a> {9 results: &'a Vec<ExportEntry>,
1 unmodified line
1112pub struct JsonExporter {}1314impl Exporter for JsonExporter {15 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {16 let serialized = to_vec_pretty(&HyperfineSummary { results });15 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String> {16 let serialized = to_string_pretty(&HyperfineSummary { results });1718 match serialized {19 Ok(file_content) => Ok(file_content),20 Err(e) => Err(Error::new(ErrorKind::Other, format!("{:?}", e))),21 }18 serialized.map_err(|e| {19 Error::new(20 ErrorKind::Other,21 format!("Error while serializing to JSON: {:}", e),22 )23 })24 }25}2627impl JsonExporter {
4 unmodified lines
modified:src/hyperfine/export/mod.rs+3 added-3 removed
68 unmodified lines
6970/// Interface for different exporters.71trait Exporter {72 /// Export the given entries in the serialized form.73 fn serialize(&self, values: &Vec<ExportEntry>) -> Result<Vec<u8>>;73 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;74}7576/// Handles the management of multiple file exporters.77pub struct ExportManager {
28 unmodified lines
106 }107}108109/// Write the given content to a file with the specified name110fn write_to_file(filename: &str, content: Vec<u8>) -> Result<()> {110fn write_to_file(filename: &str, content: String) -> Result<()> {111 let mut file = File::create(filename)?;112 file.write_all(&content)?;112 file.write_all(content.as_bytes())?;113 Ok(())114}