barter_data/streams/reconnect/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub mod stream;
4
5/// [`ReconnectingStream`](stream::ReconnectingStream) `Event` that communicates either `Stream::Item`, or that the inner
6/// `Stream` is currently reconnecting.
7#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
8pub enum Event<Origin, T> {
9    /// [`ReconnectingStream`](stream::ReconnectingStream) has disconnecting and is
10    /// attempting to reconnect.
11    Reconnecting(Origin),
12    Item(T),
13}
14
15impl<Origin, T> From<T> for Event<Origin, T> {
16    fn from(value: T) -> Self {
17        Self::Item(value)
18    }
19}
20
21impl<Origin, T> Event<Origin, T> {
22    pub fn map<F, O>(self, op: F) -> Event<Origin, O>
23    where
24        F: FnOnce(T) -> O,
25    {
26        match self {
27            Event::Reconnecting(origin) => Event::Reconnecting(origin),
28            Event::Item(item) => Event::Item(op(item)),
29        }
30    }
31}
32
33impl<Origin, T, E> Event<Origin, Result<T, E>> {
34    pub fn map_ok<F, O>(self, op: F) -> Event<Origin, Result<O, E>>
35    where
36        F: FnOnce(T) -> O,
37    {
38        match self {
39            Event::Reconnecting(origin) => Event::Reconnecting(origin),
40            Event::Item(result) => Event::Item(result.map(op)),
41        }
42    }
43
44    pub fn map_err<F, O>(self, op: F) -> Event<Origin, Result<T, O>>
45    where
46        F: FnOnce(E) -> O,
47    {
48        match self {
49            Event::Reconnecting(origin) => Event::Reconnecting(origin),
50            Event::Item(result) => Event::Item(result.map_err(op)),
51        }
52    }
53}