1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i32, get_i8, put_i16, put_i32, put_i8};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10 string_len,
11};
12use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 69;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 1;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct ConsumerGroupDescribeResponse {
25 pub throttle_time_ms: i32,
26 pub groups: Vec<DescribedGroup>,
27 pub unknown_tagged_fields: UnknownTaggedFields,
28}
29
30impl Encode for ConsumerGroupDescribeResponse {
31 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
32 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
33 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
34 }
35 let flex = is_flexible(version);
36 if version >= 0 { put_i32(buf, self.throttle_time_ms) }
37 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.groups).len(), flex); for it in &self.groups { it.encode(buf, version)?; } } }
38 if flex {
39 let tagged = WriteTaggedFields::new();
40 tagged.write(buf, &self.unknown_tagged_fields);
41 }
42 Ok(())
43 }
44 fn encoded_len(&self, version: i16) -> usize {
45 let flex = is_flexible(version);
46 let mut n: usize = 0;
47 if version >= 0 { n += 4; }
48 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.groups).len(), flex); let body: usize = (self.groups).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
49 if flex {
50 let known_pairs: Vec<(u32, usize)> = Vec::new();
51 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
52 }
53 n
54 }
55}
56
57impl<'de> Decode<'de> for ConsumerGroupDescribeResponse {
58 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
59 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
60 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
61 }
62 let flex = is_flexible(version);
63 let mut out = Self::default();
64 if version >= 0 { out.throttle_time_ms = get_i32(buf)?; }
65 if version >= 0 { out.groups = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(DescribedGroup::decode(buf, version)?); } v }; }
66 if flex {
67 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
68 Ok(false)
69 })?;
70 }
71 Ok(out)
72 }
73}
74
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct DescribedGroup {
77 pub error_code: i16,
78 pub error_message: Option<String>,
79 pub group_id: String,
80 pub group_state: String,
81 pub group_epoch: i32,
82 pub assignment_epoch: i32,
83 pub assignor_name: String,
84 pub members: Vec<Member>,
85 pub authorized_operations: i32,
86 pub unknown_tagged_fields: UnknownTaggedFields,
87}
88
89impl Default for DescribedGroup {
90 fn default() -> Self {
91 Self {
92 error_code: 0i16,
93 error_message: None,
94 group_id: String::new(),
95 group_state: String::new(),
96 group_epoch: 0i32,
97 assignment_epoch: 0i32,
98 assignor_name: String::new(),
99 members: Vec::new(),
100 authorized_operations: -2_147_483_648i32,
101 unknown_tagged_fields: Default::default(),
102 }
103 }
104}
105
106impl Encode for DescribedGroup {
107 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
108 let flex = version >= 0;
109 if version >= 0 { put_i16(buf, self.error_code) }
110 if version >= 0 { if flex { put_compact_nullable_string(buf, self.error_message.as_deref()) } else { put_nullable_string(buf, self.error_message.as_deref()) } }
111 if version >= 0 { if flex { put_compact_string(buf, &self.group_id) } else { put_string(buf, &self.group_id) } }
112 if version >= 0 { if flex { put_compact_string(buf, &self.group_state) } else { put_string(buf, &self.group_state) } }
113 if version >= 0 { put_i32(buf, self.group_epoch) }
114 if version >= 0 { put_i32(buf, self.assignment_epoch) }
115 if version >= 0 { if flex { put_compact_string(buf, &self.assignor_name) } else { put_string(buf, &self.assignor_name) } }
116 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.members).len(), flex); for it in &self.members { it.encode(buf, version)?; } } }
117 if version >= 0 { put_i32(buf, self.authorized_operations) }
118 if flex {
119 let tagged = WriteTaggedFields::new();
120 tagged.write(buf, &self.unknown_tagged_fields);
121 }
122 Ok(())
123 }
124 fn encoded_len(&self, version: i16) -> usize {
125 let flex = version >= 0;
126 let mut n: usize = 0;
127 if version >= 0 { n += 2; }
128 if version >= 0 { n += if flex { compact_nullable_string_len(self.error_message.as_deref()) } else { nullable_string_len(self.error_message.as_deref()) }; }
129 if version >= 0 { n += if flex { compact_string_len(&self.group_id) } else { string_len(&self.group_id) }; }
130 if version >= 0 { n += if flex { compact_string_len(&self.group_state) } else { string_len(&self.group_state) }; }
131 if version >= 0 { n += 4; }
132 if version >= 0 { n += 4; }
133 if version >= 0 { n += if flex { compact_string_len(&self.assignor_name) } else { string_len(&self.assignor_name) }; }
134 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.members).len(), flex); let body: usize = (self.members).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
135 if version >= 0 { n += 4; }
136 if flex {
137 let known_pairs: Vec<(u32, usize)> = Vec::new();
138 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
139 }
140 n
141 }
142}
143
144impl<'de> Decode<'de> for DescribedGroup {
145 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
146 let flex = version >= 0;
147 let mut out = Self::default();
148 if version >= 0 { out.error_code = get_i16(buf)?; }
149 if version >= 0 { out.error_message = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
150 if version >= 0 { out.group_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
151 if version >= 0 { out.group_state = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
152 if version >= 0 { out.group_epoch = get_i32(buf)?; }
153 if version >= 0 { out.assignment_epoch = get_i32(buf)?; }
154 if version >= 0 { out.assignor_name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
155 if version >= 0 { out.members = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(Member::decode(buf, version)?); } v }; }
156 if version >= 0 { out.authorized_operations = get_i32(buf)?; }
157 if flex {
158 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
159 Ok(false)
160 })?;
161 }
162 Ok(out)
163 }
164}
165
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct Member {
168 pub member_id: String,
169 pub instance_id: Option<String>,
170 pub rack_id: Option<String>,
171 pub member_epoch: i32,
172 pub client_id: String,
173 pub client_host: String,
174 pub subscribed_topic_names: Vec<String>,
175 pub subscribed_topic_regex: Option<String>,
176 pub assignment: super::common::assignment::Assignment,
177 pub target_assignment: super::common::assignment::Assignment,
178 pub member_type: i8,
179 pub unknown_tagged_fields: UnknownTaggedFields,
180}
181
182impl Default for Member {
183 fn default() -> Self {
184 Self {
185 member_id: String::new(),
186 instance_id: None,
187 rack_id: None,
188 member_epoch: 0i32,
189 client_id: String::new(),
190 client_host: String::new(),
191 subscribed_topic_names: Vec::new(),
192 subscribed_topic_regex: None,
193 assignment: Default::default(),
194 target_assignment: Default::default(),
195 member_type: -1i8,
196 unknown_tagged_fields: Default::default(),
197 }
198 }
199}
200
201impl Encode for Member {
202 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
203 let flex = version >= 0;
204 if version >= 0 { if flex { put_compact_string(buf, &self.member_id) } else { put_string(buf, &self.member_id) } }
205 if version >= 0 { if flex { put_compact_nullable_string(buf, self.instance_id.as_deref()) } else { put_nullable_string(buf, self.instance_id.as_deref()) } }
206 if version >= 0 { if flex { put_compact_nullable_string(buf, self.rack_id.as_deref()) } else { put_nullable_string(buf, self.rack_id.as_deref()) } }
207 if version >= 0 { put_i32(buf, self.member_epoch) }
208 if version >= 0 { if flex { put_compact_string(buf, &self.client_id) } else { put_string(buf, &self.client_id) } }
209 if version >= 0 { if flex { put_compact_string(buf, &self.client_host) } else { put_string(buf, &self.client_host) } }
210 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.subscribed_topic_names).len(), flex); for it in &self.subscribed_topic_names { if flex { put_compact_string(buf, &*it) } else { put_string(buf, &*it) }; } } }
211 if version >= 0 { if flex { put_compact_nullable_string(buf, self.subscribed_topic_regex.as_deref()) } else { put_nullable_string(buf, self.subscribed_topic_regex.as_deref()) } }
212 if version >= 0 { self.assignment.encode(buf, version)? }
213 if version >= 0 { self.target_assignment.encode(buf, version)? }
214 if version >= 1 { put_i8(buf, self.member_type) }
215 if flex {
216 let tagged = WriteTaggedFields::new();
217 tagged.write(buf, &self.unknown_tagged_fields);
218 }
219 Ok(())
220 }
221 fn encoded_len(&self, version: i16) -> usize {
222 let flex = version >= 0;
223 let mut n: usize = 0;
224 if version >= 0 { n += if flex { compact_string_len(&self.member_id) } else { string_len(&self.member_id) }; }
225 if version >= 0 { n += if flex { compact_nullable_string_len(self.instance_id.as_deref()) } else { nullable_string_len(self.instance_id.as_deref()) }; }
226 if version >= 0 { n += if flex { compact_nullable_string_len(self.rack_id.as_deref()) } else { nullable_string_len(self.rack_id.as_deref()) }; }
227 if version >= 0 { n += 4; }
228 if version >= 0 { n += if flex { compact_string_len(&self.client_id) } else { string_len(&self.client_id) }; }
229 if version >= 0 { n += if flex { compact_string_len(&self.client_host) } else { string_len(&self.client_host) }; }
230 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.subscribed_topic_names).len(), flex); let body: usize = (self.subscribed_topic_names).iter().map(|it| if flex { compact_string_len(&*it) } else { string_len(&*it) }).sum(); prefix + body }; }
231 if version >= 0 { n += if flex { compact_nullable_string_len(self.subscribed_topic_regex.as_deref()) } else { nullable_string_len(self.subscribed_topic_regex.as_deref()) }; }
232 if version >= 0 { n += self.assignment.encoded_len(version); }
233 if version >= 0 { n += self.target_assignment.encoded_len(version); }
234 if version >= 1 { n += 1; }
235 if flex {
236 let known_pairs: Vec<(u32, usize)> = Vec::new();
237 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
238 }
239 n
240 }
241}
242
243impl<'de> Decode<'de> for Member {
244 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
245 let flex = version >= 0;
246 let mut out = Self::default();
247 if version >= 0 { out.member_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
248 if version >= 0 { out.instance_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
249 if version >= 0 { out.rack_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
250 if version >= 0 { out.member_epoch = get_i32(buf)?; }
251 if version >= 0 { out.client_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
252 if version >= 0 { out.client_host = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
253 if version >= 0 { out.subscribed_topic_names = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }); } v }; }
254 if version >= 0 { out.subscribed_topic_regex = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
255 if version >= 0 { out.assignment = super::common::assignment::Assignment::decode(buf, version)?; }
256 if version >= 0 { out.target_assignment = super::common::assignment::Assignment::decode(buf, version)?; }
257 if version >= 1 { out.member_type = get_i8(buf)?; }
258 if flex {
259 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
260 Ok(false)
261 })?;
262 }
263 Ok(out)
264 }
265}
266
267#[must_use]
270#[allow(unused_comparisons)]
271pub fn default_json(version: i16) -> ::serde_json::Value {
272 let mut obj = ::serde_json::Map::new();
273 obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
274 obj.insert("groups".to_string(), ::serde_json::Value::Array(vec![]));
275 ::serde_json::Value::Object(obj)
276}