Better error handling (fixes #15)

This commit is contained in:
Roey Darwish Dror
2018-06-06 15:32:38 +03:00
parent 3897b2ac76
commit ef69dc01ba
4 changed files with 254 additions and 142 deletions

View File

@@ -1,6 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::process::ExitStatus;
pub type Report = HashMap<String, bool>;
@@ -8,9 +7,30 @@ pub trait Reporter {
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report);
}
impl Reporter for ExitStatus {
impl<T, E> Reporter for Result<T, E>
where
T: Reporter,
{
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
report.insert(key.into().into_owned(), self.success());
match self {
Err(_) => {
report.insert(key.into().into_owned(), false);
}
Ok(item) => {
item.report(key, report);
}
}
}
}
impl<T> Reporter for Option<T>
where
T: Reporter,
{
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
if let Some(item) = self {
item.report(key, report);
}
}
}
@@ -19,3 +39,9 @@ impl Reporter for bool {
report.insert(key.into().into_owned(), *self);
}
}
impl Reporter for () {
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
report.insert(key.into().into_owned(), true);
}
}