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
Provenance
- Commit
4b8cc34- Tree
42a6852- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
bd81fe6
3 files changed+18 added−15 removed
modified:src/hyperfine/export/csv.rs+6 added-5 removed
4 unmodified lines5 6 pub struct CsvExporter {}7 8 impl 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 }14 15- 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 }22 23 impl CsvExporter {4 unmodified linesmodified:src/hyperfine/export/json.rs+9 added-7 removed
1 use super::{ExportEntry, Exporter};2 3 use std::io::{Error, ErrorKind, Result};4 5-use serde_json::to_vec_pretty;5+use serde_json::to_string_pretty;6 7 #[derive(Serialize, Debug)]8 struct HyperfineSummary<'a> {9 results: &'a Vec<ExportEntry>,1 unmodified line11 12 pub struct JsonExporter {}13 14 impl 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 });17 18- 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 }26 27 impl JsonExporter {4 unmodified linesmodified:src/hyperfine/export/mod.rs+3 added-3 removed
68 unmodified lines69 70 /// Interface for different exporters.71 trait 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 }75 76 /// Handles the management of multiple file exporters.77 pub struct ExportManager {28 unmodified lines106 }107 }108 109 /// Write the given content to a file with the specified name110-fn write_to_file(filename: &str, content: Vec<u8>) -> Result<()> {110+fn 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 }