Skip to main content

rocketmq_client_rust/consumer/
ack_result.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 cheetah_string::CheetahString;
16
17use crate::consumer::ack_status::AckStatus;
18
19#[derive(Debug, Clone, Default)]
20pub struct AckResult {
21    pub(crate) status: AckStatus,
22    pub(crate) extra_info: CheetahString,
23    pub(crate) pop_time: i64,
24}
25
26impl AckResult {
27    pub fn status(&self) -> AckStatus {
28        self.status
29    }
30
31    pub fn extra_info(&self) -> &CheetahString {
32        &self.extra_info
33    }
34
35    pub fn pop_time(&self) -> i64 {
36        self.pop_time
37    }
38}
39
40impl std::fmt::Display for AckResult {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "AckResult [AckStatus={},extraInfo={}]", self.status, self.extra_info)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use cheetah_string::CheetahString;
49
50    use super::*;
51
52    #[test]
53    fn ack_result_display_format() {
54        let ack_result = AckResult {
55            status: AckStatus::Ok,
56            extra_info: CheetahString::from("extra info"),
57            pop_time: 123456789,
58        };
59        assert_eq!(
60            format!("{}", ack_result),
61            "AckResult [AckStatus=OK,extraInfo=extra info]"
62        );
63    }
64
65    #[test]
66    fn ack_result_display_format_with_not_exist_status() {
67        let ack_result = AckResult {
68            status: AckStatus::NotExist,
69            extra_info: CheetahString::from("extra info"),
70            pop_time: 987654321,
71        };
72        assert_eq!(
73            format!("{}", ack_result),
74            "AckResult [AckStatus=NO_EXIST,extraInfo=extra info]"
75        );
76    }
77
78    #[test]
79    fn ack_result_display_format_with_empty_extra_info() {
80        let ack_result = AckResult {
81            status: AckStatus::Ok,
82            extra_info: CheetahString::from(""),
83            pop_time: 123456789,
84        };
85        assert_eq!(format!("{}", ack_result), "AckResult [AckStatus=OK,extraInfo=]");
86    }
87
88    #[test]
89    fn ack_result_display_format_with_negative_pop_time() {
90        let ack_result = AckResult {
91            status: AckStatus::Ok,
92            extra_info: CheetahString::from("extra info"),
93            pop_time: -123456789,
94        };
95        assert_eq!(
96            format!("{}", ack_result),
97            "AckResult [AckStatus=OK,extraInfo=extra info]"
98        );
99    }
100}