Skip to main content

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