Skip to main content

ccxt_core/ws_client/
event.rs

1//! WebSocket connection events.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6/// WebSocket connection event types.
7#[derive(Debug, Clone)]
8pub enum WsEvent {
9    /// Connection attempt started.
10    Connecting,
11    /// Connection established successfully.
12    Connected,
13    /// Connection closed.
14    Disconnected,
15    /// Reconnection in progress.
16    Reconnecting {
17        /// Current reconnection attempt number (1-indexed).
18        attempt: u32,
19        /// Delay before this reconnection attempt.
20        delay: Duration,
21        /// Error that triggered the reconnection (if any).
22        error: Option<String>,
23    },
24    /// Reconnection succeeded.
25    ReconnectSuccess,
26    /// Single reconnection attempt failed.
27    ReconnectFailed {
28        /// The attempt number that failed (1-indexed).
29        attempt: u32,
30        /// Error message describing the failure.
31        error: String,
32        /// Whether this is a permanent error that should not be retried.
33        is_permanent: bool,
34    },
35    /// All reconnection attempts exhausted.
36    ReconnectExhausted {
37        /// Total number of reconnection attempts made.
38        total_attempts: u32,
39        /// The last error encountered.
40        last_error: String,
41    },
42    /// Subscriptions restored after reconnection.
43    SubscriptionRestored,
44    /// Permanent error occurred (no retry).
45    PermanentError {
46        /// Error message describing the permanent failure.
47        error: String,
48    },
49    /// Shutdown completed.
50    Shutdown,
51}
52
53impl WsEvent {
54    /// Returns true if this is a Connecting event.
55    #[inline]
56    #[must_use]
57    pub fn is_connecting(&self) -> bool {
58        matches!(self, Self::Connecting)
59    }
60
61    /// Returns true if this is a Connected event.
62    #[inline]
63    #[must_use]
64    pub fn is_connected(&self) -> bool {
65        matches!(self, Self::Connected)
66    }
67
68    /// Returns true if this is a Disconnected event.
69    #[inline]
70    #[must_use]
71    pub fn is_disconnected(&self) -> bool {
72        matches!(self, Self::Disconnected)
73    }
74
75    /// Returns true if this is a Reconnecting event.
76    #[inline]
77    #[must_use]
78    pub fn is_reconnecting(&self) -> bool {
79        matches!(self, Self::Reconnecting { .. })
80    }
81
82    /// Returns true if this is a ReconnectSuccess event.
83    #[inline]
84    #[must_use]
85    pub fn is_reconnect_success(&self) -> bool {
86        matches!(self, Self::ReconnectSuccess)
87    }
88
89    /// Returns true if this is a ReconnectFailed event.
90    #[inline]
91    #[must_use]
92    pub fn is_reconnect_failed(&self) -> bool {
93        matches!(self, Self::ReconnectFailed { .. })
94    }
95
96    /// Returns true if this is a ReconnectExhausted event.
97    #[inline]
98    #[must_use]
99    pub fn is_reconnect_exhausted(&self) -> bool {
100        matches!(self, Self::ReconnectExhausted { .. })
101    }
102
103    /// Returns true if this is a SubscriptionRestored event.
104    #[inline]
105    #[must_use]
106    pub fn is_subscription_restored(&self) -> bool {
107        matches!(self, Self::SubscriptionRestored)
108    }
109
110    /// Returns true if this is a PermanentError event.
111    #[inline]
112    #[must_use]
113    pub fn is_permanent_error(&self) -> bool {
114        matches!(self, Self::PermanentError { .. })
115    }
116
117    /// Returns true if this is a Shutdown event.
118    #[inline]
119    #[must_use]
120    pub fn is_shutdown(&self) -> bool {
121        matches!(self, Self::Shutdown)
122    }
123
124    /// Returns true if this is any error event.
125    #[inline]
126    #[must_use]
127    pub fn is_error(&self) -> bool {
128        matches!(
129            self,
130            Self::ReconnectFailed { .. }
131                | Self::ReconnectExhausted { .. }
132                | Self::PermanentError { .. }
133        )
134    }
135
136    /// Returns true if this is a terminal event.
137    #[inline]
138    #[must_use]
139    pub fn is_terminal(&self) -> bool {
140        matches!(
141            self,
142            Self::ReconnectExhausted { .. } | Self::PermanentError { .. } | Self::Shutdown
143        )
144    }
145}
146
147impl std::fmt::Display for WsEvent {
148    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149        match self {
150            Self::Connecting => write!(f, "Connecting"),
151            Self::Connected => write!(f, "Connected"),
152            Self::Disconnected => write!(f, "Disconnected"),
153            Self::Reconnecting {
154                attempt,
155                delay,
156                error,
157            } => {
158                write!(f, "Reconnecting (attempt {attempt}, delay: {delay:?}")?;
159                if let Some(err) = error {
160                    write!(f, ", error: {err}")?;
161                }
162                write!(f, ")")
163            }
164            Self::ReconnectSuccess => write!(f, "ReconnectSuccess"),
165            Self::ReconnectFailed {
166                attempt,
167                error,
168                is_permanent,
169            } => {
170                write!(
171                    f,
172                    "ReconnectFailed (attempt {attempt}, error: {error}, permanent: {is_permanent})"
173                )
174            }
175            Self::ReconnectExhausted {
176                total_attempts,
177                last_error,
178            } => {
179                write!(
180                    f,
181                    "ReconnectExhausted (attempts: {total_attempts}, last error: {last_error})"
182                )
183            }
184            Self::SubscriptionRestored => write!(f, "SubscriptionRestored"),
185            Self::PermanentError { error } => write!(f, "PermanentError: {error}"),
186            Self::Shutdown => write!(f, "Shutdown"),
187        }
188    }
189}
190
191/// Event callback function type.
192pub type WsEventCallback = Arc<dyn Fn(WsEvent) + Send + Sync>;