Skip to main content

confluent_kafka/admin/options/
terminate_transaction_options.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//! Options for `Admin::force_terminate_transaction`.
16
17/// Options for `Admin::force_terminate_transaction`.
18#[derive(Clone, Debug, Default)]
19#[doc(alias = "org.apache.kafka.clients.admin.TerminateTransactionOptions")]
20pub struct TerminateTransactionOptions {
21    timeout_ms: Option<i32>,
22}
23
24impl TerminateTransactionOptions {
25    /// Creates default options (default API timeout).
26    #[doc(alias = "org.apache.kafka.clients.admin.TerminateTransactionOptions#TerminateTransactionOptions")]
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Set the timeout in milliseconds for this operation, or `None` if the default
32    /// API timeout for the `AdminClient` should be used.
33    ///
34    /// # Arguments
35    ///
36    /// - `timeout_ms` - the timeout in milliseconds, or `None` for the default API timeout.
37    ///
38    /// # Returns
39    ///
40    /// This `TerminateTransactionOptions` instance.
41    #[must_use]
42    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
43    pub fn set_timeout_ms(mut self, timeout_ms: Option<i32>) -> Self {
44        self.timeout_ms = timeout_ms;
45        self
46    }
47
48    /// Returns the timeout in milliseconds for this operation, or `None` if the
49    /// default API timeout for the `AdminClient` should be used.
50    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
51    pub fn timeout_ms(&self) -> Option<i32> {
52        self.timeout_ms
53    }
54}
55
56impl std::fmt::Display for TerminateTransactionOptions {
57    /// Java's `toString()`: an unset timeout prints as `null`.
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        let timeout_ms = self.timeout_ms.map_or_else(|| "null".to_string(), |t| t.to_string());
60        write!(f, "TerminateTransactionOptions{{timeoutMs={timeout_ms}}}")
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn to_string_follows_java() {
70        assert_eq!(
71            TerminateTransactionOptions::new().to_string(),
72            "TerminateTransactionOptions{timeoutMs=null}"
73        );
74        assert_eq!(
75            TerminateTransactionOptions::new().set_timeout_ms(Some(500)).to_string(),
76            "TerminateTransactionOptions{timeoutMs=500}"
77        );
78    }
79
80    #[test]
81    fn defaults_match_java() {
82        assert_eq!(TerminateTransactionOptions::new().timeout_ms(), None);
83    }
84}