ccxt_core/ws_client/
event.rs1use std::sync::Arc;
4use std::time::Duration;
5
6#[derive(Debug, Clone)]
8pub enum WsEvent {
9 Connecting,
11 Connected,
13 Disconnected,
15 Reconnecting {
17 attempt: u32,
19 delay: Duration,
21 error: Option<String>,
23 },
24 ReconnectSuccess,
26 ReconnectFailed {
28 attempt: u32,
30 error: String,
32 is_permanent: bool,
34 },
35 ReconnectExhausted {
37 total_attempts: u32,
39 last_error: String,
41 },
42 SubscriptionRestored,
44 PermanentError {
46 error: String,
48 },
49 Shutdown,
51}
52
53impl WsEvent {
54 #[inline]
56 #[must_use]
57 pub fn is_connecting(&self) -> bool {
58 matches!(self, Self::Connecting)
59 }
60
61 #[inline]
63 #[must_use]
64 pub fn is_connected(&self) -> bool {
65 matches!(self, Self::Connected)
66 }
67
68 #[inline]
70 #[must_use]
71 pub fn is_disconnected(&self) -> bool {
72 matches!(self, Self::Disconnected)
73 }
74
75 #[inline]
77 #[must_use]
78 pub fn is_reconnecting(&self) -> bool {
79 matches!(self, Self::Reconnecting { .. })
80 }
81
82 #[inline]
84 #[must_use]
85 pub fn is_reconnect_success(&self) -> bool {
86 matches!(self, Self::ReconnectSuccess)
87 }
88
89 #[inline]
91 #[must_use]
92 pub fn is_reconnect_failed(&self) -> bool {
93 matches!(self, Self::ReconnectFailed { .. })
94 }
95
96 #[inline]
98 #[must_use]
99 pub fn is_reconnect_exhausted(&self) -> bool {
100 matches!(self, Self::ReconnectExhausted { .. })
101 }
102
103 #[inline]
105 #[must_use]
106 pub fn is_subscription_restored(&self) -> bool {
107 matches!(self, Self::SubscriptionRestored)
108 }
109
110 #[inline]
112 #[must_use]
113 pub fn is_permanent_error(&self) -> bool {
114 matches!(self, Self::PermanentError { .. })
115 }
116
117 #[inline]
119 #[must_use]
120 pub fn is_shutdown(&self) -> bool {
121 matches!(self, Self::Shutdown)
122 }
123
124 #[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 #[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
191pub type WsEventCallback = Arc<dyn Fn(WsEvent) + Send + Sync>;