1macro_rules! define_response_code {
21 (
22 $(#[$enum_meta:meta])*
23 pub enum $enum_name:ident {
24 $(
25 $(#[$variant_meta:meta])*
26 $variant:ident = $value:expr
27 ),* $(,)?
28 },
29 default = $default:ident
30 ) => {
31 $(#[$enum_meta])*
32 #[repr(i32)]
33 pub enum $enum_name {
34 $(
35 $(#[$variant_meta])*
36 $variant = $value,
37 )*
38 }
39
40 impl From<$enum_name> for i32 {
41 #[inline]
42 fn from(value: $enum_name) -> Self {
43 value as i32
44 }
45 }
46
47 impl From<i32> for $enum_name {
48 #[inline]
49 fn from(value: i32) -> Self {
50 match value {
51 $($value => $enum_name::$variant,)*
52 _ => $enum_name::$default,
53 }
54 }
55 }
56
57 impl $enum_name {
58 #[inline]
60 pub const fn to_i32(self) -> i32 {
61 self as i32
62 }
63
64 #[inline]
66 pub const fn is_success(&self) -> bool {
67 matches!(self, Self::Success)
68 }
69
70 #[inline]
72 pub const fn is_error(&self) -> bool {
73 !self.is_success()
74 }
75 }
76 };
77}
78
79define_response_code! {
80 #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
81 pub enum RemotingSysResponseCode {
82 Success = 0,
83 SystemError = 1,
84 SystemBusy = 2,
85 RequestCodeNotSupported = 3,
86 TransactionFailed = 4,
87 NoPermission = 16,
88 },
89 default = SystemError
90}
91
92define_response_code! {
93 #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
94 pub enum ResponseCode {
95 Success = 0,
96 SystemError = 1,
97 SystemBusy = 2,
98 RequestCodeNotSupported = 3,
99 TransactionFailed = 4,
100 FlushDiskTimeout = 10,
101 SlaveNotAvailable = 11,
102 FlushSlaveTimeout = 12,
103 MessageIllegal = 13,
104 ServiceNotAvailable = 14,
105 VersionNotSupported = 15,
106 NoPermission = 16,
107 TopicNotExist = 17,
108 TopicExistAlready = 18,
109 PullNotFound = 19,
110 PullRetryImmediately = 20,
111 PullOffsetMoved = 21,
112 QueryNotFound = 22,
113 SubscriptionParseFailed = 23,
114 SubscriptionNotExist = 24,
115 SubscriptionNotLatest = 25,
116 SubscriptionGroupNotExist = 26,
117 FilterDataNotExist = 27,
118 FilterDataNotLatest = 28,
119 TransactionShouldCommit = 200,
120 TransactionShouldRollback = 201,
121 TransactionStateUnknow = 202,
122 TransactionStateGroupWrong = 203,
123 NoBuyerId = 204,
124 NotInCurrentUnit = 205,
125 ConsumerNotOnline = 206,
126 ConsumeMsgTimeout = 207,
127 NoMessage = 208,
128 PollingFull = 209,
132 PollingTimeout = 210,
133 BrokerNotExist = 211,
134 BrokerDispatchNotComplete = 212,
135 BroadcastConsumption = 213,
136 FlowControl = 215,
137 NotLeaderForQueue = 501,
138 IllegalOperation = 604,
139 RpcUnknown = -1000,
140 RpcAddrIsNull = -1002,
141 RpcSendToChannelFailed = -1004,
142 RpcTimeOut = -1006,
143 GoAway = 1500,
144 ControllerFencedMasterEpoch = 2000,
145 ControllerFencedSyncStateSetEpoch = 2001,
146 ControllerInvalidMaster = 2002,
147 ControllerInvalidReplicas = 2003,
148 ControllerMasterNotAvailable = 2004,
149 ControllerInvalidRequest = 2005,
150 ControllerBrokerNotAlive = 2006,
151 ControllerNotLeader = 2007,
152 ControllerBrokerMetadataNotExist = 2008,
153 ControllerInvalidCleanBrokerMetadata = 2009,
154 ControllerBrokerNeedToBeRegistered = 2010,
155 ControllerMasterStillExist = 2011,
156 ControllerElectMasterFailed = 2012,
157 ControllerAlterSyncStateSetFailed = 2013,
158 ControllerBrokerIdInvalid = 2014,
159 },
160 default = SystemError
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn test_remoting_sys_response_code_to_i32() {
169 assert_eq!(RemotingSysResponseCode::Success.to_i32(), 0);
170 assert_eq!(RemotingSysResponseCode::SystemError.to_i32(), 1);
171 assert_eq!(RemotingSysResponseCode::SystemBusy.to_i32(), 2);
172 assert_eq!(RemotingSysResponseCode::RequestCodeNotSupported.to_i32(), 3);
173 assert_eq!(RemotingSysResponseCode::TransactionFailed.to_i32(), 4);
174 assert_eq!(RemotingSysResponseCode::NoPermission.to_i32(), 16);
175 }
176
177 #[test]
178 fn remoting_sys_response_code_from_i32() {
179 assert_eq!(
180 RemotingSysResponseCode::from(0),
181 RemotingSysResponseCode::Success
182 );
183 assert_eq!(
184 RemotingSysResponseCode::from(1),
185 RemotingSysResponseCode::SystemError
186 );
187 assert_eq!(
188 RemotingSysResponseCode::from(2),
189 RemotingSysResponseCode::SystemBusy
190 );
191 assert_eq!(
192 RemotingSysResponseCode::from(3),
193 RemotingSysResponseCode::RequestCodeNotSupported
194 );
195 assert_eq!(
196 RemotingSysResponseCode::from(4),
197 RemotingSysResponseCode::TransactionFailed
198 );
199 assert_eq!(
200 RemotingSysResponseCode::from(16),
201 RemotingSysResponseCode::NoPermission
202 );
203 assert_eq!(
204 RemotingSysResponseCode::from(999),
205 RemotingSysResponseCode::SystemError
206 ); }
208
209 #[test]
210 fn test_remoting_sys_response_code_is_success() {
211 assert!(RemotingSysResponseCode::Success.is_success());
212 assert!(!RemotingSysResponseCode::SystemError.is_success());
213 assert!(!RemotingSysResponseCode::SystemBusy.is_success());
214 }
215
216 #[test]
217 fn test_remoting_sys_response_code_is_error() {
218 assert!(!RemotingSysResponseCode::Success.is_error());
219 assert!(RemotingSysResponseCode::SystemError.is_error());
220 assert!(RemotingSysResponseCode::SystemBusy.is_error());
221 assert!(RemotingSysResponseCode::NoPermission.is_error());
222 }
223
224 #[test]
225 fn test_response_code_to_i32() {
226 assert_eq!(ResponseCode::Success.to_i32(), 0);
227 assert_eq!(ResponseCode::SystemError.to_i32(), 1);
228 assert_eq!(ResponseCode::FlushDiskTimeout.to_i32(), 10);
229 assert_eq!(ResponseCode::RpcUnknown.to_i32(), -1000);
230 assert_eq!(ResponseCode::ControllerFencedMasterEpoch.to_i32(), 2000);
231 }
232
233 #[test]
234 fn response_code_from_i32() {
235 assert_eq!(ResponseCode::from(0), ResponseCode::Success);
236 assert_eq!(ResponseCode::from(1), ResponseCode::SystemError);
237 assert_eq!(ResponseCode::from(2), ResponseCode::SystemBusy);
238 assert_eq!(ResponseCode::from(3), ResponseCode::RequestCodeNotSupported);
239 assert_eq!(ResponseCode::from(4), ResponseCode::TransactionFailed);
240 assert_eq!(ResponseCode::from(10), ResponseCode::FlushDiskTimeout);
241 assert_eq!(ResponseCode::from(11), ResponseCode::SlaveNotAvailable);
242 assert_eq!(ResponseCode::from(12), ResponseCode::FlushSlaveTimeout);
243 assert_eq!(ResponseCode::from(13), ResponseCode::MessageIllegal);
244 assert_eq!(ResponseCode::from(14), ResponseCode::ServiceNotAvailable);
245 assert_eq!(ResponseCode::from(15), ResponseCode::VersionNotSupported);
246 assert_eq!(ResponseCode::from(16), ResponseCode::NoPermission);
247 assert_eq!(ResponseCode::from(17), ResponseCode::TopicNotExist);
248 assert_eq!(ResponseCode::from(18), ResponseCode::TopicExistAlready);
249 assert_eq!(ResponseCode::from(19), ResponseCode::PullNotFound);
250 assert_eq!(ResponseCode::from(20), ResponseCode::PullRetryImmediately);
251 assert_eq!(ResponseCode::from(21), ResponseCode::PullOffsetMoved);
252 assert_eq!(ResponseCode::from(22), ResponseCode::QueryNotFound);
253 assert_eq!(
254 ResponseCode::from(23),
255 ResponseCode::SubscriptionParseFailed
256 );
257 assert_eq!(ResponseCode::from(24), ResponseCode::SubscriptionNotExist);
258 assert_eq!(ResponseCode::from(25), ResponseCode::SubscriptionNotLatest);
259 assert_eq!(
260 ResponseCode::from(26),
261 ResponseCode::SubscriptionGroupNotExist
262 );
263 assert_eq!(ResponseCode::from(27), ResponseCode::FilterDataNotExist);
264 assert_eq!(ResponseCode::from(28), ResponseCode::FilterDataNotLatest);
265 assert_eq!(
266 ResponseCode::from(200),
267 ResponseCode::TransactionShouldCommit
268 );
269 assert_eq!(
270 ResponseCode::from(201),
271 ResponseCode::TransactionShouldRollback
272 );
273 assert_eq!(
274 ResponseCode::from(202),
275 ResponseCode::TransactionStateUnknow
276 );
277 assert_eq!(
278 ResponseCode::from(203),
279 ResponseCode::TransactionStateGroupWrong
280 );
281 assert_eq!(ResponseCode::from(204), ResponseCode::NoBuyerId);
282 assert_eq!(ResponseCode::from(205), ResponseCode::NotInCurrentUnit);
283 assert_eq!(ResponseCode::from(206), ResponseCode::ConsumerNotOnline);
284 assert_eq!(ResponseCode::from(207), ResponseCode::ConsumeMsgTimeout);
285 assert_eq!(ResponseCode::from(208), ResponseCode::NoMessage);
286 assert_eq!(ResponseCode::from(209), ResponseCode::PollingFull);
287 assert_eq!(ResponseCode::from(210), ResponseCode::PollingTimeout);
288 assert_eq!(ResponseCode::from(211), ResponseCode::BrokerNotExist);
289 assert_eq!(
290 ResponseCode::from(212),
291 ResponseCode::BrokerDispatchNotComplete
292 );
293 assert_eq!(ResponseCode::from(213), ResponseCode::BroadcastConsumption);
294 assert_eq!(ResponseCode::from(215), ResponseCode::FlowControl);
295 assert_eq!(ResponseCode::from(501), ResponseCode::NotLeaderForQueue);
296 assert_eq!(ResponseCode::from(604), ResponseCode::IllegalOperation);
297 assert_eq!(ResponseCode::from(-1000), ResponseCode::RpcUnknown);
298 assert_eq!(ResponseCode::from(-1002), ResponseCode::RpcAddrIsNull);
299 assert_eq!(
300 ResponseCode::from(-1004),
301 ResponseCode::RpcSendToChannelFailed
302 );
303 assert_eq!(ResponseCode::from(-1006), ResponseCode::RpcTimeOut);
304 assert_eq!(ResponseCode::from(1500), ResponseCode::GoAway);
305 assert_eq!(
306 ResponseCode::from(2000),
307 ResponseCode::ControllerFencedMasterEpoch
308 );
309 assert_eq!(
310 ResponseCode::from(2001),
311 ResponseCode::ControllerFencedSyncStateSetEpoch
312 );
313 assert_eq!(
314 ResponseCode::from(2002),
315 ResponseCode::ControllerInvalidMaster
316 );
317 assert_eq!(
318 ResponseCode::from(2003),
319 ResponseCode::ControllerInvalidReplicas
320 );
321 assert_eq!(
322 ResponseCode::from(2004),
323 ResponseCode::ControllerMasterNotAvailable
324 );
325 assert_eq!(
326 ResponseCode::from(2005),
327 ResponseCode::ControllerInvalidRequest
328 );
329 assert_eq!(
330 ResponseCode::from(2006),
331 ResponseCode::ControllerBrokerNotAlive
332 );
333 assert_eq!(ResponseCode::from(2007), ResponseCode::ControllerNotLeader);
334 assert_eq!(
335 ResponseCode::from(2008),
336 ResponseCode::ControllerBrokerMetadataNotExist
337 );
338 assert_eq!(
339 ResponseCode::from(2009),
340 ResponseCode::ControllerInvalidCleanBrokerMetadata
341 );
342 assert_eq!(
343 ResponseCode::from(2010),
344 ResponseCode::ControllerBrokerNeedToBeRegistered
345 );
346 assert_eq!(
347 ResponseCode::from(2011),
348 ResponseCode::ControllerMasterStillExist
349 );
350 assert_eq!(
351 ResponseCode::from(2012),
352 ResponseCode::ControllerElectMasterFailed
353 );
354 assert_eq!(
355 ResponseCode::from(2013),
356 ResponseCode::ControllerAlterSyncStateSetFailed
357 );
358 assert_eq!(
359 ResponseCode::from(2014),
360 ResponseCode::ControllerBrokerIdInvalid
361 );
362 assert_eq!(ResponseCode::from(9999), ResponseCode::SystemError); }
365
366 #[test]
367 fn test_response_code_is_success() {
368 assert!(ResponseCode::Success.is_success());
369 assert!(!ResponseCode::SystemError.is_success());
370 assert!(!ResponseCode::FlushDiskTimeout.is_success());
371 assert!(!ResponseCode::RpcUnknown.is_success());
372 }
373
374 #[test]
375 fn test_response_code_is_error() {
376 assert!(!ResponseCode::Success.is_error());
377 assert!(ResponseCode::SystemError.is_error());
378 assert!(ResponseCode::SystemBusy.is_error());
379 assert!(ResponseCode::TopicNotExist.is_error());
380 assert!(ResponseCode::RpcTimeOut.is_error());
381 }
382
383 #[test]
384 fn test_response_code_round_trip() {
385 let codes = vec![
386 ResponseCode::Success,
387 ResponseCode::SystemError,
388 ResponseCode::FlushDiskTimeout,
389 ResponseCode::NoPermission,
390 ResponseCode::TransactionShouldCommit,
391 ResponseCode::RpcUnknown,
392 ResponseCode::ControllerNotLeader,
393 ];
394
395 for code in codes {
396 let i32_val = code.to_i32();
397 let converted_back = ResponseCode::from(i32_val);
398 assert_eq!(code, converted_back, "Round trip failed for {:?}", code);
399 }
400 }
401
402 #[test]
403 fn test_response_code_from_trait() {
404 let code: i32 = ResponseCode::Success.into();
405 assert_eq!(code, 0);
406
407 let code: i32 = ResponseCode::SystemError.into();
408 assert_eq!(code, 1);
409
410 let code: i32 = ResponseCode::RpcUnknown.into();
411 assert_eq!(code, -1000);
412 }
413
414 #[test]
415 fn test_response_code_const_fn() {
416 const SUCCESS_CODE: i32 = ResponseCode::Success.to_i32();
417 assert_eq!(SUCCESS_CODE, 0);
418
419 const ERROR_CODE: i32 = ResponseCode::SystemError.to_i32();
420 assert_eq!(ERROR_CODE, 1);
421 }
422
423 #[test]
424 fn test_response_code_derive_traits() {
425 let code = ResponseCode::Success;
427 assert_eq!(format!("{:?}", code), "Success");
428
429 let code1 = ResponseCode::SystemError;
431 let code2 = code1;
432 assert_eq!(code1, code2);
433
434 assert_eq!(ResponseCode::Success, ResponseCode::Success);
436 assert_ne!(ResponseCode::Success, ResponseCode::SystemError);
437
438 use std::collections::HashSet;
440 let mut set = HashSet::new();
441 set.insert(ResponseCode::Success);
442 set.insert(ResponseCode::Success); set.insert(ResponseCode::SystemError);
444 assert_eq!(set.len(), 2);
445 }
446
447 #[test]
448 fn test_response_code_repr_i32_size() {
449 use std::mem::size_of;
450 assert_eq!(size_of::<ResponseCode>(), size_of::<i32>());
451 assert_eq!(size_of::<RemotingSysResponseCode>(), size_of::<i32>());
452 }
453
454 #[test]
455 fn test_negative_response_codes() {
456 assert_eq!(ResponseCode::from(-1000), ResponseCode::RpcUnknown);
458 assert_eq!(ResponseCode::from(-1002), ResponseCode::RpcAddrIsNull);
459 assert_eq!(
460 ResponseCode::from(-1004),
461 ResponseCode::RpcSendToChannelFailed
462 );
463 assert_eq!(ResponseCode::from(-1006), ResponseCode::RpcTimeOut);
464
465 assert_eq!(ResponseCode::RpcUnknown.to_i32(), -1000);
466 assert_eq!(ResponseCode::RpcAddrIsNull.to_i32(), -1002);
467 }
468
469 #[test]
470 fn test_controller_response_codes() {
471 assert_eq!(
473 ResponseCode::from(2000),
474 ResponseCode::ControllerFencedMasterEpoch
475 );
476 assert_eq!(ResponseCode::from(2007), ResponseCode::ControllerNotLeader);
477 assert_eq!(
478 ResponseCode::from(2014),
479 ResponseCode::ControllerBrokerIdInvalid
480 );
481
482 assert_eq!(ResponseCode::ControllerFencedMasterEpoch.to_i32(), 2000);
483 assert_eq!(ResponseCode::ControllerNotLeader.to_i32(), 2007);
484 }
485}