use core::fmt;
use core::ops::{Deref, DerefMut};
use std::error::Error;
#[repr(transparent)]
pub struct AggregateError<E, const N: usize> {
inner: [E; N],
}
impl<E, const N: usize> AggregateError<E, N> {
pub(super) fn new(inner: [E; N]) -> Self {
Self { inner }
}
}
impl<E: Error, const N: usize> fmt::Debug for AggregateError<E, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{self}:")?;
for (i, err) in self.inner.iter().enumerate() {
writeln!(f, "- Error {}: {err}", i + 1)?;
let mut source = err.source();
while let Some(err) = source {
writeln!(f, " ↳ Caused by: {err}")?;
source = err.source();
}
}
Ok(())
}
}
impl<E: Error, const N: usize> fmt::Display for AggregateError<E, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} errors occured", self.inner.len())
}
}
impl<E, const N: usize> Deref for AggregateError<E, N> {
type Target = [E; N];
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<E, const N: usize> DerefMut for AggregateError<E, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<E: Error, const N: usize> std::error::Error for AggregateError<E, N> {}