elfo_core/
errors.rs

1use derive_more::{Display, Error};
2
3#[derive(Debug, Display, Error)]
4#[non_exhaustive]
5pub enum StartError {
6    /// Configs must be valid at the start-up.
7    #[display(fmt = "invalid config")]
8    InvalidConfig,
9    Other(#[error(not(source))] String),
10}
11
12#[derive(Debug, Display, Error)]
13#[display(fmt = "mailbox closed")]
14pub struct SendError<T>(#[error(not(source))] pub T);
15
16#[derive(Debug, Display, Error)]
17pub enum TrySendError<T> {
18    /// The mailbox is full.
19    #[display(fmt = "mailbox full")]
20    Full(#[error(not(source))] T),
21    /// The mailbox has been closed.
22    #[display(fmt = "mailbox closed")]
23    Closed(#[error(not(source))] T),
24}
25
26impl<T> TrySendError<T> {
27    /// Converts the error into its inner value.
28    #[inline]
29    pub fn into_inner(self) -> T {
30        match self {
31            Self::Closed(inner) => inner,
32            Self::Full(inner) => inner,
33        }
34    }
35
36    /// Transforms the inner message.
37    #[inline]
38    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> TrySendError<U> {
39        match self {
40            Self::Full(inner) => TrySendError::Full(f(inner)),
41            Self::Closed(inner) => TrySendError::Closed(f(inner)),
42        }
43    }
44
45    /// Returns whether the error is the `Full` variant.
46    #[inline]
47    pub fn is_full(&self) -> bool {
48        matches!(self, Self::Full(_))
49    }
50
51    /// Returns whether the error is the `Closed` variant.
52    #[inline]
53    pub fn is_closed(&self) -> bool {
54        matches!(self, Self::Closed(_))
55    }
56}
57
58#[derive(Debug, Display, Error)]
59pub enum RequestError<T> {
60    // Nobody has responded to the request.
61    #[display(fmt = "request ignored")]
62    Ignored, // TODO: can we provide `T` here?
63    /// The mailbox has been closed.
64    #[display(fmt = "mailbox closed")]
65    Closed(#[error(not(source))] T),
66}
67
68impl<T> RequestError<T> {
69    /// Converts the error into its inner value.
70    #[inline]
71    pub fn into_inner(self) -> Option<T> {
72        match self {
73            Self::Ignored => None,
74            Self::Closed(inner) => Some(inner),
75        }
76    }
77
78    /// Transforms the inner message.
79    #[inline]
80    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> RequestError<U> {
81        match self {
82            Self::Ignored => RequestError::Ignored,
83            Self::Closed(inner) => RequestError::Closed(f(inner)),
84        }
85    }
86
87    /// Returns whether the error is the `Full` variant.
88    #[inline]
89    pub fn is_ignored(&self) -> bool {
90        matches!(self, Self::Ignored)
91    }
92
93    /// Returns whether the error is the `Closed` variant.
94    #[inline]
95    pub fn is_closed(&self) -> bool {
96        matches!(self, Self::Closed(_))
97    }
98}
99
100#[derive(Debug, Clone, Display, Error)]
101pub enum TryRecvError {
102    /// The mailbox is empty.
103    #[display(fmt = "mailbox empty")]
104    Empty,
105    /// The mailbox has been closed.
106    #[display(fmt = "mailbox closed")]
107    Closed,
108}
109
110impl TryRecvError {
111    /// Returns whether the error is the `Empty` variant.
112    #[inline]
113    pub fn is_empty(&self) -> bool {
114        matches!(self, Self::Empty)
115    }
116
117    /// Returns whether the error is the `Closed` variant.
118    #[inline]
119    pub fn is_closed(&self) -> bool {
120        matches!(self, Self::Closed)
121    }
122}