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
// 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::any::Any;
use std::collections::HashMap;
use cheetah_string::CheetahString;
use crate::protocol::header_codec::HeaderFieldSource;
use crate::protocol::header_codec::ResolvedHeaderKey;
use crate::rocketmq_serializable::RocketMQSerializable;
use crate::ProtocolContractViolation;
/// Wire-format extension fields carried by a RocketMQ remoting command.
pub type HeaderMap = HashMap<CheetahString, CheetahString>;
/// Encoding paths available for a custom header.
///
/// This capability is intentionally limited to encoding. Legacy fast decode
/// remains a separate compatibility concern until all handwritten decoders
/// have migrated to the typed [`crate::HeaderCodec`] path.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum HeaderEncodeCapability {
/// Materialize the header into extension fields before serialization.
#[default]
MapOnly,
/// Encode the header directly into the ROCKETMQ extension-field payload.
DirectBinary,
}
pub trait CommandCustomHeader: AsAny {
/// Checks the fields of the implementing type.
///
/// Returns a typed result indicating whether the fields are valid.
///
/// If the fields are valid, the `Ok` variant is returned with an empty `()` value.
/// If the fields are invalid, an error governed by a stable
/// [`rocketmq_error::ErrorDescriptor`] is returned.
fn check_fields(&self) -> rocketmq_error::Result<()> {
Ok(())
}
/// Converts the implementing type to a map.
///
/// Returns an `Option` that contains a `HashMap` of string keys and string values,
/// representing the implementing type's fields.
/// If the conversion is successful, a map is returned; explicitly audited
/// fieldless headers return an empty map. If conversion fails, `None` is
/// returned.
fn to_map(&self) -> Option<HashMap<CheetahString, CheetahString>>;
/// Appends this header's extension fields to an existing map.
///
/// Generated implementations write directly into `out`. The default preserves compatibility
/// for handwritten implementations.
fn encode_into_map(&self, out: &mut HeaderMap) {
if let Some(fields) = self.to_map() {
out.extend(fields);
}
}
/// Validates and appends this header's fields to an existing map.
///
/// Typed codecs override this method to preserve classified failures. The
/// default adapts handwritten implementations without changing their object-safe
/// trait surface or silently treating `None` as a successful conversion.
///
/// # Errors
///
/// Returns [`ProtocolContractViolation::LegacyValidation`] when `check_fields`
/// fails, or [`ProtocolContractViolation::LegacyMapConversionFailed`] when a legacy
/// `to_map` implementation returns `None`.
fn try_encode_into_map(&self, out: &mut HeaderMap) -> Result<(), ProtocolContractViolation> {
self.check_fields()
.map_err(|_| ProtocolContractViolation::LegacyValidation {
header: std::any::type_name::<Self>(),
})?;
let fields = self
.to_map()
.ok_or(ProtocolContractViolation::LegacyMapConversionFailed {
header: std::any::type_name::<Self>(),
})?;
out.extend(fields);
Ok(())
}
/// Resolves a canonical key or decode alias for typed headers.
fn resolve_wire_key(&self, _key: &str) -> Option<ResolvedHeaderKey> {
None
}
/// Resolves a key and returns its canonical wire key.
fn canonical_wire_key(&self, key: &str) -> Option<&'static str> {
self.resolve_wire_key(key).map(|resolved| resolved.canonical)
}
/// Returns whether a canonical key or alias belongs to this header schema.
fn contains_wire_key(&self, key: &str) -> bool {
self.resolve_wire_key(key).is_some()
}
/// Returns a saturating extension-field payload capacity hint.
fn encoded_len_hint(&self) -> usize {
0
}
/// Returns the authoritative encoding path for this header.
fn encode_capability(&self) -> HeaderEncodeCapability {
HeaderEncodeCapability::MapOnly
}
/// Appends this header directly to a ROCKETMQ extension-field payload.
///
/// Implementations must require only shared access so cloned remoting
/// commands can encode the same immutable header. Callers must check
/// [`Self::encode_capability`] before invoking this method. Implementations
/// are responsible for validating the header before the first write; the
/// frame entrypoint does not pre-validate because map and binary encoders
/// already own the authoritative validation boundary.
///
/// # Errors
///
/// Returns a classified header codec error when validation or binary
/// representation fails. The default reports that direct encoding is
/// unavailable.
fn encode_direct_binary(&self, _out: &mut bytes::BytesMut) -> Result<(), ProtocolContractViolation> {
Err(ProtocolContractViolation::FastCodecUnavailable {
header: std::any::type_name::<Self>(),
})
}
/// Returns whether this header can write its complete JSON extension-field
/// object without first materializing a [`HeaderMap`].
fn supports_direct_json_fields(&self) -> bool {
false
}
/// Writes this header as one complete JSON extension-field object.
///
/// Callers must check [`Self::supports_direct_json_fields`] before invoking
/// this method. Implementations must validate before writing and must leave
/// rollback to the enclosing frame encoder.
///
/// # Errors
///
/// Returns a classified header codec error when direct JSON encoding is
/// unavailable or validation fails.
fn encode_direct_json_fields(&self, _out: &mut bytes::BytesMut) -> Result<(), ProtocolContractViolation> {
Err(ProtocolContractViolation::FastCodecUnavailable {
header: std::any::type_name::<Self>(),
})
}
/// Writes the provided `key` to the `out` buffer if the `value` is not empty.
///
/// # Arguments
///
/// * `out` - A mutable reference to a `BytesMut` buffer where the `key` will be written.
/// * `key` - A string slice that represents the key to be written.
/// * `value` - A string slice that represents the value associated with the key.
///
/// # Behavior
///
/// If `value` is not empty, the function will write the `key` to the `out` buffer twice,
/// first with a short length prefix and then with a long length prefix.
fn write_if_not_null(&self, out: &mut bytes::BytesMut, key: &str, value: &str) {
if !value.is_empty() {
RocketMQSerializable::write_str(out, true, key);
RocketMQSerializable::write_str(out, false, value);
}
}
/// Compatibility adapter for the legacy mutable fast-encoding API.
///
/// Production serialization uses [`Self::encode_capability`] and
/// [`Self::encode_direct_binary`] directly. This adapter cannot propagate
/// errors and remains only for existing callers during migration.
fn encode_fast(&mut self, out: &mut bytes::BytesMut) {
let _ = self.encode_direct_binary(out);
}
/// A placeholder function for fast decoding.
///
/// This function currently does nothing and can be overridden by implementing types.
///
/// # Arguments
///
/// * `_fields` - A reference to a `HashMap` that contains the fields to be decoded.
fn decode_fast(&mut self, _fields: &HashMap<CheetahString, CheetahString>) -> rocketmq_error::Result<()> {
Ok(())
}
/// Indicates whether the implementing type supports fast codec.
///
/// # Returns
///
/// This legacy capability is limited to hand-written fast decoding. Direct
/// binary encoding is reported independently by [`Self::encode_capability`].
/// The default remains `false` so enabling generated direct encoding cannot
/// route decoding through an unimplemented legacy hook.
fn support_fast_codec(&self) -> bool {
false
}
/// Retrieves the value associated with the specified field from the provided map.
///
/// # Arguments
///
/// * `map` - A reference to a `HashMap` containing `CheetahString` keys and values.
/// * `field` - A reference to a `CheetahString` representing the field to retrieve.
///
/// # Returns
///
/// * `Ok(CheetahString)` - If the field is found in the map, returns the associated value.
/// * `Err(Error)` - If the field is not found in the map, returns an error indicating the
/// field is required.
///
/// # Errors
///
/// This function returns a serialization error if the specified field is not found in the
/// map.
#[inline(always)]
fn get_and_check_not_none(
&self,
map: &HashMap<CheetahString, CheetahString>,
field: &CheetahString,
) -> rocketmq_error::Result<CheetahString> {
match map.get(field) {
Some(value) => Ok(value.clone()),
None => Err(crate::error::serialization_decode_failed(
"header",
format!("The field {field} is required."),
)),
}
}
/// Retrieves a borrowed value associated with the specified field from the provided map.
#[inline(always)]
fn get_and_check_not_none_ref<'a>(
&self,
map: &'a HashMap<CheetahString, CheetahString>,
field: &CheetahString,
) -> rocketmq_error::Result<&'a CheetahString> {
match map.get(field) {
Some(value) => Ok(value),
None => Err(crate::error::serialization_decode_failed(
"header",
format!("The field {field} is required."),
)),
}
}
}
pub trait AsAny: Any {
fn as_any_mut(&mut self) -> &mut dyn Any;
fn as_any(&self) -> &dyn Any;
}
impl<T: CommandCustomHeader> AsAny for T {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub trait FromMap {
type Error: From<rocketmq_error::Error>;
type Target;
/// Whether production decode may call [`Self::from_field_source`] without
/// first materializing the compatibility map.
const SUPPORTS_HEADER_FIELD_SOURCE: bool = false;
/// Converts the implementing type from a map.
///
/// Returns an instance of `Self::Target` that is created from the provided map.
fn from(map: &HashMap<CheetahString, CheetahString>) -> Result<Self::Target, Self::Error>;
/// Converts the implementing type from a borrowed field source.
///
/// Generated request-header implementations override this method. The default is an
/// additive compatibility fallback for existing implementations and is not
/// selected by production dispatch while the capability remains `false`.
fn from_field_source(source: &dyn HeaderFieldSource) -> Result<Self::Target, Self::Error> {
let map = source.to_header_map();
Self::from(&map)
}
}