You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
use std::fmt::Debug; |
|
|
|
use crate::utils::OneOrVec; |
|
use anyhow::Error; |
|
|
|
pub struct CombinedResult<T, E: Debug = Error> { |
|
ok: Vec<T>, |
|
err: Vec<E>, |
|
} |
|
|
|
impl<T, E: Debug> CombinedResult<T, E> { |
|
pub fn new() -> Self { |
|
Self { |
|
ok: vec![], |
|
err: vec![], |
|
} |
|
} |
|
|
|
pub fn ok(&mut self, result: impl OneOrVec<T>) { |
|
self.ok.extend(result.into_vec()); |
|
} |
|
|
|
pub fn err<I: Into<E>>(&mut self, err: impl OneOrVec<I>) { |
|
self.err.extend( |
|
err.into_vec() |
|
.into_iter() |
|
.map(Into::into) |
|
.collect::<Vec<_>>(), |
|
); |
|
} |
|
|
|
pub fn unwrap(self) -> Vec<T> { |
|
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<E> { |
|
self.err.drain(..).collect() |
|
} |
|
}
|
|
|