1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use cheetah_string::CheetahString;
use crate::common::message::message_single::Message;
use crate::common::message::MessageConst;
use crate::common::message::MessageTrait;
pub struct MessageAccessor;
impl MessageAccessor {
/// Sets the properties of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `properties` - A `HashMap` containing the properties to set.
#[inline]
pub fn set_properties<T: MessageTrait>(msg: &mut T, properties: HashMap<CheetahString, CheetahString>) {
msg.set_properties(properties);
}
/// Puts a property into a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `name` - The name of the property.
/// * `value` - The value of the property.
#[inline]
pub fn put_property<T: MessageTrait>(msg: &mut T, name: CheetahString, value: CheetahString) {
msg.put_property(name, value);
}
/// Clears a property from a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `name` - The name of the property to clear.
#[inline]
pub fn clear_property<T: MessageTrait>(msg: &mut T, name: &str) {
msg.clear_property(name);
}
/// Sets the transfer flag of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `unit` - The transfer flag value.
#[inline]
pub fn set_transfer_flag<T: MessageTrait>(msg: &mut T, unit: CheetahString) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_TRANSFER_FLAG),
unit,
);
}
/// Gets the transfer flag of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The transfer flag value if it exists.
#[inline]
pub fn get_transfer_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(MessageConst::PROPERTY_TRANSFER_FLAG))
}
/// Sets the correction flag of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `unit` - The correction flag value.
#[inline]
pub fn set_correction_flag<T: MessageTrait>(msg: &mut T, unit: CheetahString) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_CORRECTION_FLAG),
unit,
);
}
/// Gets the correction flag of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The correction flag value if it exists.
#[inline]
pub fn get_correction_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(MessageConst::PROPERTY_CORRECTION_FLAG))
}
/// Sets the origin message ID of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `origin_message_id` - The origin message ID value.
#[inline]
pub fn set_origin_message_id<T: MessageTrait>(msg: &mut T, origin_message_id: CheetahString) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_ORIGIN_MESSAGE_ID),
origin_message_id,
);
}
/// Gets the origin message ID of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The origin message ID value if it exists.
#[inline]
pub fn get_origin_message_id<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(
MessageConst::PROPERTY_ORIGIN_MESSAGE_ID,
))
}
/// Sets the MQ2 flag of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `flag` - The MQ2 flag value.
#[inline]
pub fn set_mq2_flag<T: MessageTrait>(msg: &mut T, flag: CheetahString) {
msg.put_property(CheetahString::from_static_str(MessageConst::PROPERTY_MQ2_FLAG), flag);
}
/// Gets the MQ2 flag of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The MQ2 flag value if it exists.
#[inline]
pub fn get_mq2_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(MessageConst::PROPERTY_MQ2_FLAG))
}
/// Sets the reconsume time of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `reconsume_times` - The reconsume time value.
#[inline]
pub fn set_reconsume_time<T: MessageTrait>(msg: &mut T, reconsume_times: CheetahString) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_RECONSUME_TIME),
reconsume_times,
);
}
/// Gets the reconsume time of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The reconsume time value if it exists.
#[inline]
pub fn get_reconsume_time<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(MessageConst::PROPERTY_RECONSUME_TIME))
}
/// Sets the maximum reconsume times of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `max_reconsume_times` - The maximum reconsume times value.
#[inline]
pub fn set_max_reconsume_times<T: MessageTrait>(msg: &mut T, max_reconsume_times: CheetahString) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_MAX_RECONSUME_TIMES),
max_reconsume_times,
);
}
/// Gets the maximum reconsume times of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The maximum reconsume times value if it exists.
#[inline]
pub fn get_max_reconsume_times<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(
MessageConst::PROPERTY_MAX_RECONSUME_TIMES,
))
}
/// Sets the consume start timestamp of a message.
///
/// # Arguments
///
/// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
/// * `property_consume_start_time_stamp` - The consume start timestamp value.
#[inline]
pub fn set_consume_start_time_stamp<T: MessageTrait>(
msg: &mut T,
property_consume_start_time_stamp: CheetahString,
) {
msg.put_property(
CheetahString::from_static_str(MessageConst::PROPERTY_CONSUME_START_TIMESTAMP),
property_consume_start_time_stamp,
);
}
/// Gets the consume start timestamp of a message.
///
/// # Arguments
///
/// * `msg` - A reference to a message implementing the `MessageTrait`.
///
/// # Returns
///
/// * `Option<String>` - The consume start timestamp value if it exists.
#[inline]
pub fn get_consume_start_time_stamp<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
msg.property(&CheetahString::from_static_str(
MessageConst::PROPERTY_CONSUME_START_TIMESTAMP,
))
}
/// Clones a message.
pub fn clone_message<M>(message: &M) -> Message
where
M: MessageTrait,
{
let mut new_message = Message::default();
new_message.set_topic(message.topic().clone());
if let Some(body) = message.get_body() {
new_message.set_body(Some(body.clone()));
}
new_message.set_flag(message.get_flag());
new_message.set_properties(message.get_properties().clone());
new_message
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
#[test]
fn clone_message_preserves_fields_and_copies_properties() {
let mut source = Message::default();
source.set_topic("topic".into());
source.set_body(Some(Bytes::from_static(b"body")));
source.set_flag(7);
source.put_property("key".into(), "value".into());
source.put_property("other".into(), "retained".into());
let cloned = MessageAccessor::clone_message(&source);
assert_eq!(cloned.topic(), source.topic());
assert_eq!(cloned.get_body(), source.get_body());
assert_eq!(cloned.get_flag(), source.get_flag());
assert_eq!(cloned.get_properties(), source.get_properties());
source.put_property("key".into(), "changed".into());
assert_eq!(cloned.property("key"), Some("value"));
}
#[test]
fn clone_message_without_body_retains_topic_and_properties() {
let mut source = Message::default();
source.set_topic("topic".into());
source.put_property("key".into(), "value".into());
let cloned = MessageAccessor::clone_message(&source);
assert_eq!(cloned.get_body(), None);
assert_eq!(cloned.topic(), source.topic());
assert_eq!(cloned.get_properties(), source.get_properties());
}
}