Skip to main content

rocketmq_common/common/message/
message_client_ext.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::any::Any;
16use std::collections::HashMap;
17use std::fmt::Display;
18use std::fmt::Formatter;
19
20use bytes::Bytes;
21use cheetah_string::CheetahString;
22
23use crate::common::message::message_client_id_setter::MessageClientIDSetter;
24use crate::common::message::message_ext::MessageExt;
25use crate::common::message::MessageTrait;
26
27#[derive(Clone, Debug, Default)]
28pub struct MessageClientExt {
29    pub message_ext_inner: MessageExt,
30}
31
32impl MessageClientExt {
33    pub fn new(message: MessageExt) -> Self {
34        Self {
35            message_ext_inner: message,
36        }
37    }
38
39    pub fn get_offset_msg_id(&self) -> &str {
40        self.message_ext_inner.msg_id()
41    }
42
43    pub fn set_offset_msg_id(&mut self, offset_msg_id: impl Into<CheetahString>) {
44        self.message_ext_inner.set_msg_id(offset_msg_id.into());
45    }
46
47    pub fn get_msg_id(&self) -> CheetahString {
48        let uniq_id = MessageClientIDSetter::get_uniq_id(&self.message_ext_inner);
49        if let Some(uniq_id) = uniq_id {
50            uniq_id
51        } else {
52            self.message_ext_inner.msg_id().clone()
53        }
54    }
55}
56
57impl Display for MessageClientExt {
58    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59        write!(
60            f,
61            "MessageClientExt {{ message_ext_inner: {:?} }}",
62            self.message_ext_inner
63        )
64    }
65}
66
67impl MessageTrait for MessageClientExt {
68    #[inline]
69    fn put_property(&mut self, key: CheetahString, value: CheetahString) {
70        self.message_ext_inner.put_property(key, value);
71    }
72
73    #[inline]
74    fn clear_property(&mut self, name: &str) {
75        self.message_ext_inner.clear_property(name);
76    }
77
78    #[inline]
79    fn property(&self, name: &CheetahString) -> Option<CheetahString> {
80        self.message_ext_inner.property(name)
81    }
82
83    fn property_ref(&self, name: &CheetahString) -> Option<&CheetahString> {
84        self.message_ext_inner.property_ref(name)
85    }
86
87    #[inline]
88    fn topic(&self) -> &CheetahString {
89        self.message_ext_inner.topic()
90    }
91
92    #[inline]
93    fn set_topic(&mut self, topic: CheetahString) {
94        self.message_ext_inner.set_topic(topic);
95    }
96
97    #[inline]
98    fn get_flag(&self) -> i32 {
99        self.message_ext_inner.get_flag()
100    }
101
102    #[inline]
103    fn set_flag(&mut self, flag: i32) {
104        self.message_ext_inner.set_flag(flag);
105    }
106
107    #[inline]
108    fn get_body(&self) -> Option<&Bytes> {
109        self.message_ext_inner.get_body()
110    }
111
112    #[inline]
113    fn set_body(&mut self, body: Bytes) {
114        self.message_ext_inner.set_body(body);
115    }
116
117    #[inline]
118    fn get_properties(&self) -> &HashMap<CheetahString, CheetahString> {
119        self.message_ext_inner.get_properties()
120    }
121
122    #[inline]
123    fn set_properties(&mut self, properties: HashMap<CheetahString, CheetahString>) {
124        self.message_ext_inner.set_properties(properties);
125    }
126
127    #[inline]
128    fn transaction_id(&self) -> Option<&CheetahString> {
129        self.message_ext_inner.transaction_id()
130    }
131
132    #[inline]
133    fn set_transaction_id(&mut self, transaction_id: CheetahString) {
134        self.message_ext_inner.set_transaction_id(transaction_id);
135    }
136
137    #[inline]
138    fn get_compressed_body_mut(&mut self) -> Option<&mut Bytes> {
139        self.message_ext_inner.get_compressed_body_mut()
140    }
141
142    #[inline]
143    fn get_compressed_body(&self) -> Option<&Bytes> {
144        self.message_ext_inner.get_compressed_body()
145    }
146
147    #[inline]
148    fn set_compressed_body_mut(&mut self, compressed_body: Bytes) {
149        self.message_ext_inner.set_compressed_body_mut(compressed_body);
150    }
151
152    #[inline]
153    fn take_body(&mut self) -> Option<Bytes> {
154        self.message_ext_inner.take_body()
155    }
156
157    #[inline]
158    fn as_any(&self) -> &dyn Any {
159        self
160    }
161
162    #[inline]
163    fn as_any_mut(&mut self) -> &mut dyn Any {
164        self
165    }
166}