Skip to main content

confluent_kafka/admin/
create_partitions_result.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//! The result of `Admin::create_partitions`.
16
17use std::collections::HashMap;
18
19use crate::common::KafkaFuture;
20
21/// The result of `Admin::create_partitions`.
22#[derive(Clone, Debug)]
23#[doc(alias = "org.apache.kafka.clients.admin.CreatePartitionsResult")]
24pub struct CreatePartitionsResult {
25    values: HashMap<String, KafkaFuture<()>>,
26}
27
28impl CreatePartitionsResult {
29    /// Creates a result from the per-topic futures.
30    #[doc(alias = "org.apache.kafka.clients.admin.CreatePartitionsResult#CreatePartitionsResult")]
31    pub(crate) fn new(values: HashMap<String, KafkaFuture<()>>) -> Self {
32        Self { values }
33    }
34
35    /// Returns a map from topic names to futures, which can be used to check the
36    /// status of individual partition creations.
37    #[doc(alias = "org.apache.kafka.clients.admin.CreatePartitionsResult#values")]
38    pub fn values(&self) -> &HashMap<String, KafkaFuture<()>> {
39        &self.values
40    }
41
42    /// Returns a future which succeeds if all the partition creations succeed.
43    #[doc(alias = "org.apache.kafka.clients.admin.CreatePartitionsResult#all")]
44    pub fn all(&self) -> KafkaFuture<()> {
45        KafkaFuture::all_of(self.values.values().cloned().collect())
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use crate::common::Error;
53    use crate::common::internals::KafkaFutureImpl;
54
55    #[tokio::test]
56    async fn all_succeeds_when_each_completes() {
57        let h1: KafkaFutureImpl<()> = KafkaFutureImpl::new();
58        let h2: KafkaFutureImpl<()> = KafkaFutureImpl::new();
59        let mut map = HashMap::new();
60        map.insert("a".to_string(), h1.future());
61        map.insert("b".to_string(), h2.future());
62        let result = CreatePartitionsResult::new(map);
63        h1.complete(());
64        h2.complete(());
65        assert_eq!(result.all().get().await.unwrap(), ());
66    }
67
68    #[tokio::test]
69    async fn all_fails_if_one_fails() {
70        let h1: KafkaFutureImpl<()> = KafkaFutureImpl::new();
71        let mut map = HashMap::new();
72        map.insert("a".to_string(), h1.future());
73        let result = CreatePartitionsResult::new(map);
74        h1.complete_with_error(Error::local_illegal_state("boom".to_string()));
75        assert!(result.all().get().await.is_err());
76    }
77}