Unified error handling

This commit is contained in:
David Peter authored and David Peter committed 2022-02-06 20:19:50 +01:00
1 parent 34dabcbed7
commit 2040810ce3
10 files changed
+95 -101

No files matched your search

+4 -5
View File
@@ -1,5 +1,3 @@
use std::io::{Error, ErrorKind, Result};
use crate::benchmark_result::BenchmarkResult;
use crate::format::format_duration_value;
use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
@@ -7,6 +5,8 @@ use crate::units::Unit;
use super::Exporter;
use anyhow::{anyhow, Result};
#[derive(Default)]
pub struct AsciidocExporter {}
@@ -36,9 +36,8 @@ impl Exporter for AsciidocExporter {
Ok(res)
} else {
Err(Error::new(
ErrorKind::Other,
"Relative speed comparison is not available for Asciidoctor export.",
Err(anyhow!(
"Relative speed comparison is not available for Asciidoctor export."
))
}
}
+3 -4
View File
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::io::{Error, ErrorKind, Result};
use csv::WriterBuilder;
@@ -7,6 +6,8 @@ use super::Exporter;
use crate::benchmark_result::BenchmarkResult;
use crate::units::Unit;
use anyhow::Result;
#[derive(Default)]
pub struct CsvExporter {}
@@ -49,9 +50,7 @@ impl Exporter for CsvExporter {
writer.write_record(fields)?;
}
writer
.into_inner()
.map_err(|e| Error::new(ErrorKind::Other, e))
Ok(writer.into_inner()?)
}
}
+3 -3
View File
@@ -1,5 +1,3 @@
use std::io::{Error, ErrorKind, Result};
use serde::*;
use serde_json::to_vec_pretty;
@@ -7,6 +5,8 @@ use super::Exporter;
use crate::benchmark_result::BenchmarkResult;
use crate::units::Unit;
use anyhow::Result;
#[derive(Serialize, Debug)]
struct HyperfineSummary<'a> {
results: &'a [BenchmarkResult],
@@ -22,6 +22,6 @@ impl Exporter for JsonExporter {
content.push(b'\n');
}
output.map_err(|e| Error::new(ErrorKind::Other, e))
Ok(output?)
}
}
+4 -5
View File
@@ -1,11 +1,11 @@
use std::io::{Error, ErrorKind, Result};
use super::Exporter;
use crate::benchmark_result::BenchmarkResult;
use crate::format::format_duration_value;
use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
use crate::units::Unit;
use anyhow::{anyhow, Result};
#[derive(Default)]
pub struct MarkdownExporter {}
@@ -31,9 +31,8 @@ impl Exporter for MarkdownExporter {
Ok(destination)
} else {
Err(Error::new(
ErrorKind::Other,
"Relative speed comparison is not available for Markdown export.",
Err(anyhow!(
"Relative speed comparison is not available for Markdown export."
))
}
}
+6 -2
View File
@@ -1,5 +1,5 @@
use std::fs::{File, OpenOptions};
use std::io::{Result, Write};
use std::io::Write;
mod asciidoc;
mod csv;
@@ -14,6 +14,8 @@ use self::markdown::MarkdownExporter;
use crate::benchmark_result::BenchmarkResult;
use crate::units::Unit;
use anyhow::{Context, Result};
/// The desired form of exporter to use for a given file.
#[derive(Clone)]
pub enum ExportType {
@@ -50,7 +52,8 @@ pub struct ExportManager {
impl ExportManager {
/// Add an additional exporter to the ExportManager
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
let _ = File::create(filename)?;
let _ = File::create(filename)
.with_context(|| format!("Could not create export file '{filename}'"))?;
let exporter: Box<dyn Exporter> = match export_type {
ExportType::Asciidoc => Box::new(AsciidocExporter::default()),
@@ -80,4 +83,5 @@ impl ExportManager {
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = OpenOptions::new().write(true).open(filename)?;
file.write_all(content)
.with_context(|| format!("Failed to export results to '{}'", filename))
}