dialectic_tokio_serde/
error.rs

1use super::*;
2
3/// Shorthand for an [`Error`] resulting from a symmetrically serialized/encoded connection.
4pub type SymmetricalError<F, E> = Error<F, F, E, E>;
5
6/// An error during operations on a [`Sender`] or [`Receiver`], unifying [`SendError`] and
7/// [`RecvError`].
8pub enum Error<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> {
9    /// An error occurred while attempting to [`send`](Transmit::send) a value.
10    Send(SendError<F, E>),
11    /// An error occurred while attempting to [`recv`](Receive::recv) a value.
12    Recv(RecvError<G, D>),
13}
14
15/// An error while sending on a [`Sender`].
16pub enum SendError<F: Serializer, E: Encoder<F::Output>> {
17    /// An error occurred while attempting to serialize a value.
18    Serialize(F::Error),
19    /// An error occurred while attempting to encode and transmit a serialized value as a frame.
20    Encode(E::Error),
21}
22
23/// An error while receiving from a [`Receiver`].
24pub enum RecvError<F: Deserializer<D::Item>, D: Decoder> {
25    /// An error occurred while attempting to deserialize a value.
26    Deserialize(F::Error),
27    /// An error occurred while attempting to receive and decode a serialized value as a frame.
28    Decode(D::Error),
29    /// The underlying stream was closed.
30    Closed,
31}
32
33impl<F, G, E, D> From<SendError<F, E>> for Error<F, G, E, D>
34where
35    F: Serializer,
36    G: Deserializer<D::Item>,
37    E: Encoder<F::Output>,
38    D: Decoder,
39{
40    fn from(err: SendError<F, E>) -> Self {
41        Error::Send(err)
42    }
43}
44
45impl<F, G, E, D> From<RecvError<G, D>> for Error<F, G, E, D>
46where
47    F: Serializer,
48    G: Deserializer<D::Item>,
49    E: Encoder<F::Output>,
50    D: Decoder,
51{
52    fn from(err: RecvError<G, D>) -> Self {
53        Error::Recv(err)
54    }
55}
56
57impl<F, G, E, D> std::error::Error for Error<F, G, E, D>
58where
59    F: Serializer,
60    G: Deserializer<D::Item>,
61    E: Encoder<F::Output>,
62    D: Decoder,
63    F::Error: Debug + Display,
64    G::Error: Debug + Display,
65    E::Error: Debug + Display,
66    D::Error: Debug + Display,
67{
68}
69
70impl<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> Debug
71    for Error<F, G, E, D>
72where
73    F::Error: Debug,
74    G::Error: Debug,
75    E::Error: Debug,
76    D::Error: Debug,
77{
78    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
79        match self {
80            Error::Send(err) => write!(f, "{:?}", err),
81            Error::Recv(err) => write!(f, "{:?}", err),
82        }
83    }
84}
85
86impl<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> Display
87    for Error<F, G, E, D>
88where
89    F::Error: Display,
90    G::Error: Display,
91    E::Error: Display,
92    D::Error: Display,
93{
94    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
95        match self {
96            Error::Send(err) => write!(f, "{}", err),
97            Error::Recv(err) => write!(f, "{}", err),
98        }
99    }
100}
101
102impl<F: Serializer, E: Encoder<F::Output>> Debug for SendError<F, E>
103where
104    F::Error: Debug,
105    E::Error: Debug,
106{
107    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
108        match self {
109            SendError::Serialize(err) => write!(f, "{:?}", err),
110            SendError::Encode(err) => write!(f, "{:?}", err),
111        }
112    }
113}
114
115impl<F: Serializer, E: Encoder<F::Output>> Display for SendError<F, E>
116where
117    F::Error: Display,
118    E::Error: Display,
119{
120    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
121        match self {
122            SendError::Serialize(err) => write!(f, "{}", err),
123            SendError::Encode(err) => write!(f, "{}", err),
124        }
125    }
126}
127
128impl<F, E> std::error::Error for SendError<F, E>
129where
130    F: Serializer,
131    E: Encoder<F::Output>,
132    F::Error: Display + Debug,
133    E::Error: Display + Debug,
134{
135}
136
137impl<F: Deserializer<D::Item>, D: Decoder> Debug for RecvError<F, D>
138where
139    F::Error: Debug,
140    D::Error: Debug,
141{
142    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
143        match self {
144            RecvError::Deserialize(err) => write!(f, "{:?}", err),
145            RecvError::Decode(err) => write!(f, "{:?}", err),
146            RecvError::Closed => write!(f, "Closed"),
147        }
148    }
149}
150
151impl<F: Deserializer<D::Item>, D: Decoder> Display for RecvError<F, D>
152where
153    F::Error: Display,
154    D::Error: Display,
155{
156    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
157        match self {
158            RecvError::Deserialize(err) => write!(f, "{}", err),
159            RecvError::Decode(err) => write!(f, "{}", err),
160            RecvError::Closed => write!(f, "connection closed"),
161        }
162    }
163}
164
165impl<F, D> std::error::Error for RecvError<F, D>
166where
167    F: Deserializer<D::Item>,
168    D: Decoder,
169    F::Error: Display + Debug,
170    D::Error: Display + Debug,
171{
172}
173
174impl<F, G, E, D> Clone for Error<F, G, E, D>
175where
176    F: Serializer,
177    G: Deserializer<D::Item>,
178    E: Encoder<F::Output>,
179    D: Decoder,
180    F::Error: Clone,
181    G::Error: Clone,
182    E::Error: Clone,
183    D::Error: Clone,
184{
185    fn clone(&self) -> Self {
186        match self {
187            Error::Send(err) => Error::Send(err.clone()),
188            Error::Recv(err) => Error::Recv(err.clone()),
189        }
190    }
191}
192
193impl<F, E> Clone for SendError<F, E>
194where
195    F: Serializer,
196    E: Encoder<F::Output>,
197    F::Error: Clone,
198    E::Error: Clone,
199{
200    fn clone(&self) -> Self {
201        match self {
202            SendError::Serialize(err) => SendError::Serialize(err.clone()),
203            SendError::Encode(err) => SendError::Encode(err.clone()),
204        }
205    }
206}
207
208impl<F, D> Clone for RecvError<F, D>
209where
210    F: Deserializer<D::Item>,
211    D: Decoder,
212    F::Error: Clone,
213    D::Error: Clone,
214{
215    fn clone(&self) -> Self {
216        match self {
217            RecvError::Deserialize(err) => RecvError::Deserialize(err.clone()),
218            RecvError::Decode(err) => RecvError::Decode(err.clone()),
219            RecvError::Closed => RecvError::Closed,
220        }
221    }
222}