reconnecting_websocket/
event.rs

1use std::{fmt::Debug, task::Poll};
2
3use cfg_if::cfg_if;
4use gloo::net::websocket::Message;
5
6use crate::{Error, SocketInput, SocketOutput};
7
8cfg_if! {
9    if #[cfg(feature = "state-events")] {
10        use crate::State;
11
12        /// [`futures::Stream::Item`] type for [`crate::Socket`] when `state-events` feature is enabled
13        pub enum Event<I, O>
14        where
15            I: SocketInput,
16            O: SocketOutput,
17            Message: TryFrom<I>,
18            <Message as TryFrom<I>>::Error: Debug,
19            <O as TryFrom<Message>>::Error: Debug,
20        {
21            /// A message from the websocket mapped with `<`[`SocketOutput`]` as `[`TryFrom`]`>::try_from(`[`Message`]`)`
22            Message(Result<O, Error<I, O>>),
23            /// An update to the state of the underlying [`gloo::net::websocket::futures::WebSocket`]
24            State(State),
25        }
26
27        impl<I, O> From<Result<O, Error<I, O>>> for Event<I, O>
28        where
29            I: SocketInput,
30            O: SocketOutput,
31            Message: TryFrom<I>,
32            <Message as TryFrom<I>>::Error: Debug,
33            <O as TryFrom<Message>>::Error: Debug,
34        {
35            fn from(value: Result<O, Error<I, O>>) -> Self {
36                Self::Message(value)
37            }
38        }
39
40        impl<I, O> From<State> for Event<I, O>
41        where
42            I: SocketInput,
43            O: SocketOutput,
44            Message: TryFrom<I>,
45            <Message as TryFrom<I>>::Error: Debug,
46            <O as TryFrom<Message>>::Error: Debug,
47        {
48            fn from(value: State) -> Self {
49                Self::State(value)
50            }
51        }
52
53        pub(crate) fn map_poll<I, O>(
54            poll: Poll<Option<Result<O, Error<I, O>>>>,
55        ) -> Poll<Option<Event<I, O>>>
56        where
57            I: SocketInput,
58            O: SocketOutput,
59            Message: TryFrom<I>,
60            <Message as TryFrom<I>>::Error: Debug,
61            <O as TryFrom<Message>>::Error: Debug,
62        {
63            poll.map(|option| option.map(|result| Event::<_, _>::from(result)))
64        }
65
66        pub(crate) fn map_err<I, O>(
67            e: Error<I, O>,
68        ) -> Poll<Option<Event<I, O>>>
69        where
70            I: SocketInput,
71            O: SocketOutput,
72            Message: TryFrom<I>,
73            <Message as TryFrom<I>>::Error: Debug,
74            <O as TryFrom<Message>>::Error: Debug,
75        {
76            Poll::Ready(Some(Event::<_, _>::from(Err(e))))
77        }
78} else {
79        /// [`futures::Stream::Item`] type for [`Socket`] when `state-events` feature is not enabled
80        pub type Event<I, O> = Result<O, Error<I, O>>;
81
82        // Does nothing
83        pub(crate) fn map_poll<I, O>(
84            poll: Poll<Option<Event<I, O>>>,
85        ) -> Poll<Option<Event<I, O>>>
86        where
87            I: SocketInput,
88            O: SocketOutput,
89            Message: TryFrom<I>,
90            <Message as TryFrom<I>>::Error: Debug,
91            <O as TryFrom<Message>>::Error: Debug,
92        {
93            poll
94        }
95
96        pub(crate) fn map_err<I, O>(
97            e: Error<I, O>,
98        ) -> Poll<Option<Event<I, O>>>
99        where
100            I: SocketInput,
101            O: SocketOutput,
102            Message: TryFrom<I>,
103            <Message as TryFrom<I>>::Error: Debug,
104            <O as TryFrom<Message>>::Error: Debug,
105        {
106            Poll::Ready(Some(Err(e)))
107        }
108    }
109}