1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::error::Error;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Deref;

/// Type erased error returned by systems.
pub type SystemError = anyhow::Error;
/// Result returned by `Dispatcher::run_seq` and `Dispatcher::run_par`.
pub type RunResult = Result<(), RunError>;
/// Result returned by systems.
pub type SystemResult = anyhow::Result<()>;

/// Error returned by `Dispatcher::run_seq` and `Dispatcher::run_par`.
pub struct RunError {
    errors: Vec<SystemError>,
}

impl RunError {
    /// Returns all `SystemError`s as a slice.
    pub fn errors(&self) -> &[SystemError] {
        &self.errors
    }

    /// Returns all `SystemError`s as a vector.
    pub fn into_errors(self) -> Vec<SystemError> {
        self.errors
    }
}

impl From<SystemError> for RunError {
    fn from(error: SystemError) -> Self {
        Self {
            errors: vec![error],
        }
    }
}

impl From<Vec<SystemError>> for RunError {
    fn from(errors: Vec<SystemError>) -> Self {
        Self { errors }
    }
}

impl Deref for RunError {
    type Target = [SystemError];

    fn deref(&self) -> &Self::Target {
        &self.errors
    }
}

impl Error for RunError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.errors.first().map(|error| error.as_ref())
    }
}

impl Debug for RunError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self.errors.first() {
            Some(error) => Debug::fmt(error, f),
            None => Ok(()),
        }
    }
}

impl Display for RunError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self.errors.first() {
            Some(error) => Display::fmt(error, f),
            None => Ok(()),
        }
    }
}