rocketmq_protocol/protocol/body/topic_info_wrapper/
topic_queue_wrapper.rs1use std::collections::HashMap;
16
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
22use crate::protocol::DataVersion;
23
24#[derive(Clone, Debug, Serialize, Deserialize, Default)]
25#[serde(rename_all = "camelCase")]
26pub struct TopicQueueMappingSerializeWrapper {
27 topic_queue_mapping_info_map: Option<HashMap<CheetahString, TopicQueueMappingDetail>>,
28 data_version: Option<DataVersion>,
29}
30
31impl TopicQueueMappingSerializeWrapper {
32 pub fn new(
33 topic_queue_mapping_info_map: Option<HashMap<CheetahString, TopicQueueMappingDetail>>,
34 data_version: Option<DataVersion>,
35 ) -> Self {
36 Self {
37 topic_queue_mapping_info_map,
38 data_version,
39 }
40 }
41}
42
43impl TopicQueueMappingSerializeWrapper {
44 pub fn topic_queue_mapping_info_map(&self) -> Option<&HashMap<CheetahString, TopicQueueMappingDetail>> {
45 self.topic_queue_mapping_info_map.as_ref()
46 }
47
48 pub fn take_topic_queue_mapping_info_map(&mut self) -> Option<HashMap<CheetahString, TopicQueueMappingDetail>> {
49 self.topic_queue_mapping_info_map.take()
50 }
51
52 pub fn data_version(&self) -> Option<&DataVersion> {
53 match &self.data_version {
54 None => None,
55 Some(value) => Some(value),
56 }
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 fn create_test_map() -> HashMap<CheetahString, TopicQueueMappingDetail> {
65 let mut map = HashMap::new();
66 let detail = TopicQueueMappingDetail::default();
67 map.insert(CheetahString::from("test_topic"), detail);
68 map
69 }
70
71 #[test]
72 fn test_take_topic_queue_mapping_info_map() {
73 let map = create_test_map();
74 let mut wrapper = TopicQueueMappingSerializeWrapper::new(Some(map), None);
75
76 assert!(wrapper.data_version().is_none());
77 let taken_map = wrapper.take_topic_queue_mapping_info_map();
78
79 assert!(taken_map.is_some());
80 assert_eq!(taken_map.unwrap().len(), 1);
81 assert!(wrapper.topic_queue_mapping_info_map().is_none());
82 }
83
84 #[test]
85 fn test_serialization_deserialization() {
86 let map = create_test_map();
87 let version = DataVersion::default();
88 let wrapper = TopicQueueMappingSerializeWrapper::new(Some(map), Some(version));
89
90 let json = serde_json::to_string(&wrapper).expect("Should serialize");
91
92 assert!(json.contains("topicQueueMappingInfoMap"));
93 assert!(json.contains("dataVersion"));
94
95 let deserialized: TopicQueueMappingSerializeWrapper = serde_json::from_str(&json).expect("Should deserialize");
96
97 assert_eq!(deserialized.topic_queue_mapping_info_map().unwrap().len(), 1);
98 }
99}