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

+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))
}