Uber refactor

This commit is contained in:
Roey Darwish Dror
2018-08-19 14:45:23 +03:00
parent 8ae5065167
commit 473f361fca
12 changed files with 434 additions and 340 deletions

View File

@@ -3,45 +3,11 @@ use std::borrow::Cow;
type CowString<'a> = Cow<'a, str>;
pub type Report<'a> = Vec<(CowString<'a>, bool)>;
pub trait Reporter {
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>);
}
pub fn report<'a, M: Into<CowString<'a>>>(report: &mut Report<'a>, result: Option<(M, bool)>) {
if let Some((key, success)) = result {
let key = key.into();
impl<T, E> Reporter for Result<T, E>
where
T: Reporter,
{
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
match self {
Err(_) => {
report.push((key.into(), false));
}
Ok(item) => {
item.report(key, report);
}
}
}
}
impl<T> Reporter for Option<T>
where
T: Reporter,
{
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
if let Some(item) = self {
item.report(key, report);
}
}
}
impl Reporter for bool {
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
report.push((key.into(), *self));
}
}
impl Reporter for () {
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
report.push((key.into(), true));
debug_assert!(!report.iter().any(|(k, _)| k == &key), "{} already reported", key);
report.push((key, success));
}
}