pub use models::{Error, ErrorKind, OptionExt, ResultExt}; #[derive(Debug, Default)] pub struct ErrorCollection(Vec); impl ErrorCollection { pub fn new() -> Self { Self::default() } pub fn handle>(&mut self, result: Result) -> Option { match result { Ok(a) => Some(a), Err(e) => { self.0.push(e.into()); None } } } pub fn into_result(mut self) -> Result<(), Error> { if self.0.len() <= 1 { if let Some(err) = self.0.pop() { Err(err) } else { Ok(()) } } else { Err(Error::new(self, ErrorKind::MultipleErrors)) } } } impl From for Result<(), Error> { fn from(e: ErrorCollection) -> Self { e.into_result() } } impl> Extend> for ErrorCollection { fn extend>>(&mut self, iter: I) { for item in iter { self.handle(item); } } } impl std::fmt::Display for ErrorCollection { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (idx, e) in self.0.iter().enumerate() { if idx > 0 { write!(f, "; ")?; } write!(f, "{}", e)?; } Ok(()) } } impl std::error::Error for ErrorCollection {} #[macro_export] macro_rules! ensure_code { ($x:expr, $c:expr, $fmt:expr $(, $arg:expr)*) => { if !($x) { Err::<(), _>(crate::error::Error::new(color_eyre::eyre::eyre!($fmt, $($arg, )*), $c))?; } }; }