Skip to main content

rocketmq_client_rust/producer/
transaction_send_result.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::fmt::Display;
16
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::producer::local_transaction_state::LocalTransactionState;
21use crate::producer::send_result::SendResult;
22
23#[derive(Debug, Serialize, Deserialize, Clone)]
24pub struct TransactionSendResult {
25    pub local_transaction_state: Option<LocalTransactionState>,
26
27    #[serde(flatten)]
28    pub send_result: Option<SendResult>,
29}
30
31impl Display for TransactionSendResult {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self.send_result.as_ref() {
34            Some(send_result) => write!(f, "{send_result}"),
35            None => write!(
36                f,
37                "SendResult [sendStatus=null, msgId=null, offsetMsgId=null, messageQueue=null, queueOffset=0, \
38                 recallHandle=null]"
39            ),
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use cheetah_string::CheetahString;
47    use rocketmq_model::common::message::message_queue::MessageQueue;
48
49    use super::*;
50    use crate::producer::send_status::SendStatus;
51
52    #[test]
53    fn transaction_send_result_display_delegates_to_send_result_like_java() {
54        let mut send_result = SendResult::new(
55            SendStatus::SendOk,
56            Some(CheetahString::from("msg-a")),
57            Some("offset-a".to_string()),
58            Some(MessageQueue::from_parts("TopicA", "BrokerA", 1)),
59            9,
60        );
61        send_result.set_recall_handle("recall-a".to_string());
62
63        let transaction_result = TransactionSendResult {
64            local_transaction_state: Some(LocalTransactionState::CommitMessage),
65            send_result: Some(send_result),
66        };
67
68        assert_eq!(
69            transaction_result.to_string(),
70            "SendResult [sendStatus=SEND_OK, msgId=msg-a, offsetMsgId=offset-a, messageQueue=MessageQueue \
71             [topic=TopicA, brokerName=BrokerA, queueId=1], queueOffset=9, recallHandle=recall-a]"
72        );
73    }
74
75    #[test]
76    fn empty_transaction_send_result_display_matches_java_empty_parent_result() {
77        let transaction_result = TransactionSendResult {
78            local_transaction_state: None,
79            send_result: None,
80        };
81
82        assert_eq!(
83            transaction_result.to_string(),
84            "SendResult [sendStatus=null, msgId=null, offsetMsgId=null, messageQueue=null, queueOffset=0, \
85             recallHandle=null]"
86        );
87    }
88}