Skip to main content

confluent_kafka/admin/
group_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 group in the cluster.
16
17use crate::common::{GroupState, GroupType};
18
19/// A listing of a group in the cluster.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.clients.admin.GroupListing")]
22pub struct GroupListing {
23    group_id: String,
24    group_type: Option<GroupType>,
25    protocol: String,
26    group_state: Option<GroupState>,
27}
28
29impl GroupListing {
30    /// Creates a group listing.
31    ///
32    /// # Arguments
33    ///
34    /// - `group_id` - Group Id
35    /// - `group_type` - Group type
36    /// - `protocol` - Protocol
37    /// - `group_state` - Group state
38    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#GroupListing")]
39    pub fn new(
40        group_id: impl Into<String>,
41        group_type: Option<GroupType>,
42        protocol: impl Into<String>,
43        group_state: Option<GroupState>,
44    ) -> Self {
45        Self { group_id: group_id.into(), group_type, protocol: protocol.into(), group_state }
46    }
47
48    /// The group id.
49    ///
50    /// # Returns
51    ///
52    /// Group Id
53    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#groupId")]
54    pub fn group_id(&self) -> &str {
55        &self.group_id
56    }
57
58    /// The type of the group.
59    ///
60    /// If the broker returns a group type which is not recognised, as might happen when talking
61    /// to a broker with a later version, the type will be `Some(GroupType::Unknown)`. If the
62    /// broker is earlier than version 2.6.0, the group type will not be available, and the type
63    /// will be `None`.
64    ///
65    /// # Returns
66    ///
67    /// The type, if available.
68    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#type")]
69    pub fn r#type(&self) -> Option<GroupType> {
70        self.group_type
71    }
72
73    /// The protocol of the group.
74    ///
75    /// # Returns
76    ///
77    /// The protocol
78    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#protocol")]
79    pub fn protocol(&self) -> &str {
80        &self.protocol
81    }
82
83    /// The group state.
84    ///
85    /// If the broker returns a group state which is not recognised, as might happen when talking
86    /// to a broker with a later version, the state will be `Some(GroupState::Unknown)`.
87    ///
88    /// # Returns
89    ///
90    /// The state, if available.
91    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#groupState")]
92    pub fn group_state(&self) -> Option<GroupState> {
93        self.group_state
94    }
95
96    /// Returns whether the group is a simple consumer group or not.
97    #[doc(alias = "org.apache.kafka.clients.admin.GroupListing#isSimpleConsumerGroup")]
98    pub fn is_simple_consumer_group(&self) -> bool {
99        self.group_type == Some(GroupType::Classic) && self.protocol.is_empty()
100    }
101}
102
103impl std::fmt::Display for GroupListing {
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        write!(
106            f,
107            "(groupId='{}', type={}, protocol='{}', groupState={})",
108            self.group_id,
109            self.group_type.map_or_else(|| "none".to_string(), |t| t.to_string()),
110            self.protocol,
111            self.group_state.map_or_else(|| "none".to_string(), |s| s.to_string()),
112        )
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119    use crate::consumer::internals::ConsumerProtocol;
120
121    const GROUP_ID: &str = "mygroup";
122
123    /// Translated from `GroupListingTest.testSimpleConsumerGroup`.
124    #[test]
125    #[doc(alias = "org.apache.kafka.clients.admin.GroupListingTest#testSimpleConsumerGroup")]
126    fn test_simple_consumer_group() {
127        let gl = GroupListing::new(GROUP_ID, Some(GroupType::Classic), "", Some(GroupState::Empty));
128        assert!(gl.is_simple_consumer_group());
129
130        let gl = GroupListing::new(
131            GROUP_ID,
132            Some(GroupType::Classic),
133            ConsumerProtocol::PROTOCOL_TYPE,
134            Some(GroupState::Stable),
135        );
136        assert!(!gl.is_simple_consumer_group());
137
138        let gl = GroupListing::new(GROUP_ID, Some(GroupType::Consumer), "", Some(GroupState::Empty));
139        assert!(!gl.is_simple_consumer_group());
140
141        let gl = GroupListing::new(GROUP_ID, None, "", None);
142        assert!(!gl.is_simple_consumer_group());
143    }
144}