Skip to main content

rocketmq_client_rust/producer/
local_transaction_state.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;
16use std::fmt::Display;
17
18use serde::Deserialize;
19use serde::Deserializer;
20use serde::Serialize;
21use serde::Serializer;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum LocalTransactionState {
25    #[default]
26    CommitMessage,
27    RollbackMessage,
28    //java is UNKNOW
29    Unknown,
30}
31
32impl Serialize for LocalTransactionState {
33    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
34    where
35        S: Serializer,
36    {
37        let value = match self {
38            LocalTransactionState::CommitMessage => "COMMIT_MESSAGE",
39            LocalTransactionState::RollbackMessage => "ROLLBACK_MESSAGE",
40            LocalTransactionState::Unknown => "UNKNOW",
41        };
42        serializer.serialize_str(value)
43    }
44}
45
46impl<'de> Deserialize<'de> for LocalTransactionState {
47    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
48    where
49        D: Deserializer<'de>,
50    {
51        struct StoreTypeVisitor;
52
53        impl serde::de::Visitor<'_> for StoreTypeVisitor {
54            type Value = LocalTransactionState;
55
56            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
57                formatter.write_str("a string representing SendStatus")
58            }
59
60            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
61            where
62                E: serde::de::Error,
63            {
64                match value {
65                    "COMMIT_MESSAGE" => Ok(LocalTransactionState::CommitMessage),
66                    "ROLLBACK_MESSAGE" => Ok(LocalTransactionState::RollbackMessage),
67                    "UNKNOW" | "UNKNOWN" => Ok(LocalTransactionState::Unknown),
68                    _ => Err(serde::de::Error::unknown_variant(
69                        value,
70                        &["COMMIT_MESSAGE", "ROLLBACK_MESSAGE", "UNKNOWN", "UNKNOW"],
71                    )),
72                }
73            }
74        }
75        deserializer.deserialize_str(StoreTypeVisitor)
76    }
77}
78
79impl Display for LocalTransactionState {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        match self {
82            LocalTransactionState::CommitMessage => write!(f, "COMMIT_MESSAGE"),
83            LocalTransactionState::RollbackMessage => write!(f, "ROLLBACK_MESSAGE"),
84            LocalTransactionState::Unknown => write!(f, "UNKNOW"),
85        }
86    }
87}