1use std::collections::HashSet;
20use std::fmt;
21
22use crate::common::GroupType;
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
41#[non_exhaustive]
42#[doc(alias = "org.apache.kafka.common.GroupState")]
43pub enum GroupState {
44 Unknown,
46 PreparingRebalance,
48 CompletingRebalance,
50 Stable,
52 Dead,
54 Empty,
56 Assigning,
58 Reconciling,
60 NotReady,
62}
63
64impl GroupState {
65 #[doc(alias = "org.apache.kafka.common.GroupState#name")]
72 pub fn name(&self) -> &'static str {
73 match self {
74 Self::Unknown => "UNKNOWN",
75 Self::PreparingRebalance => "PREPARING_REBALANCE",
76 Self::CompletingRebalance => "COMPLETING_REBALANCE",
77 Self::Stable => "STABLE",
78 Self::Dead => "DEAD",
79 Self::Empty => "EMPTY",
80 Self::Assigning => "ASSIGNING",
81 Self::Reconciling => "RECONCILING",
82 Self::NotReady => "NOT_READY",
83 }
84 }
85
86 #[doc(alias = "org.apache.kafka.common.GroupState#parse")]
96 pub fn parse(name: &str) -> Self {
97 match name.to_uppercase().as_str() {
98 "UNKNOWN" => Self::Unknown,
99 "PREPARINGREBALANCE" => Self::PreparingRebalance,
100 "COMPLETINGREBALANCE" => Self::CompletingRebalance,
101 "STABLE" => Self::Stable,
102 "DEAD" => Self::Dead,
103 "EMPTY" => Self::Empty,
104 "ASSIGNING" => Self::Assigning,
105 "RECONCILING" => Self::Reconciling,
106 "NOTREADY" => Self::NotReady,
107 _ => Self::Unknown,
108 }
109 }
110
111 #[doc(alias = "org.apache.kafka.common.GroupState#groupStatesForType")]
126 pub fn group_states_for_type(group_type: GroupType) -> HashSet<GroupState> {
127 match group_type {
128 GroupType::Classic => [
129 Self::PreparingRebalance,
130 Self::CompletingRebalance,
131 Self::Stable,
132 Self::Dead,
133 Self::Empty,
134 ]
135 .into_iter()
136 .collect(),
137 GroupType::Consumer => [
138 Self::PreparingRebalance,
139 Self::CompletingRebalance,
140 Self::Stable,
141 Self::Dead,
142 Self::Empty,
143 Self::Assigning,
144 Self::Reconciling,
145 ]
146 .into_iter()
147 .collect(),
148 GroupType::Streams => [
149 Self::Stable,
150 Self::Dead,
151 Self::Empty,
152 Self::Assigning,
153 Self::Reconciling,
154 Self::NotReady,
155 ]
156 .into_iter()
157 .collect(),
158 GroupType::Share => [Self::Stable, Self::Dead, Self::Empty].into_iter().collect(),
159 GroupType::Unknown => panic!("Group type not known"),
160 }
161 }
162}
163
164impl fmt::Display for GroupState {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 f.write_str(match self {
170 Self::Unknown => "Unknown",
171 Self::PreparingRebalance => "PreparingRebalance",
172 Self::CompletingRebalance => "CompletingRebalance",
173 Self::Stable => "Stable",
174 Self::Dead => "Dead",
175 Self::Empty => "Empty",
176 Self::Assigning => "Assigning",
177 Self::Reconciling => "Reconciling",
178 Self::NotReady => "NotReady",
179 })
180 }
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186
187 #[test]
188 fn parse_is_case_insensitive() {
189 assert_eq!(GroupState::parse("stable"), GroupState::Stable);
190 assert_eq!(GroupState::parse("STABLE"), GroupState::Stable);
191 assert_eq!(GroupState::parse("PreparingRebalance"), GroupState::PreparingRebalance);
192 assert_eq!(GroupState::parse("NotReady"), GroupState::NotReady);
193 }
194
195 #[test]
196 fn parse_unknown_returns_unknown() {
197 assert_eq!(GroupState::parse(""), GroupState::Unknown);
198 assert_eq!(GroupState::parse("bogus"), GroupState::Unknown);
199 }
200
201 #[test]
205 fn name_and_display_match_java_for_every_constant() {
206 for (state, name, display) in [
207 (GroupState::Unknown, "UNKNOWN", "Unknown"),
208 (GroupState::PreparingRebalance, "PREPARING_REBALANCE", "PreparingRebalance"),
209 (GroupState::CompletingRebalance, "COMPLETING_REBALANCE", "CompletingRebalance"),
210 (GroupState::Stable, "STABLE", "Stable"),
211 (GroupState::Dead, "DEAD", "Dead"),
212 (GroupState::Empty, "EMPTY", "Empty"),
213 (GroupState::Assigning, "ASSIGNING", "Assigning"),
214 (GroupState::Reconciling, "RECONCILING", "Reconciling"),
215 (GroupState::NotReady, "NOT_READY", "NotReady"),
216 ] {
217 assert_eq!(state.name(), name);
218 assert_eq!(state.to_string(), display);
219 assert_eq!(GroupState::parse(&state.to_string()), state);
220 }
221 }
222
223 #[test]
227 fn parse_does_not_read_constant_names_with_an_underscore() {
228 assert_eq!(GroupState::parse("PREPARING_REBALANCE"), GroupState::Unknown);
229 assert_eq!(GroupState::parse("COMPLETING_REBALANCE"), GroupState::Unknown);
230 assert_eq!(GroupState::parse("NOT_READY"), GroupState::Unknown);
231 assert_eq!(GroupState::parse(GroupState::Stable.name()), GroupState::Stable);
232 }
233
234 #[test]
235 fn group_states_for_classic() {
236 let states = GroupState::group_states_for_type(GroupType::Classic);
237 assert_eq!(states.len(), 5);
238 assert!(states.contains(&GroupState::PreparingRebalance));
239 assert!(states.contains(&GroupState::Empty));
240 assert!(!states.contains(&GroupState::Assigning));
241 }
242
243 #[test]
244 fn group_states_for_consumer() {
245 let states = GroupState::group_states_for_type(GroupType::Consumer);
246 assert_eq!(states.len(), 7);
247 assert!(states.contains(&GroupState::Assigning));
248 assert!(states.contains(&GroupState::Reconciling));
249 assert!(!states.contains(&GroupState::NotReady));
250 }
251
252 #[test]
253 fn group_states_for_streams() {
254 let states = GroupState::group_states_for_type(GroupType::Streams);
255 assert_eq!(states.len(), 6);
256 assert!(states.contains(&GroupState::NotReady));
257 assert!(!states.contains(&GroupState::PreparingRebalance));
258 }
259
260 #[test]
261 fn group_states_for_share() {
262 let states = GroupState::group_states_for_type(GroupType::Share);
263 assert_eq!(states.len(), 3);
264 assert!(states.contains(&GroupState::Stable));
265 }
266
267 #[test]
268 #[should_panic(expected = "Group type not known")]
269 fn group_states_for_unknown_panics() {
270 GroupState::group_states_for_type(GroupType::Unknown);
271 }
272}