Skip to main content

rocketmq_remoting/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::RequestHeaderCodecV2;
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, RequestHeaderCodecV2)]
25#[serde(rename_all = "camelCase")]
26pub struct RecallMessageResponseHeader {
27    /// Message ID of the recalled message (required).
28    #[required]
29    pub msg_id: CheetahString,
30}
31
32impl RecallMessageResponseHeader {
33    pub fn new(msg_id: impl Into<CheetahString>) -> Self {
34        Self { msg_id: msg_id.into() }
35    }
36
37    pub fn msg_id(&self) -> &CheetahString {
38        &self.msg_id
39    }
40
41    pub fn set_msg_id(&mut self, msg_id: impl Into<CheetahString>) {
42        self.msg_id = msg_id.into();
43    }
44}
45
46impl std::fmt::Display for RecallMessageResponseHeader {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "RecallMessageResponseHeader {{ msg_id: {} }}", self.msg_id)
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn new_with_msg_id() {
58        let mut body = RecallMessageResponseHeader::new("some_message");
59        assert_eq!(body.msg_id(), &CheetahString::from("some_message"));
60
61        body.set_msg_id("some_new_message");
62        assert_eq!(body.msg_id(), &CheetahString::from("some_new_message"));
63
64        let display_output = format!("{}", body);
65        assert_eq!(
66            display_output,
67            "RecallMessageResponseHeader { msg_id: some_new_message }"
68        );
69    }
70    #[test]
71    fn recall_message_serialisation() {
72        let body = RecallMessageResponseHeader::new("some_message");
73
74        let json = serde_json::to_string(&body).unwrap();
75        assert!(json.contains("\"msgId\":\"some_message\""));
76    }
77    #[test]
78    fn recall_message_deserialisation() {
79        let json = r#"{"msgId": "some_message"}"#;
80
81        let body: RecallMessageResponseHeader = serde_json::from_str(json).unwrap();
82        assert_eq!(body.msg_id(), &CheetahString::from("some_message"));
83    }
84    #[test]
85    fn recall_message_clone() {
86        let body = RecallMessageResponseHeader::new("some_message");
87        let cloned = body.clone();
88        assert_eq!(body.msg_id(), cloned.msg_id());
89    }
90}