Skip to main content

confluent_kafka/admin/
transaction_state.rs

1// Copyright 2025 Confluent Inc.
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
15//! The state of a transaction, as reported by `DescribeTransactions`.
16
17/// The state of a transaction.
18#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
19#[non_exhaustive]
20#[doc(alias = "org.apache.kafka.clients.admin.TransactionState")]
21pub enum TransactionState {
22    /// A transaction is in progress.
23    Ongoing,
24    /// The transaction is being aborted.
25    PrepareAbort,
26    /// The transaction is being committed.
27    PrepareCommit,
28    /// The transaction has been aborted.
29    CompleteAbort,
30    /// The transaction has been committed.
31    CompleteCommit,
32    /// No transaction is in progress.
33    Empty,
34    /// The producer epoch is being fenced.
35    PrepareEpochFence,
36    /// The transaction state could not be recognized.
37    Unknown,
38}
39
40impl TransactionState {
41    /// The wire/display name of this state (mirrors the Java enum's `name`
42    /// field and `toString`).
43    fn name(&self) -> &'static str {
44        match self {
45            TransactionState::Ongoing => "Ongoing",
46            TransactionState::PrepareAbort => "PrepareAbort",
47            TransactionState::PrepareCommit => "PrepareCommit",
48            TransactionState::CompleteAbort => "CompleteAbort",
49            TransactionState::CompleteCommit => "CompleteCommit",
50            TransactionState::Empty => "Empty",
51            TransactionState::PrepareEpochFence => "PrepareEpochFence",
52            TransactionState::Unknown => "Unknown",
53        }
54    }
55
56    /// Parses a state name, returning [`TransactionState::Unknown`] for any
57    /// unrecognized value.
58    ///
59    /// # Arguments
60    ///
61    /// - `name` - the state name.
62    #[doc(alias = "org.apache.kafka.clients.admin.TransactionState#parse")]
63    pub fn parse(name: &str) -> TransactionState {
64        match name {
65            "Ongoing" => TransactionState::Ongoing,
66            "PrepareAbort" => TransactionState::PrepareAbort,
67            "PrepareCommit" => TransactionState::PrepareCommit,
68            "CompleteAbort" => TransactionState::CompleteAbort,
69            "CompleteCommit" => TransactionState::CompleteCommit,
70            "Empty" => TransactionState::Empty,
71            "PrepareEpochFence" => TransactionState::PrepareEpochFence,
72            _ => TransactionState::Unknown,
73        }
74    }
75}
76
77impl std::fmt::Display for TransactionState {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        write!(f, "{}", self.name())
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn parse_round_trips_known_states() {
89        for state in [
90            TransactionState::Ongoing,
91            TransactionState::PrepareAbort,
92            TransactionState::PrepareCommit,
93            TransactionState::CompleteAbort,
94            TransactionState::CompleteCommit,
95            TransactionState::Empty,
96            TransactionState::PrepareEpochFence,
97            TransactionState::Unknown,
98        ] {
99            assert_eq!(TransactionState::parse(&state.to_string()), state);
100        }
101    }
102
103    #[test]
104    fn parse_unknown_name_is_unknown() {
105        assert_eq!(TransactionState::parse("NotAState"), TransactionState::Unknown);
106    }
107
108    #[test]
109    fn display_matches_java() {
110        assert_eq!(TransactionState::CompleteCommit.to_string(), "CompleteCommit");
111        assert_eq!(TransactionState::PrepareEpochFence.to_string(), "PrepareEpochFence");
112    }
113}