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
use alloc::vec::Vec;
use core::fmt;

use retry_policy::StopReason as RetryPolicyStopReason;

//
pub struct Error<T> {
    pub stop_reason: RetryPolicyStopReason,
    errors: Vec<T>,
}

impl<T> Error<T> {
    pub(crate) fn new(stop_reason: RetryPolicyStopReason, errors: Vec<T>) -> Self {
        assert!(!errors.is_empty());

        Self {
            stop_reason,
            errors,
        }
    }

    pub fn last_error(mut self) -> T {
        self.errors.pop().expect("unreachable!()")
    }

    pub fn errors(self) -> Vec<T> {
        self.errors
    }
}

impl<T> fmt::Debug for Error<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Error")
            .field("stop_reason", &self.stop_reason)
            .field("errors", &self.errors)
            .finish()
    }
}

impl<T> fmt::Display for Error<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

#[cfg(feature = "std")]
impl<T> std::error::Error for Error<T> where T: fmt::Debug {}