Skip to main content

confluent_kafka/admin/
topic_listing.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//! A listing of a topic in the cluster.
16
17use crate::common::Uuid;
18
19/// A listing of a topic in the cluster.
20#[derive(Clone, Debug)]
21#[doc(alias = "org.apache.kafka.clients.admin.TopicListing")]
22pub struct TopicListing {
23    name: String,
24    topic_id: Uuid,
25    internal: bool,
26}
27
28impl TopicListing {
29    /// Create an instance with the specified parameters.
30    ///
31    /// # Arguments
32    ///
33    /// - `name` - The topic name
34    /// - `topic_id` - The topic id.
35    /// - `internal` - Whether the topic is internal to Kafka
36    #[doc(alias = "org.apache.kafka.clients.admin.TopicListing#TopicListing")]
37    pub fn new(name: impl Into<String>, topic_id: Uuid, internal: bool) -> Self {
38        Self { name: name.into(), topic_id, internal }
39    }
40
41    /// Returns the id of the topic.
42    #[doc(alias = "org.apache.kafka.clients.admin.TopicListing#topicId")]
43    pub fn topic_id(&self) -> Uuid {
44        self.topic_id
45    }
46
47    /// Returns the name of the topic.
48    #[doc(alias = "org.apache.kafka.clients.admin.TopicListing#name")]
49    pub fn name(&self) -> &str {
50        &self.name
51    }
52
53    /// Returns whether the topic is internal to Kafka. An example of an internal topic
54    /// is the offsets and group management topic: `__consumer_offsets`.
55    #[doc(alias = "org.apache.kafka.clients.admin.TopicListing#isInternal")]
56    pub fn is_internal(&self) -> bool {
57        self.internal
58    }
59}
60
61impl std::fmt::Display for TopicListing {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "(name={}, topicId={}, internal={})", self.name, self.topic_id, self.internal)
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn accessors() {
73        let id = Uuid::new(1, 2);
74        let listing = TopicListing::new("t", id, true);
75        assert_eq!(listing.name(), "t");
76        assert_eq!(listing.topic_id(), id);
77        assert!(listing.is_internal());
78    }
79}