docbox_core/utils/
error.rs

1use std::fmt;
2
3/// Error type that contains multiple errors
4#[derive(Debug)]
5pub struct CompositeError(Vec<anyhow::Error>);
6
7impl fmt::Display for CompositeError {
8    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9        let messages = self
10            .0
11            .iter()
12            .map(|e| format!("{e}"))
13            .collect::<Vec<_>>()
14            .join(", ");
15        write!(f, "multiple errors occurred: {messages}")
16    }
17}
18
19impl std::error::Error for CompositeError {}
20
21impl Extend<anyhow::Error> for CompositeError {
22    fn extend<T: IntoIterator<Item = anyhow::Error>>(&mut self, iter: T) {
23        self.0.extend(iter);
24    }
25}
26
27impl FromIterator<anyhow::Error> for CompositeError {
28    fn from_iter<T: IntoIterator<Item = anyhow::Error>>(iter: T) -> Self {
29        CompositeError(iter.into_iter().collect())
30    }
31}