Skip to main content

confluent_kafka/admin/
transaction_listing.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//! A transaction listing, as reported by `ListTransactions`.
16
17use crate::admin::TransactionState;
18
19/// A transaction listing.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.clients.admin.TransactionListing")]
22pub struct TransactionListing {
23    transactional_id: String,
24    producer_id: i64,
25    transaction_state: TransactionState,
26}
27
28impl TransactionListing {
29    /// Creates a new `TransactionListing`.
30    ///
31    /// # Arguments
32    ///
33    /// - `transactional_id` - the transactional id of the transaction.
34    /// - `producer_id` - the producer id of the transaction.
35    /// - `transaction_state` - the current state of the transaction.
36    #[doc(alias = "org.apache.kafka.clients.admin.TransactionListing#TransactionListing")]
37    pub fn new(transactional_id: impl Into<String>, producer_id: i64, transaction_state: TransactionState) -> Self {
38        Self { transactional_id: transactional_id.into(), producer_id, transaction_state }
39    }
40
41    /// Returns the transactional id of the transaction.
42    #[doc(alias = "org.apache.kafka.clients.admin.TransactionListing#transactionalId")]
43    pub fn transactional_id(&self) -> &str {
44        &self.transactional_id
45    }
46
47    /// Returns the producer id of the transaction.
48    #[doc(alias = "org.apache.kafka.clients.admin.TransactionListing#producerId")]
49    pub fn producer_id(&self) -> i64 {
50        self.producer_id
51    }
52
53    /// Returns the current state of the transaction.
54    #[doc(alias = "org.apache.kafka.clients.admin.TransactionListing#state")]
55    pub fn state(&self) -> TransactionState {
56        self.transaction_state
57    }
58}
59
60impl std::fmt::Display for TransactionListing {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(
63            f,
64            "TransactionListing(transactionalId='{}', producerId={}, transactionState={})",
65            self.transactional_id, self.producer_id, self.transaction_state,
66        )
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn accessors_and_equality() {
76        let a = TransactionListing::new("foo", 12345, TransactionState::Ongoing);
77        assert_eq!(a.transactional_id(), "foo");
78        assert_eq!(a.producer_id(), 12345);
79        assert_eq!(a.state(), TransactionState::Ongoing);
80        assert_eq!(a, TransactionListing::new("foo", 12345, TransactionState::Ongoing));
81        assert_ne!(a, TransactionListing::new("foo", 12345, TransactionState::Empty));
82    }
83}