Skip to main content

rocketmq_protocol/protocol/remoting_command/
accessors.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::collections::HashMap;
16
17use bytes::Bytes;
18use cheetah_string::CheetahString;
19use rocketmq_model::version::RocketMqVersion;
20
21use super::RemotingCommand;
22use super::SerializeType;
23use crate::code::request_code::RequestCode;
24use crate::protocol::LanguageCode;
25
26#[cfg(test)]
27use super::ExtensionFields;
28#[cfg(test)]
29use crate::protocol::header_codec::BinaryHeaderFields;
30
31impl RemotingCommand {
32    pub fn set_code(mut self, code: impl Into<i32>) -> Self {
33        self.code = code.into();
34        self
35    }
36
37    pub fn set_code_ref(&mut self, code: impl Into<i32>) {
38        self.code = code.into();
39    }
40
41    pub fn set_code_mut(&mut self, code: impl Into<i32>) -> &mut Self {
42        self.code = code.into();
43        self
44    }
45
46    pub fn set_language(mut self, language: LanguageCode) -> Self {
47        self.language = language;
48        self
49    }
50
51    pub fn set_version_ref(&mut self, version: i32) {
52        self.version = version;
53    }
54
55    pub fn set_version(mut self, version: i32) -> Self {
56        self.version = version;
57        self
58    }
59
60    #[inline]
61    pub fn set_opaque(mut self, opaque: i32) -> Self {
62        self.opaque = opaque;
63        self
64    }
65
66    #[inline]
67    pub fn set_opaque_mut(&mut self, opaque: i32) {
68        self.opaque = opaque;
69    }
70
71    #[inline]
72    pub fn set_remark_option(mut self, remark: Option<impl Into<CheetahString>>) -> Self {
73        self.remark = remark.map(|item| item.into());
74        self
75    }
76
77    #[inline]
78    pub fn set_remark(mut self, remark: impl Into<CheetahString>) -> Self {
79        self.remark = Some(remark.into());
80        self
81    }
82
83    #[inline]
84    pub fn set_remark_option_mut(&mut self, remark: Option<impl Into<CheetahString>>) {
85        self.remark = remark.map(|item| item.into());
86    }
87
88    #[inline]
89    pub fn set_remark_mut(&mut self, remark: impl Into<CheetahString>) {
90        self.remark = Some(remark.into());
91    }
92
93    #[inline]
94    pub fn set_ext_fields(mut self, ext_fields: HashMap<CheetahString, CheetahString>) -> Self {
95        self.ext_fields.replace_map(ext_fields);
96        self.custom_header_to_net = false;
97        self
98    }
99
100    #[cfg(test)]
101    pub(super) fn set_binary_ext_fields(mut self, ext_fields: BinaryHeaderFields) -> Self {
102        self.ext_fields = ExtensionFields::from_rocketmq_raw(ext_fields);
103        self.custom_header_to_net = false;
104        self
105    }
106
107    #[inline]
108    pub fn set_body(mut self, body: impl Into<Bytes>) -> Self {
109        self.body = Some(body.into());
110        self
111    }
112
113    #[inline]
114    pub fn set_body_mut_ref(&mut self, body: impl Into<Bytes>) {
115        self.body = Some(body.into());
116    }
117
118    #[inline]
119    pub fn set_suspended(mut self, suspended: bool) -> Self {
120        self.suspended = suspended;
121        self
122    }
123
124    #[inline]
125    pub fn set_suspended_ref(&mut self, suspended: bool) {
126        self.suspended = suspended;
127    }
128
129    #[inline]
130    pub fn set_serialize_type(mut self, serialize_type: SerializeType) -> Self {
131        self.serialize_type = serialize_type;
132        self
133    }
134
135    #[inline]
136    pub(crate) fn set_serialize_type_ref(&mut self, serialize_type: SerializeType) {
137        self.serialize_type = serialize_type;
138    }
139
140    #[inline]
141    pub fn get_body(&self) -> Option<&Bytes> {
142        self.body.as_ref()
143    }
144
145    #[inline]
146    pub fn get_body_mut(&mut self) -> Option<&mut Bytes> {
147        self.body.as_mut()
148    }
149
150    #[inline]
151    pub fn code(&self) -> i32 {
152        self.code
153    }
154
155    #[inline]
156    pub fn request_code(&self) -> RequestCode {
157        RequestCode::from(self.code)
158    }
159
160    #[inline]
161    pub fn code_ref(&self) -> &i32 {
162        &self.code
163    }
164
165    #[inline]
166    pub fn language(&self) -> LanguageCode {
167        self.language
168    }
169
170    #[inline]
171    pub fn version(&self) -> i32 {
172        self.version
173    }
174
175    pub fn rocketmq_version(&self) -> RocketMqVersion {
176        RocketMqVersion::from_ordinal(self.version as u32)
177    }
178
179    #[inline]
180    pub fn opaque(&self) -> i32 {
181        self.opaque
182    }
183
184    #[inline]
185    pub fn remark(&self) -> Option<&CheetahString> {
186        self.remark.as_ref()
187    }
188
189    #[inline]
190    pub fn ext_fields(&self) -> Option<&HashMap<CheetahString, CheetahString>> {
191        self.ext_fields.as_map()
192    }
193
194    #[inline]
195    pub fn body(&self) -> Option<&Bytes> {
196        self.body.as_ref()
197    }
198
199    #[inline]
200    pub fn take_body(&mut self) -> Option<Bytes> {
201        self.body.take()
202    }
203
204    #[inline]
205    pub fn suspended(&self) -> bool {
206        self.suspended
207    }
208
209    #[inline]
210    pub fn serialize_type(&self) -> SerializeType {
211        self.serialize_type
212    }
213
214    #[inline]
215    pub fn with_opaque(&mut self, opaque: i32) -> &mut Self {
216        self.opaque = opaque;
217        self
218    }
219
220    pub fn add_ext_field(&mut self, key: impl Into<CheetahString>, value: impl Into<CheetahString>) -> &mut Self {
221        self.ext_fields.get_or_insert_map().insert(key.into(), value.into());
222        self
223    }
224
225    #[inline]
226    pub fn with_code(&mut self, code: impl Into<i32>) -> &mut Self {
227        self.code = code.into();
228        self
229    }
230
231    #[inline]
232    pub fn with_remark(&mut self, remark: impl Into<CheetahString>) -> &mut Self {
233        self.remark = Some(remark.into());
234        self
235    }
236
237    #[inline]
238    pub fn get_ext_fields(&self) -> Option<&HashMap<CheetahString, CheetahString>> {
239        self.ext_fields.as_map()
240    }
241
242    #[inline]
243    pub fn add_ext_field_if_not_exist(&mut self, key: impl Into<CheetahString>, value: impl Into<CheetahString>) {
244        self.ext_fields
245            .get_or_insert_map()
246            .entry(key.into())
247            .or_insert(value.into());
248    }
249
250    /// Ensures the extension fields map is initialized.
251    ///
252    /// If `ext_fields` is `None`, initializes it with an empty `HashMap`.
253    /// This method is idempotent and safe to call multiple times.
254    #[inline]
255    pub fn ensure_ext_fields_initialized(&mut self) {
256        if self.ext_fields.is_absent() {
257            let _ = self.ext_fields.get_or_insert_map();
258        }
259    }
260}
261
262impl AsRef<RemotingCommand> for RemotingCommand {
263    #[inline]
264    fn as_ref(&self) -> &RemotingCommand {
265        self
266    }
267}
268
269impl AsMut<RemotingCommand> for RemotingCommand {
270    #[inline]
271    fn as_mut(&mut self) -> &mut RemotingCommand {
272        self
273    }
274}