Skip to main content

confluent_kafka/admin/options/
create_topics_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::create_topics`.
16
17/// Options for `Admin::create_topics`.
18#[derive(Clone, Debug)]
19#[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions")]
20pub struct CreateTopicsOptions {
21    timeout_ms: Option<i32>,
22    validate_only: bool,
23    retry_on_quota_violation: bool,
24}
25
26impl Default for CreateTopicsOptions {
27    fn default() -> Self {
28        Self { timeout_ms: None, validate_only: false, retry_on_quota_violation: true }
29    }
30}
31
32impl CreateTopicsOptions {
33    /// Creates default options (`validate_only=false`,
34    /// `retry_on_quota_violation=true`, default API timeout).
35    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#CreateTopicsOptions")]
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// Set the timeout in milliseconds for this operation, or `None` if the default
41    /// API timeout for the `AdminClient` should be used.
42    ///
43    /// # Arguments
44    ///
45    /// - `timeout_ms` - the timeout in milliseconds, or `None` for the default API timeout.
46    ///
47    /// # Returns
48    ///
49    /// This `CreateTopicsOptions` instance.
50    #[must_use]
51    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#timeoutMs")]
52    pub fn set_timeout_ms(mut self, timeout_ms: Option<i32>) -> Self {
53        self.timeout_ms = timeout_ms;
54        self
55    }
56
57    /// Returns the timeout in milliseconds for this operation, or `None` if the
58    /// default API timeout for the `AdminClient` should be used.
59    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#timeoutMs")]
60    pub fn timeout_ms(&self) -> Option<i32> {
61        self.timeout_ms
62    }
63
64    /// Set to true if the request should be validated without creating the topic.
65    ///
66    /// # Arguments
67    ///
68    /// - `validate_only` - `true` to only validate the request.
69    ///
70    /// # Returns
71    ///
72    /// This `CreateTopicsOptions` instance.
73    #[must_use]
74    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#validateOnly")]
75    pub fn set_validate_only(mut self, validate_only: bool) -> Self {
76        self.validate_only = validate_only;
77        self
78    }
79
80    /// Returns true if the request should be validated without creating the topic.
81    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#shouldValidateOnly")]
82    pub fn should_validate_only(&self) -> bool {
83        self.validate_only
84    }
85
86    /// Set to true if quota violation should be automatically retried.
87    ///
88    /// # Arguments
89    ///
90    /// - `retry_on_quota_violation` - `true` to retry on quota violation.
91    ///
92    /// # Returns
93    ///
94    /// This `CreateTopicsOptions` instance.
95    #[must_use]
96    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#retryOnQuotaViolation")]
97    pub fn set_retry_on_quota_violation(mut self, retry_on_quota_violation: bool) -> Self {
98        self.retry_on_quota_violation = retry_on_quota_violation;
99        self
100    }
101
102    /// Returns true if quota violation should be automatically retried.
103    #[doc(alias = "org.apache.kafka.clients.admin.CreateTopicsOptions#shouldRetryOnQuotaViolation")]
104    pub fn should_retry_on_quota_violation(&self) -> bool {
105        self.retry_on_quota_violation
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    fn defaults_match_java() {
115        let options = CreateTopicsOptions::new();
116        assert_eq!(options.timeout_ms(), None);
117        assert!(!options.should_validate_only());
118        assert!(options.should_retry_on_quota_violation());
119    }
120
121    #[test]
122    fn fluent_setters() {
123        let options = CreateTopicsOptions::new()
124            .set_timeout_ms(Some(5000))
125            .set_validate_only(true)
126            .set_retry_on_quota_violation(false);
127        assert_eq!(options.timeout_ms(), Some(5000));
128        assert!(options.should_validate_only());
129        assert!(!options.should_retry_on_quota_violation());
130    }
131}