Skip to main content

confluent_kafka/admin/options/
describe_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::describe_producers`.
16
17/// Options for `Admin::describe_producers`.
18#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
19#[doc(alias = "org.apache.kafka.clients.admin.DescribeProducersOptions")]
20pub struct DescribeProducersOptions {
21    timeout_ms: Option<i32>,
22    broker_id: Option<i32>,
23}
24
25impl DescribeProducersOptions {
26    /// Creates default options (default API timeout, no broker override).
27    #[doc(alias = "org.apache.kafka.clients.admin.DescribeProducersOptions#DescribeProducersOptions")]
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    /// Set the timeout in milliseconds for this operation, or `None` if the default
33    /// API timeout for the `AdminClient` should be used.
34    ///
35    /// # Arguments
36    ///
37    /// - `timeout_ms` - the timeout in milliseconds, or `None` for the default API timeout.
38    ///
39    /// # Returns
40    ///
41    /// This `DescribeProducersOptions` instance.
42    #[must_use]
43    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
44    pub fn set_timeout_ms(mut self, timeout_ms: Option<i32>) -> Self {
45        self.timeout_ms = timeout_ms;
46        self
47    }
48
49    /// Returns the timeout in milliseconds for this operation, or `None` if the
50    /// default API timeout for the `AdminClient` should be used.
51    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
52    pub fn timeout_ms(&self) -> Option<i32> {
53        self.timeout_ms
54    }
55
56    /// Sets the broker id to query for the topic partitions: the request is sent directly to that
57    /// broker rather than to each partition's leader.
58    ///
59    /// # Arguments
60    ///
61    /// - `broker_id` - the broker id.
62    ///
63    /// # Returns
64    ///
65    /// This `DescribeProducersOptions` instance.
66    #[must_use]
67    #[doc(alias = "org.apache.kafka.clients.admin.DescribeProducersOptions#brokerId")]
68    pub fn set_broker_id(mut self, broker_id: i32) -> Self {
69        self.broker_id = Some(broker_id);
70        self
71    }
72
73    /// Returns the broker id to query, or `None` if not set.
74    #[doc(alias = "org.apache.kafka.clients.admin.DescribeProducersOptions#brokerId")]
75    pub fn broker_id(&self) -> Option<i32> {
76        self.broker_id
77    }
78}
79
80impl std::fmt::Display for DescribeProducersOptions {
81    /// Java's `toString()`: the broker id prints as Java's `OptionalInt`
82    /// (`OptionalInt[3]`, `OptionalInt.empty`) and an unset timeout as `null`.
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        let broker_id = self
85            .broker_id
86            .map_or_else(|| "OptionalInt.empty".to_string(), |id| format!("OptionalInt[{id}]"));
87        let timeout_ms = self.timeout_ms.map_or_else(|| "null".to_string(), |t| t.to_string());
88        write!(f, "DescribeProducersOptions(brokerId={broker_id}, timeoutMs={timeout_ms})")
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn to_string_follows_java() {
98        assert_eq!(
99            DescribeProducersOptions::new().to_string(),
100            "DescribeProducersOptions(brokerId=OptionalInt.empty, timeoutMs=null)"
101        );
102        assert_eq!(
103            DescribeProducersOptions::new()
104                .set_broker_id(3)
105                .set_timeout_ms(Some(500))
106                .to_string(),
107            "DescribeProducersOptions(brokerId=OptionalInt[3], timeoutMs=500)"
108        );
109    }
110
111    #[test]
112    fn defaults_match_java() {
113        let options = DescribeProducersOptions::new();
114        assert_eq!(options.timeout_ms(), None);
115        assert_eq!(options.broker_id(), None);
116    }
117
118    #[test]
119    fn fluent_broker_id_and_timeout() {
120        let options = DescribeProducersOptions::new().set_broker_id(3).set_timeout_ms(Some(5000));
121        assert_eq!(options.broker_id(), Some(3));
122        assert_eq!(options.timeout_ms(), Some(5000));
123    }
124}