Skip to main content

rocketmq_client_rust/consumer/
pop_status.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Display;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18pub enum PopStatus {
19    /// Founded
20    #[default]
21    Found,
22    /// No new message can be pulled after polling timeout
23    NoNewMsg,
24    /// Polling pool is full, do not try again immediately
25    PollingFull,
26    /// Polling timeout but no message found
27    PollingNotFound,
28}
29
30impl Display for PopStatus {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            PopStatus::Found => write!(f, "FOUND"),
34            PopStatus::NoNewMsg => write!(f, "NO_NEW_MSG"),
35            PopStatus::PollingFull => write!(f, "POLLING_FULL"),
36            PopStatus::PollingNotFound => write!(f, "POLLING_NOT_FOUND"),
37        }
38    }
39}
40
41impl From<PopStatus> for i32 {
42    fn from(status: PopStatus) -> i32 {
43        match status {
44            PopStatus::Found => 0,
45            PopStatus::NoNewMsg => 1,
46            PopStatus::PollingFull => 2,
47            PopStatus::PollingNotFound => 3,
48        }
49    }
50}
51
52impl From<i32> for PopStatus {
53    fn from(status: i32) -> Self {
54        match status {
55            0 => PopStatus::Found,
56            1 => PopStatus::NoNewMsg,
57            2 => PopStatus::PollingFull,
58            3 => PopStatus::PollingNotFound,
59            _ => PopStatus::Found,
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn pop_status_display_found() {
70        assert_eq!(PopStatus::Found.to_string(), "FOUND");
71    }
72
73    #[test]
74    fn pop_status_display_no_new_msg() {
75        assert_eq!(PopStatus::NoNewMsg.to_string(), "NO_NEW_MSG");
76    }
77
78    #[test]
79    fn pop_status_display_polling_full() {
80        assert_eq!(PopStatus::PollingFull.to_string(), "POLLING_FULL");
81    }
82
83    #[test]
84    fn pop_status_display_polling_not_found() {
85        assert_eq!(PopStatus::PollingNotFound.to_string(), "POLLING_NOT_FOUND");
86    }
87
88    #[test]
89    fn pop_status_from_i32_found() {
90        assert_eq!(PopStatus::from(0), PopStatus::Found);
91    }
92
93    #[test]
94    fn pop_status_from_i32_no_new_msg() {
95        assert_eq!(PopStatus::from(1), PopStatus::NoNewMsg);
96    }
97
98    #[test]
99    fn pop_status_from_i32_polling_full() {
100        assert_eq!(PopStatus::from(2), PopStatus::PollingFull);
101    }
102
103    #[test]
104    fn pop_status_from_i32_polling_not_found() {
105        assert_eq!(PopStatus::from(3), PopStatus::PollingNotFound);
106    }
107
108    #[test]
109    fn pop_status_from_i32_default() {
110        assert_eq!(PopStatus::from(999), PopStatus::Found);
111    }
112
113    #[test]
114    fn pop_status_into_i32_found() {
115        let status: i32 = PopStatus::Found.into();
116        assert_eq!(status, 0);
117    }
118
119    #[test]
120    fn pop_status_into_i32_no_new_msg() {
121        let status: i32 = PopStatus::NoNewMsg.into();
122        assert_eq!(status, 1);
123    }
124
125    #[test]
126    fn pop_status_into_i32_polling_full() {
127        let status: i32 = PopStatus::PollingFull.into();
128        assert_eq!(status, 2);
129    }
130
131    #[test]
132    fn pop_status_into_i32_polling_not_found() {
133        let status: i32 = PopStatus::PollingNotFound.into();
134        assert_eq!(status, 3);
135    }
136}