Skip to main content

polybox_core/
errors.rs

1use thiserror::Error;
2
3use crate::RxError;
4
5/// Error returned when trying to send a message, checking at compile-time that
6/// the message is actually accepted by the actor.
7#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq, Hash)]
8pub enum TrySendCheckedError<M> {
9    Full(M),
10    Closed(M),
11    NotAccepted(M),
12}
13
14/// Error returned when sending a message, checking at compile-time that
15/// the message is actually accepted by the actor.
16#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq, Hash)]
17pub enum SendCheckedError<M> {
18    Closed(M),
19    NotAccepted(M),
20}
21
22/// An error returned when trying to send a message.
23#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
24pub enum TrySendError<M> {
25    /// The channel has been closed, and no longer accepts new messages.
26    #[error("Couldn't send message because Channel is closed")]
27    Closed(M),
28    /// The channel is full.
29    #[error("Couldn't send message because Channel is full")]
30    Full(M),
31}
32
33/// The channel has been closed, and no longer accepts new messages.
34#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
35pub struct SendError<M>(pub M);
36
37/// Error returned when sending a request.
38///
39/// This error combines failures in sending and receiving.
40#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
41pub enum RequestError<M> {
42    NoReply,
43    Closed(M),
44}
45
46/// Error returned when trying to send a request.
47///
48/// This error combines failures in sending and receiving.
49#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
50pub enum TryRequestError<M, E> {
51    NoReply(E),
52    Closed(M),
53    Full(M),
54}
55
56/// Error returned when receiving a message.
57#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
58pub enum RecvError {
59    /// Process has been halted and should now exit.
60    #[error("Couldn't receive because the process has been halted")]
61    Halted,
62    /// Channel has been closed, and contains no more messages. It is impossible for new
63    /// messages to be sent to the channel.
64    #[error("Couldn't receive becuase the channel is closed and empty")]
65    ClosedAndEmpty,
66}
67
68/// Error returned when trying to receive a message.
69#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
70pub enum TryRecvError {
71    /// Process has been halted and should now exit.
72    #[error("Couldn't receive because the process has been halted")]
73    Halted,
74    /// The channel is empty, but is not yet closed. New messges may arrive.
75    #[error("Couldn't receive because the channel is empty")]
76    Empty,
77    /// Channel has been closed, and contains no more messages. It is impossible for new
78    /// messages to be sent to the channel.
79    #[error("Couldn't receive becuase the channel is closed and empty")]
80    ClosedAndEmpty,
81}
82
83/// This process has been halted and should now exit.
84#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
85#[error("Process has been halted")]
86pub struct Halted;
87
88impl<T> From<TrySendError<T>> for TrySendCheckedError<T> {
89    fn from(err: TrySendError<T>) -> Self {
90        match err {
91            TrySendError::Full(msg) => Self::Full(msg),
92            TrySendError::Closed(msg) => Self::Closed(msg),
93        }
94    }
95}
96
97impl<T> From<SendError<T>> for SendCheckedError<T> {
98    fn from(err: SendError<T>) -> Self {
99        Self::Closed(err.0)
100    }
101}
102
103impl<T> From<RxError> for RequestError<T> {
104    fn from(RxError: RxError) -> Self {
105        Self::NoReply
106    }
107}
108
109impl<T> From<SendError<T>> for RequestError<T> {
110    fn from(err: SendError<T>) -> Self {
111        Self::Closed(err.0)
112    }
113}