1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64};
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 = 9;
16pub const MIN_VERSION: i16 = 1;
17pub const MAX_VERSION: i16 = 10;
18pub const FLEXIBLE_MIN: i16 = 6;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct OffsetFetchResponse {
25 pub throttle_time_ms: i32,
26 pub topics: Vec<OffsetFetchResponseTopic>,
27 pub error_code: i16,
28 pub groups: Vec<OffsetFetchResponseGroup>,
29 pub unknown_tagged_fields: UnknownTaggedFields,
30}
31
32impl Encode for OffsetFetchResponse {
33 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
36 }
37 let flex = is_flexible(version);
38 if version >= 3 { put_i32(buf, self.throttle_time_ms) }
39 if version >= 0 && version <= 7 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
40 if version >= 2 && version <= 7 { put_i16(buf, self.error_code) }
41 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.groups).len(), flex); for it in &self.groups { it.encode(buf, version)?; } } }
42 if flex {
43 let tagged = WriteTaggedFields::new();
44 tagged.write(buf, &self.unknown_tagged_fields);
45 }
46 Ok(())
47 }
48 fn encoded_len(&self, version: i16) -> usize {
49 let flex = is_flexible(version);
50 let mut n: usize = 0;
51 if version >= 3 { n += 4; }
52 if version >= 0 && version <= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
53 if version >= 2 && version <= 7 { n += 2; }
54 if version >= 8 { 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 }; }
55 if flex {
56 let known_pairs: Vec<(u32, usize)> = Vec::new();
57 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
58 }
59 n
60 }
61}
62
63impl<'de> Decode<'de> for OffsetFetchResponse {
64 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
65 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
66 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
67 }
68 let flex = is_flexible(version);
69 let mut out = Self::default();
70 if version >= 3 { out.throttle_time_ms = get_i32(buf)?; }
71 if version >= 0 && version <= 7 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchResponseTopic::decode(buf, version)?); } v }; }
72 if version >= 2 && version <= 7 { out.error_code = get_i16(buf)?; }
73 if version >= 8 { 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(OffsetFetchResponseGroup::decode(buf, version)?); } v }; }
74 if flex {
75 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
76 Ok(false)
77 })?;
78 }
79 Ok(out)
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq, Default)]
84pub struct OffsetFetchResponseTopic {
85 pub name: String,
86 pub partitions: Vec<OffsetFetchResponsePartition>,
87 pub unknown_tagged_fields: UnknownTaggedFields,
88}
89
90impl Encode for OffsetFetchResponseTopic {
91 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
92 let flex = version >= 6;
93 if version >= 0 && version <= 7 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
94 if version >= 0 && version <= 7 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
95 if flex {
96 let tagged = WriteTaggedFields::new();
97 tagged.write(buf, &self.unknown_tagged_fields);
98 }
99 Ok(())
100 }
101 fn encoded_len(&self, version: i16) -> usize {
102 let flex = version >= 6;
103 let mut n: usize = 0;
104 if version >= 0 && version <= 7 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
105 if version >= 0 && version <= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
106 if flex {
107 let known_pairs: Vec<(u32, usize)> = Vec::new();
108 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
109 }
110 n
111 }
112}
113
114impl<'de> Decode<'de> for OffsetFetchResponseTopic {
115 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
116 let flex = version >= 6;
117 let mut out = Self::default();
118 if version >= 0 && version <= 7 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
119 if version >= 0 && version <= 7 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchResponsePartition::decode(buf, version)?); } v }; }
120 if flex {
121 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
122 Ok(false)
123 })?;
124 }
125 Ok(out)
126 }
127}
128
129#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct OffsetFetchResponsePartition {
131 pub partition_index: i32,
132 pub committed_offset: i64,
133 pub committed_leader_epoch: i32,
134 pub metadata: Option<String>,
135 pub error_code: i16,
136 pub unknown_tagged_fields: UnknownTaggedFields,
137}
138
139impl Default for OffsetFetchResponsePartition {
140 fn default() -> Self {
141 Self {
142 partition_index: 0i32,
143 committed_offset: 0i64,
144 committed_leader_epoch: -1i32,
145 metadata: None,
146 error_code: 0i16,
147 unknown_tagged_fields: Default::default(),
148 }
149 }
150}
151
152impl Encode for OffsetFetchResponsePartition {
153 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
154 let flex = version >= 6;
155 if version >= 0 && version <= 7 { put_i32(buf, self.partition_index) }
156 if version >= 0 && version <= 7 { put_i64(buf, self.committed_offset) }
157 if version >= 5 && version <= 7 { put_i32(buf, self.committed_leader_epoch) }
158 if version >= 0 && version <= 7 { if flex { put_compact_nullable_string(buf, self.metadata.as_deref()) } else { put_nullable_string(buf, self.metadata.as_deref()) } }
159 if version >= 0 && version <= 7 { put_i16(buf, self.error_code) }
160 if flex {
161 let tagged = WriteTaggedFields::new();
162 tagged.write(buf, &self.unknown_tagged_fields);
163 }
164 Ok(())
165 }
166 fn encoded_len(&self, version: i16) -> usize {
167 let flex = version >= 6;
168 let mut n: usize = 0;
169 if version >= 0 && version <= 7 { n += 4; }
170 if version >= 0 && version <= 7 { n += 8; }
171 if version >= 5 && version <= 7 { n += 4; }
172 if version >= 0 && version <= 7 { n += if flex { compact_nullable_string_len(self.metadata.as_deref()) } else { nullable_string_len(self.metadata.as_deref()) }; }
173 if version >= 0 && version <= 7 { n += 2; }
174 if flex {
175 let known_pairs: Vec<(u32, usize)> = Vec::new();
176 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
177 }
178 n
179 }
180}
181
182impl<'de> Decode<'de> for OffsetFetchResponsePartition {
183 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
184 let flex = version >= 6;
185 let mut out = Self::default();
186 if version >= 0 && version <= 7 { out.partition_index = get_i32(buf)?; }
187 if version >= 0 && version <= 7 { out.committed_offset = get_i64(buf)?; }
188 if version >= 5 && version <= 7 { out.committed_leader_epoch = get_i32(buf)?; }
189 if version >= 0 && version <= 7 { out.metadata = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
190 if version >= 0 && version <= 7 { out.error_code = get_i16(buf)?; }
191 if flex {
192 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
193 Ok(false)
194 })?;
195 }
196 Ok(out)
197 }
198}
199
200#[derive(Debug, Clone, PartialEq, Eq, Default)]
201pub struct OffsetFetchResponseGroup {
202 pub group_id: String,
203 pub topics: Vec<OffsetFetchResponseTopics>,
204 pub error_code: i16,
205 pub unknown_tagged_fields: UnknownTaggedFields,
206}
207
208impl Encode for OffsetFetchResponseGroup {
209 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
210 let flex = version >= 6;
211 if version >= 8 { if flex { put_compact_string(buf, &self.group_id) } else { put_string(buf, &self.group_id) } }
212 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
213 if version >= 8 { put_i16(buf, self.error_code) }
214 if flex {
215 let tagged = WriteTaggedFields::new();
216 tagged.write(buf, &self.unknown_tagged_fields);
217 }
218 Ok(())
219 }
220 fn encoded_len(&self, version: i16) -> usize {
221 let flex = version >= 6;
222 let mut n: usize = 0;
223 if version >= 8 { n += if flex { compact_string_len(&self.group_id) } else { string_len(&self.group_id) }; }
224 if version >= 8 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
225 if version >= 8 { n += 2; }
226 if flex {
227 let known_pairs: Vec<(u32, usize)> = Vec::new();
228 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
229 }
230 n
231 }
232}
233
234impl<'de> Decode<'de> for OffsetFetchResponseGroup {
235 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
236 let flex = version >= 6;
237 let mut out = Self::default();
238 if version >= 8 { out.group_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
239 if version >= 8 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchResponseTopics::decode(buf, version)?); } v }; }
240 if version >= 8 { out.error_code = get_i16(buf)?; }
241 if flex {
242 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
243 Ok(false)
244 })?;
245 }
246 Ok(out)
247 }
248}
249
250#[derive(Debug, Clone, PartialEq, Eq, Default)]
251pub struct OffsetFetchResponseTopics {
252 pub name: String,
253 pub topic_id: crate::primitives::uuid::Uuid,
254 pub partitions: Vec<OffsetFetchResponsePartitions>,
255 pub unknown_tagged_fields: UnknownTaggedFields,
256}
257
258impl Encode for OffsetFetchResponseTopics {
259 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
260 let flex = version >= 6;
261 if version >= 8 && version <= 9 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
262 if version >= 10 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
263 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
264 if flex {
265 let tagged = WriteTaggedFields::new();
266 tagged.write(buf, &self.unknown_tagged_fields);
267 }
268 Ok(())
269 }
270 fn encoded_len(&self, version: i16) -> usize {
271 let flex = version >= 6;
272 let mut n: usize = 0;
273 if version >= 8 && version <= 9 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
274 if version >= 10 { n += 16; }
275 if version >= 8 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
276 if flex {
277 let known_pairs: Vec<(u32, usize)> = Vec::new();
278 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
279 }
280 n
281 }
282}
283
284impl<'de> Decode<'de> for OffsetFetchResponseTopics {
285 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
286 let flex = version >= 6;
287 let mut out = Self::default();
288 if version >= 8 && version <= 9 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
289 if version >= 10 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
290 if version >= 8 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchResponsePartitions::decode(buf, version)?); } v }; }
291 if flex {
292 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
293 Ok(false)
294 })?;
295 }
296 Ok(out)
297 }
298}
299
300#[derive(Debug, Clone, PartialEq, Eq)]
301pub struct OffsetFetchResponsePartitions {
302 pub partition_index: i32,
303 pub committed_offset: i64,
304 pub committed_leader_epoch: i32,
305 pub metadata: Option<String>,
306 pub error_code: i16,
307 pub unknown_tagged_fields: UnknownTaggedFields,
308}
309
310impl Default for OffsetFetchResponsePartitions {
311 fn default() -> Self {
312 Self {
313 partition_index: 0i32,
314 committed_offset: 0i64,
315 committed_leader_epoch: -1i32,
316 metadata: None,
317 error_code: 0i16,
318 unknown_tagged_fields: Default::default(),
319 }
320 }
321}
322
323impl Encode for OffsetFetchResponsePartitions {
324 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
325 let flex = version >= 6;
326 if version >= 8 { put_i32(buf, self.partition_index) }
327 if version >= 8 { put_i64(buf, self.committed_offset) }
328 if version >= 8 { put_i32(buf, self.committed_leader_epoch) }
329 if version >= 8 { if flex { put_compact_nullable_string(buf, self.metadata.as_deref()) } else { put_nullable_string(buf, self.metadata.as_deref()) } }
330 if version >= 8 { put_i16(buf, self.error_code) }
331 if flex {
332 let tagged = WriteTaggedFields::new();
333 tagged.write(buf, &self.unknown_tagged_fields);
334 }
335 Ok(())
336 }
337 fn encoded_len(&self, version: i16) -> usize {
338 let flex = version >= 6;
339 let mut n: usize = 0;
340 if version >= 8 { n += 4; }
341 if version >= 8 { n += 8; }
342 if version >= 8 { n += 4; }
343 if version >= 8 { n += if flex { compact_nullable_string_len(self.metadata.as_deref()) } else { nullable_string_len(self.metadata.as_deref()) }; }
344 if version >= 8 { n += 2; }
345 if flex {
346 let known_pairs: Vec<(u32, usize)> = Vec::new();
347 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
348 }
349 n
350 }
351}
352
353impl<'de> Decode<'de> for OffsetFetchResponsePartitions {
354 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
355 let flex = version >= 6;
356 let mut out = Self::default();
357 if version >= 8 { out.partition_index = get_i32(buf)?; }
358 if version >= 8 { out.committed_offset = get_i64(buf)?; }
359 if version >= 8 { out.committed_leader_epoch = get_i32(buf)?; }
360 if version >= 8 { out.metadata = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
361 if version >= 8 { out.error_code = get_i16(buf)?; }
362 if flex {
363 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
364 Ok(false)
365 })?;
366 }
367 Ok(out)
368 }
369}
370
371#[must_use]
374#[allow(unused_comparisons)]
375pub fn default_json(version: i16) -> ::serde_json::Value {
376 let mut obj = ::serde_json::Map::new();
377 if version >= 3 {
378 obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
379 }
380 if version <= 7 {
381 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
382 }
383 if version >= 2 && version <= 7 {
384 obj.insert("errorCode".to_string(), ::serde_json::json!(0));
385 }
386 if version >= 8 {
387 obj.insert("groups".to_string(), ::serde_json::Value::Array(vec![]));
388 }
389 ::serde_json::Value::Object(obj)
390}