Skip to main content

rocketmq_protocol/protocol/body/
consume_queue_data.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
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Debug, Clone, Serialize, Deserialize, Default)]
22#[serde(rename_all = "camelCase")]
23pub struct ConsumeQueueData {
24    pub physic_offset: i64,
25    pub physic_size: i32,
26    pub tags_code: i64,
27    pub extend_data_json: Option<CheetahString>,
28    pub bit_map: Option<CheetahString>,
29    pub eval: bool,
30    pub msg: Option<CheetahString>,
31}
32
33impl Display for ConsumeQueueData {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(
36            f,
37            "ConsumeQueueData [physic_offset={}, physic_size={}, tags_code={}, extend_data_json={:?}, bit_map={:?}, \
38             eval={}, msg={:?}]",
39            self.physic_offset,
40            self.physic_size,
41            self.tags_code,
42            self.extend_data_json,
43            self.bit_map,
44            self.eval,
45            self.msg
46        )
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use cheetah_string::CheetahString;
53
54    use super::*;
55
56    #[test]
57    fn consume_queue_data_serialization() {
58        let data = ConsumeQueueData {
59            physic_offset: 123,
60            physic_size: 456,
61            tags_code: 789,
62            extend_data_json: Some(CheetahString::from("extend_data")),
63            bit_map: Some(CheetahString::from("bit_map")),
64            eval: true,
65            msg: Some(CheetahString::from("message")),
66        };
67        let serialized = serde_json::to_string(&data).unwrap();
68        assert_eq!(
69            serialized,
70            r#"{"physicOffset":123,"physicSize":456,"tagsCode":789,"extendDataJson":"extend_data","bitMap":"bit_map","eval":true,"msg":"message"}"#
71        );
72        assert_eq!(
73            data.to_string(),
74            "ConsumeQueueData [physic_offset=123, physic_size=456, tags_code=789, \
75             extend_data_json=Some(\"extend_data\"), bit_map=Some(\"bit_map\"), eval=true, msg=Some(\"message\")]"
76        );
77    }
78
79    #[test]
80    fn consume_queue_data_deserialization() {
81        let json = r#"{"physicOffset":123,"physicSize":456,"tagsCode":789,"extendDataJson":"extend_data","bitMap":"bit_map","eval":true,"msg":"message"}"#;
82        let deserialized: ConsumeQueueData = serde_json::from_str(json).unwrap();
83        assert_eq!(deserialized.physic_offset, 123);
84        assert_eq!(deserialized.physic_size, 456);
85        assert_eq!(deserialized.tags_code, 789);
86        assert_eq!(
87            deserialized.extend_data_json.unwrap(),
88            CheetahString::from("extend_data")
89        );
90        assert_eq!(deserialized.bit_map.unwrap(), CheetahString::from("bit_map"));
91        assert!(deserialized.eval);
92        assert_eq!(deserialized.msg.unwrap(), CheetahString::from("message"));
93    }
94
95    #[test]
96    fn consume_queue_data_deserialization_missing_fields() {
97        let json = r#"{"physicOffset":123,"physicSize":456,"tagsCode":789,"eval":false}"#;
98        let deserialized: ConsumeQueueData = serde_json::from_str(json).unwrap();
99        assert_eq!(deserialized.physic_offset, 123);
100        assert_eq!(deserialized.physic_size, 456);
101        assert_eq!(deserialized.tags_code, 789);
102        assert!(deserialized.extend_data_json.is_none());
103        assert!(deserialized.bit_map.is_none());
104        assert!(!deserialized.eval);
105        assert!(deserialized.msg.is_none());
106    }
107}