Skip to main content

confluent_kafka/admin/
describe_configs_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::describe_configs`.
16
17use std::collections::HashMap;
18
19use crate::common::KafkaFuture;
20use crate::common::config::ConfigResource;
21
22use super::Config;
23
24/// The result of the `Admin::describe_configs` call.
25#[derive(Debug)]
26#[doc(alias = "org.apache.kafka.clients.admin.DescribeConfigsResult")]
27pub struct DescribeConfigsResult {
28    futures: HashMap<ConfigResource, KafkaFuture<Config>>,
29}
30
31impl DescribeConfigsResult {
32    /// Creates a new result from the per-resource futures.
33    #[doc(alias = "org.apache.kafka.clients.admin.DescribeConfigsResult#DescribeConfigsResult")]
34    pub(crate) fn new(futures: HashMap<ConfigResource, KafkaFuture<Config>>) -> Self {
35        Self { futures }
36    }
37
38    /// Returns a map from resources to futures which can be used to check the
39    /// status of the configuration for each resource.
40    #[doc(alias = "org.apache.kafka.clients.admin.DescribeConfigsResult#values")]
41    pub fn values(&self) -> &HashMap<ConfigResource, KafkaFuture<Config>> {
42        &self.futures
43    }
44
45    /// Returns a future which succeeds only if all the config descriptions succeed.
46    #[doc(alias = "org.apache.kafka.clients.admin.DescribeConfigsResult#all")]
47    pub fn all(&self) -> KafkaFuture<HashMap<ConfigResource, Config>> {
48        let entries: Vec<(ConfigResource, KafkaFuture<Config>)> =
49            self.futures.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
50        KafkaFuture::join_map(entries)
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use crate::common::config::config_resource;
58
59    /// The constructor stays `pub(crate)` (Java's is `protected`), so this
60    /// in-crate test, not the `tests/admin` crate, checks that a result built
61    /// from completed futures reads them back through `values()` and `all()`.
62    #[tokio::test]
63    async fn values_and_all_read_back_the_futures_it_was_built_from() {
64        let resource = ConfigResource::new(config_resource::Type::Topic, "t".to_string());
65        let result = DescribeConfigsResult::new(HashMap::from([(
66            resource.clone(),
67            KafkaFuture::completed_future(Ok(Config::new(Vec::new()))),
68        )]));
69        let config = result.values()[&resource].get().await.expect("completed future");
70        assert_eq!(config.entries().count(), 0);
71        let all = result.all().get().await.expect("completed futures");
72        assert_eq!(all.len(), 1);
73        assert_eq!(all[&resource].entries().count(), 0);
74    }
75}