use std::fmt::Debug; use crate::utils::OneOrVec; use anyhow::Error; pub struct CombinedResult { ok: Vec, err: Vec, } impl CombinedResult { pub fn new() -> Self { Self { ok: vec![], err: vec![], } } pub fn ok(&mut self, result: impl OneOrVec) { self.ok.extend(result.into_vec()); } pub fn err>(&mut self, err: impl OneOrVec) { self.err.extend( err.into_vec() .into_iter() .map(Into::into) .collect::>(), ); } pub fn unwrap(self) -> Vec { let err_len = self.err.len(); if err_len > 0 { panic!("CombinedResult has errors: {:?}", self.err); } self.ok } pub fn has_err(&self) -> bool { !self.err.is_empty() } pub fn unwrap_one(self) -> T { self.unwrap().pop().unwrap() } pub fn pop_errors(&mut self) -> Vec { self.err.drain(..).collect() } }