Skip to content

Commit d6e2554

Browse files
authored
[ISSUE #1659]🚀Add AckStatus enum🔥 (#1660)
1 parent 8bbac8f commit d6e2554

2 files changed

Lines changed: 237 additions & 0 deletions

File tree

rocketmq-client/src/consumer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
pub(crate) mod ack_status;
1718
pub mod allocate_message_queue_strategy;
1819
pub(crate) mod consumer_impl;
1920
pub mod default_mq_push_consumer;
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
use std::fmt::Display;
18+
19+
use cheetah_string::CheetahString;
20+
use serde::Deserialize;
21+
use serde::Deserializer;
22+
use serde::Serialize;
23+
use serde::Serializer;
24+
25+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
26+
pub enum AckStatus {
27+
#[default]
28+
Ok, //ack success
29+
NotExist, // msg not exist
30+
}
31+
32+
impl AckStatus {
33+
pub fn from_i32(value: i32) -> Option<Self> {
34+
match value {
35+
0 => Some(AckStatus::Ok),
36+
1 => Some(AckStatus::NotExist),
37+
_ => None,
38+
}
39+
}
40+
41+
pub fn to_i32(self) -> i32 {
42+
match self {
43+
AckStatus::Ok => 0,
44+
AckStatus::NotExist => 1,
45+
}
46+
}
47+
}
48+
49+
impl Display for AckStatus {
50+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51+
match self {
52+
AckStatus::Ok => write!(f, "OK"),
53+
AckStatus::NotExist => write!(f, "NO_EXIST"),
54+
}
55+
}
56+
}
57+
58+
impl From<String> for AckStatus {
59+
fn from(s: String) -> Self {
60+
match s.as_str() {
61+
"OK" => AckStatus::Ok,
62+
"NO_EXIST" => AckStatus::NotExist,
63+
_ => AckStatus::default(),
64+
}
65+
}
66+
}
67+
68+
impl From<&str> for AckStatus {
69+
fn from(s: &str) -> Self {
70+
match s {
71+
"OK" => AckStatus::Ok,
72+
"NO_EXIST" => AckStatus::NotExist,
73+
_ => AckStatus::default(),
74+
}
75+
}
76+
}
77+
78+
impl From<AckStatus> for String {
79+
fn from(status: AckStatus) -> Self {
80+
match status {
81+
AckStatus::Ok => "OK".to_string(),
82+
AckStatus::NotExist => "NO_EXIST".to_string(),
83+
}
84+
}
85+
}
86+
87+
impl From<AckStatus> for i32 {
88+
fn from(status: AckStatus) -> Self {
89+
match status {
90+
AckStatus::Ok => 0,
91+
AckStatus::NotExist => 1,
92+
}
93+
}
94+
}
95+
96+
impl From<i32> for AckStatus {
97+
fn from(value: i32) -> Self {
98+
match value {
99+
0 => AckStatus::Ok,
100+
1 => AckStatus::NotExist,
101+
_ => AckStatus::default(),
102+
}
103+
}
104+
}
105+
106+
impl From<CheetahString> for AckStatus {
107+
fn from(s: CheetahString) -> Self {
108+
match s.as_str() {
109+
"OK" => AckStatus::Ok,
110+
"NO_EXIST" => AckStatus::NotExist,
111+
_ => AckStatus::default(),
112+
}
113+
}
114+
}
115+
116+
impl Serialize for AckStatus {
117+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
118+
where
119+
S: Serializer,
120+
{
121+
let s = match *self {
122+
AckStatus::Ok => "OK",
123+
AckStatus::NotExist => "NO_EXIST",
124+
};
125+
serializer.serialize_str(s)
126+
}
127+
}
128+
129+
impl<'de> Deserialize<'de> for AckStatus {
130+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
131+
where
132+
D: Deserializer<'de>,
133+
{
134+
let s = String::deserialize(deserializer)?;
135+
match s.as_str() {
136+
"OK" => Ok(AckStatus::Ok),
137+
"NO_EXIST" => Ok(AckStatus::NotExist),
138+
_ => Err(serde::de::Error::custom("unknown value")),
139+
}
140+
}
141+
}
142+
143+
#[cfg(test)]
144+
mod tests {
145+
use cheetah_string::CheetahString;
146+
use serde_json;
147+
148+
use super::*;
149+
150+
#[test]
151+
fn ack_status_from_i32_valid_values() {
152+
assert_eq!(AckStatus::from_i32(0), Some(AckStatus::Ok));
153+
assert_eq!(AckStatus::from_i32(1), Some(AckStatus::NotExist));
154+
}
155+
156+
#[test]
157+
fn ack_status_from_i32_invalid_value() {
158+
assert_eq!(AckStatus::from_i32(2), None);
159+
}
160+
161+
#[test]
162+
fn ack_status_to_i32() {
163+
assert_eq!(AckStatus::Ok.to_i32(), 0);
164+
assert_eq!(AckStatus::NotExist.to_i32(), 1);
165+
}
166+
167+
#[test]
168+
fn ack_status_display() {
169+
assert_eq!(format!("{}", AckStatus::Ok), "OK");
170+
assert_eq!(format!("{}", AckStatus::NotExist), "NO_EXIST");
171+
}
172+
173+
#[test]
174+
fn ack_status_from_string() {
175+
assert_eq!(AckStatus::from("OK".to_string()), AckStatus::Ok);
176+
assert_eq!(AckStatus::from("NO_EXIST".to_string()), AckStatus::NotExist);
177+
assert_eq!(AckStatus::from("UNKNOWN".to_string()), AckStatus::default());
178+
}
179+
180+
#[test]
181+
fn ack_status_from_str() {
182+
assert_eq!(AckStatus::from("OK"), AckStatus::Ok);
183+
assert_eq!(AckStatus::from("NO_EXIST"), AckStatus::NotExist);
184+
assert_eq!(AckStatus::from("UNKNOWN"), AckStatus::default());
185+
}
186+
187+
#[test]
188+
fn ack_status_from_cheetah_string() {
189+
assert_eq!(AckStatus::from(CheetahString::from("OK")), AckStatus::Ok);
190+
assert_eq!(
191+
AckStatus::from(CheetahString::from("NO_EXIST")),
192+
AckStatus::NotExist
193+
);
194+
assert_eq!(
195+
AckStatus::from(CheetahString::from("UNKNOWN")),
196+
AckStatus::default()
197+
);
198+
}
199+
200+
#[test]
201+
fn ack_status_to_string() {
202+
assert_eq!(String::from(AckStatus::Ok), "OK".to_string());
203+
assert_eq!(String::from(AckStatus::NotExist), "NO_EXIST".to_string());
204+
}
205+
206+
#[test]
207+
fn ack_status_from_i32_conversion() {
208+
assert_eq!(i32::from(AckStatus::Ok), 0);
209+
assert_eq!(i32::from(AckStatus::NotExist), 1);
210+
}
211+
212+
#[test]
213+
fn ack_status_from_i32_default() {
214+
assert_eq!(AckStatus::from(2), AckStatus::default());
215+
}
216+
217+
#[test]
218+
fn ack_status_serialize() {
219+
let ok_status = AckStatus::Ok;
220+
let serialized = serde_json::to_string(&ok_status).unwrap();
221+
assert_eq!(serialized, "\"OK\"");
222+
223+
let not_exist_status = AckStatus::NotExist;
224+
let serialized = serde_json::to_string(&not_exist_status).unwrap();
225+
assert_eq!(serialized, "\"NO_EXIST\"");
226+
}
227+
228+
#[test]
229+
fn ack_status_deserialize() {
230+
let ok_status: AckStatus = serde_json::from_str("\"OK\"").unwrap();
231+
assert_eq!(ok_status, AckStatus::Ok);
232+
233+
let not_exist_status: AckStatus = serde_json::from_str("\"NO_EXIST\"").unwrap();
234+
assert_eq!(not_exist_status, AckStatus::NotExist);
235+
}
236+
}

0 commit comments

Comments
 (0)