Skip to main content

rocketmq_protocol/protocol/header/
recall_message_response_header.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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20/// Response header for message recall operation.
21///
22/// This header is returned by the broker after processing a recall message request.
23/// It contains the message ID of the recalled message.
24#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
25#[serde(rename_all = "camelCase")]
26#[header(
27    type_id = "rocketmq_protocol::protocol::header::recall_message_response_header::RecallMessageResponseHeader",
28    java_class = "org.apache.rocketmq.remoting.protocol.header.RecallMessageResponseHeader"
29)]
30pub struct RecallMessageResponseHeader {
31    /// Message ID of the recalled message (required).
32    #[header(required)]
33    pub msg_id: CheetahString,
34}
35
36impl RecallMessageResponseHeader {
37    pub fn new(msg_id: impl Into<CheetahString>) -> Self {
38        Self { msg_id: msg_id.into() }
39    }
40
41    pub fn msg_id(&self) -> &CheetahString {
42        &self.msg_id
43    }
44
45    pub fn set_msg_id(&mut self, msg_id: impl Into<CheetahString>) {
46        self.msg_id = msg_id.into();
47    }
48}
49
50impl std::fmt::Display for RecallMessageResponseHeader {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "RecallMessageResponseHeader {{ msg_id: {} }}", self.msg_id)
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn display_includes_message_id() {
62        let mut body = RecallMessageResponseHeader::new("initial_message");
63        body.set_msg_id("some_message");
64        let display_output = format!("{}", body);
65        assert_eq!(display_output, "RecallMessageResponseHeader { msg_id: some_message }");
66    }
67    #[test]
68    fn recall_message_serialisation() {
69        let body = RecallMessageResponseHeader::new("some_message");
70
71        let json = serde_json::to_string(&body).unwrap();
72        assert!(json.contains("\"msgId\":\"some_message\""));
73    }
74    #[test]
75    fn recall_message_deserialisation() {
76        let json = r#"{"msgId": "some_message"}"#;
77
78        let body: RecallMessageResponseHeader = serde_json::from_str(json).unwrap();
79        assert_eq!(body.msg_id(), &CheetahString::from("some_message"));
80    }
81}