confluent_kafka/common/classic_group_state.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 classic group state.
16//!
17//! Translated from `org.apache.kafka.common.ClassicGroupState`.
18
19use std::fmt;
20
21/// The classic group state.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[non_exhaustive]
24#[doc(alias = "org.apache.kafka.common.ClassicGroupState")]
25pub enum ClassicGroupState {
26 /// An unrecognized classic group state (e.g. a state newer than this
27 /// client).
28 Unknown,
29 /// The group is preparing to rebalance.
30 PreparingRebalance,
31 /// The group is completing a rebalance.
32 CompletingRebalance,
33 /// The group is stable.
34 Stable,
35 /// The group is dead.
36 Dead,
37 /// The group is empty.
38 Empty,
39}
40
41impl ClassicGroupState {
42 /// Returns the name of this enum constant, exactly as declared in Java
43 /// (`PREPARING_REBALANCE`).
44 ///
45 /// Java's `Enum.name()`, which `ClassicGroupState` inherits. The text Java's
46 /// `toString()` returns (`PreparingRebalance`, the text the broker sends and
47 /// [`parse`](Self::parse) reads) is this type's [`Display`](fmt::Display).
48 #[doc(alias = "org.apache.kafka.common.ClassicGroupState#name")]
49 pub fn name(&self) -> &'static str {
50 match self {
51 Self::Unknown => "UNKNOWN",
52 Self::PreparingRebalance => "PREPARING_REBALANCE",
53 Self::CompletingRebalance => "COMPLETING_REBALANCE",
54 Self::Stable => "STABLE",
55 Self::Dead => "DEAD",
56 Self::Empty => "EMPTY",
57 }
58 }
59
60 /// Case-insensitive classic group state lookup by string name.
61 ///
62 /// # Arguments
63 ///
64 /// - `name` - the state name
65 ///
66 /// # Returns
67 ///
68 /// The state, `Unknown` if unrecognized.
69 #[doc(alias = "org.apache.kafka.common.ClassicGroupState#parse")]
70 pub fn parse(name: &str) -> Self {
71 match name.to_uppercase().as_str() {
72 "UNKNOWN" => Self::Unknown,
73 "PREPARINGREBALANCE" => Self::PreparingRebalance,
74 "COMPLETINGREBALANCE" => Self::CompletingRebalance,
75 "STABLE" => Self::Stable,
76 "DEAD" => Self::Dead,
77 "EMPTY" => Self::Empty,
78 _ => Self::Unknown,
79 }
80 }
81}
82
83/// Java's `toString()`, which returns the private `name` field: the text
84/// (`PreparingRebalance`) the broker sends and [`ClassicGroupState::parse`] reads, not
85/// the constant name [`ClassicGroupState::name`] returns (the constants of `ClassicGroupState`).
86impl fmt::Display for ClassicGroupState {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 f.write_str(match self {
89 Self::Unknown => "Unknown",
90 Self::PreparingRebalance => "PreparingRebalance",
91 Self::CompletingRebalance => "CompletingRebalance",
92 Self::Stable => "Stable",
93 Self::Dead => "Dead",
94 Self::Empty => "Empty",
95 })
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn parse_is_case_insensitive() {
105 assert_eq!(ClassicGroupState::parse("stable"), ClassicGroupState::Stable);
106 assert_eq!(ClassicGroupState::parse("DEAD"), ClassicGroupState::Dead);
107 assert_eq!(ClassicGroupState::parse("Empty"), ClassicGroupState::Empty);
108 }
109
110 #[test]
111 fn parse_unknown_returns_unknown() {
112 assert_eq!(ClassicGroupState::parse(""), ClassicGroupState::Unknown);
113 assert_eq!(ClassicGroupState::parse("bogus"), ClassicGroupState::Unknown);
114 }
115
116 /// New test, no Java original (Java has no `ClassicGroupStateTest`): every
117 /// constant's `name()` is Java's `Enum.name()` and its `Display` is Java's
118 /// `toString()` (`ClassicGroupState#toString`), and `parse` reads the `toString()` text back.
119 #[test]
120 fn name_and_display_match_java_for_every_constant() {
121 for (state, name, display) in [
122 (ClassicGroupState::Unknown, "UNKNOWN", "Unknown"),
123 (
124 ClassicGroupState::PreparingRebalance,
125 "PREPARING_REBALANCE",
126 "PreparingRebalance",
127 ),
128 (
129 ClassicGroupState::CompletingRebalance,
130 "COMPLETING_REBALANCE",
131 "CompletingRebalance",
132 ),
133 (ClassicGroupState::Stable, "STABLE", "Stable"),
134 (ClassicGroupState::Dead, "DEAD", "Dead"),
135 (ClassicGroupState::Empty, "EMPTY", "Empty"),
136 ] {
137 assert_eq!(state.name(), name);
138 assert_eq!(state.to_string(), display);
139 assert_eq!(ClassicGroupState::parse(&state.to_string()), state);
140 }
141 }
142}