confluent_kafka/admin/
list_topics_result.rs1use std::collections::{HashMap, HashSet};
18
19use crate::admin::TopicListing;
20use crate::common::KafkaFuture;
21
22#[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 #[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 #[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 #[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 #[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}