Skip to main content

confluent_kafka/admin/
list_topics_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::list_topics`.
16
17use std::collections::{HashMap, HashSet};
18
19use crate::admin::TopicListing;
20use crate::common::KafkaFuture;
21
22/// The result of `Admin::list_topics`.
23#[derive(Clone, Debug)]
24#[doc(alias = "org.apache.kafka.clients.admin.ListTopicsResult")]
25pub struct ListTopicsResult {
26    future: KafkaFuture<HashMap<String, TopicListing>>,
27}
28
29impl ListTopicsResult {
30    /// Creates a result wrapping the topic-listing future.
31    #[doc(alias = "org.apache.kafka.clients.admin.ListTopicsResult#ListTopicsResult")]
32    pub(crate) fn new(future: KafkaFuture<HashMap<String, TopicListing>>) -> Self {
33        Self { future }
34    }
35
36    /// Returns a future which yields a map of topic names to `TopicListing`
37    /// objects.
38    #[doc(alias = "org.apache.kafka.clients.admin.ListTopicsResult#namesToListings")]
39    pub fn names_to_listings(&self) -> KafkaFuture<HashMap<String, TopicListing>> {
40        self.future.clone()
41    }
42
43    /// Returns a future which yields a collection of `TopicListing` objects.
44    #[doc(alias = "org.apache.kafka.clients.admin.ListTopicsResult#listings")]
45    pub fn listings(&self) -> KafkaFuture<Vec<TopicListing>> {
46        self.future
47            .then_apply(|map: HashMap<String, TopicListing>| Ok(map.into_values().collect()))
48    }
49
50    /// Returns a future which yields a set of topic names.
51    #[doc(alias = "org.apache.kafka.clients.admin.ListTopicsResult#names")]
52    pub fn names(&self) -> KafkaFuture<HashSet<String>> {
53        self.future
54            .then_apply(|map: HashMap<String, TopicListing>| Ok(map.into_keys().collect()))
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::common::Uuid;
62    use crate::common::internals::KafkaFutureImpl;
63
64    #[tokio::test]
65    async fn names_listings_and_map() {
66        let handle: KafkaFutureImpl<HashMap<String, TopicListing>> = KafkaFutureImpl::new();
67        let result = ListTopicsResult::new(handle.future());
68
69        let mut map = HashMap::new();
70        map.insert("t1".to_string(), TopicListing::new("t1", Uuid::new(1, 1), false));
71        map.insert("t2".to_string(), TopicListing::new("t2", Uuid::new(2, 2), true));
72        handle.complete(map);
73
74        let names = result.names().get().await.unwrap();
75        assert_eq!(names, HashSet::from(["t1".to_string(), "t2".to_string()]));
76        assert_eq!(result.listings().get().await.unwrap().len(), 2);
77        assert_eq!(result.names_to_listings().get().await.unwrap().len(), 2);
78    }
79}