1#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15 buf::{ByteBuf, ByteBufMut},
16 compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17 Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct EpochEndOffset {
24 pub error_code: i16,
28
29 pub partition: i32,
33
34 pub leader_epoch: i32,
38
39 pub end_offset: i64,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl EpochEndOffset {
49 pub fn with_error_code(mut self, value: i16) -> Self {
55 self.error_code = value;
56 self
57 }
58 pub fn with_partition(mut self, value: i32) -> Self {
64 self.partition = value;
65 self
66 }
67 pub fn with_leader_epoch(mut self, value: i32) -> Self {
73 self.leader_epoch = value;
74 self
75 }
76 pub fn with_end_offset(mut self, value: i64) -> Self {
82 self.end_offset = value;
83 self
84 }
85 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87 self.unknown_tagged_fields = value;
88 self
89 }
90 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92 self.unknown_tagged_fields.insert(key, value);
93 self
94 }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for EpochEndOffset {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version < 0 || version > 4 {
101 bail!("specified version not supported by this message type");
102 }
103 types::Int16.encode(buf, &self.error_code)?;
104 types::Int32.encode(buf, &self.partition)?;
105 if version >= 1 {
106 types::Int32.encode(buf, &self.leader_epoch)?;
107 }
108 types::Int64.encode(buf, &self.end_offset)?;
109 if version >= 4 {
110 let num_tagged_fields = self.unknown_tagged_fields.len();
111 if num_tagged_fields > std::u32::MAX as usize {
112 bail!(
113 "Too many tagged fields to encode ({} fields)",
114 num_tagged_fields
115 );
116 }
117 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
118
119 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
120 }
121 Ok(())
122 }
123 fn compute_size(&self, version: i16) -> Result<usize> {
124 let mut total_size = 0;
125 total_size += types::Int16.compute_size(&self.error_code)?;
126 total_size += types::Int32.compute_size(&self.partition)?;
127 if version >= 1 {
128 total_size += types::Int32.compute_size(&self.leader_epoch)?;
129 }
130 total_size += types::Int64.compute_size(&self.end_offset)?;
131 if version >= 4 {
132 let num_tagged_fields = self.unknown_tagged_fields.len();
133 if num_tagged_fields > std::u32::MAX as usize {
134 bail!(
135 "Too many tagged fields to encode ({} fields)",
136 num_tagged_fields
137 );
138 }
139 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
140
141 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
142 }
143 Ok(total_size)
144 }
145}
146
147#[cfg(feature = "client")]
148impl Decodable for EpochEndOffset {
149 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
150 if version < 0 || version > 4 {
151 bail!("specified version not supported by this message type");
152 }
153 let error_code = types::Int16.decode(buf)?;
154 let partition = types::Int32.decode(buf)?;
155 let leader_epoch = if version >= 1 {
156 types::Int32.decode(buf)?
157 } else {
158 -1
159 };
160 let end_offset = types::Int64.decode(buf)?;
161 let mut unknown_tagged_fields = BTreeMap::new();
162 if version >= 4 {
163 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
164 for _ in 0..num_tagged_fields {
165 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
166 let size: u32 = types::UnsignedVarInt.decode(buf)?;
167 let unknown_value = buf.try_get_bytes(size as usize)?;
168 unknown_tagged_fields.insert(tag as i32, unknown_value);
169 }
170 }
171 Ok(Self {
172 error_code,
173 partition,
174 leader_epoch,
175 end_offset,
176 unknown_tagged_fields,
177 })
178 }
179}
180
181impl Default for EpochEndOffset {
182 fn default() -> Self {
183 Self {
184 error_code: 0,
185 partition: 0,
186 leader_epoch: -1,
187 end_offset: -1,
188 unknown_tagged_fields: BTreeMap::new(),
189 }
190 }
191}
192
193impl Message for EpochEndOffset {
194 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
195 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
196}
197
198#[non_exhaustive]
200#[derive(Debug, Clone, PartialEq)]
201pub struct OffsetForLeaderEpochResponse {
202 pub throttle_time_ms: i32,
206
207 pub topics: Vec<OffsetForLeaderTopicResult>,
211
212 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
214}
215
216impl OffsetForLeaderEpochResponse {
217 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
223 self.throttle_time_ms = value;
224 self
225 }
226 pub fn with_topics(mut self, value: Vec<OffsetForLeaderTopicResult>) -> Self {
232 self.topics = value;
233 self
234 }
235 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
237 self.unknown_tagged_fields = value;
238 self
239 }
240 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
242 self.unknown_tagged_fields.insert(key, value);
243 self
244 }
245}
246
247#[cfg(feature = "broker")]
248impl Encodable for OffsetForLeaderEpochResponse {
249 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
250 if version < 0 || version > 4 {
251 bail!("specified version not supported by this message type");
252 }
253 if version >= 2 {
254 types::Int32.encode(buf, &self.throttle_time_ms)?;
255 }
256 if version >= 4 {
257 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
258 } else {
259 types::Array(types::Struct { version }).encode(buf, &self.topics)?;
260 }
261 if version >= 4 {
262 let num_tagged_fields = self.unknown_tagged_fields.len();
263 if num_tagged_fields > std::u32::MAX as usize {
264 bail!(
265 "Too many tagged fields to encode ({} fields)",
266 num_tagged_fields
267 );
268 }
269 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
270
271 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
272 }
273 Ok(())
274 }
275 fn compute_size(&self, version: i16) -> Result<usize> {
276 let mut total_size = 0;
277 if version >= 2 {
278 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
279 }
280 if version >= 4 {
281 total_size +=
282 types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
283 } else {
284 total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
285 }
286 if version >= 4 {
287 let num_tagged_fields = self.unknown_tagged_fields.len();
288 if num_tagged_fields > std::u32::MAX as usize {
289 bail!(
290 "Too many tagged fields to encode ({} fields)",
291 num_tagged_fields
292 );
293 }
294 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
295
296 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
297 }
298 Ok(total_size)
299 }
300}
301
302#[cfg(feature = "client")]
303impl Decodable for OffsetForLeaderEpochResponse {
304 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
305 if version < 0 || version > 4 {
306 bail!("specified version not supported by this message type");
307 }
308 let throttle_time_ms = if version >= 2 {
309 types::Int32.decode(buf)?
310 } else {
311 0
312 };
313 let topics = if version >= 4 {
314 types::CompactArray(types::Struct { version }).decode(buf)?
315 } else {
316 types::Array(types::Struct { version }).decode(buf)?
317 };
318 let mut unknown_tagged_fields = BTreeMap::new();
319 if version >= 4 {
320 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
321 for _ in 0..num_tagged_fields {
322 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
323 let size: u32 = types::UnsignedVarInt.decode(buf)?;
324 let unknown_value = buf.try_get_bytes(size as usize)?;
325 unknown_tagged_fields.insert(tag as i32, unknown_value);
326 }
327 }
328 Ok(Self {
329 throttle_time_ms,
330 topics,
331 unknown_tagged_fields,
332 })
333 }
334}
335
336impl Default for OffsetForLeaderEpochResponse {
337 fn default() -> Self {
338 Self {
339 throttle_time_ms: 0,
340 topics: Default::default(),
341 unknown_tagged_fields: BTreeMap::new(),
342 }
343 }
344}
345
346impl Message for OffsetForLeaderEpochResponse {
347 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
348 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
349}
350
351#[non_exhaustive]
353#[derive(Debug, Clone, PartialEq)]
354pub struct OffsetForLeaderTopicResult {
355 pub topic: super::TopicName,
359
360 pub partitions: Vec<EpochEndOffset>,
364
365 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
367}
368
369impl OffsetForLeaderTopicResult {
370 pub fn with_topic(mut self, value: super::TopicName) -> Self {
376 self.topic = value;
377 self
378 }
379 pub fn with_partitions(mut self, value: Vec<EpochEndOffset>) -> Self {
385 self.partitions = value;
386 self
387 }
388 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
390 self.unknown_tagged_fields = value;
391 self
392 }
393 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
395 self.unknown_tagged_fields.insert(key, value);
396 self
397 }
398}
399
400#[cfg(feature = "broker")]
401impl Encodable for OffsetForLeaderTopicResult {
402 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
403 if version < 0 || version > 4 {
404 bail!("specified version not supported by this message type");
405 }
406 if version >= 4 {
407 types::CompactString.encode(buf, &self.topic)?;
408 } else {
409 types::String.encode(buf, &self.topic)?;
410 }
411 if version >= 4 {
412 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
413 } else {
414 types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
415 }
416 if version >= 4 {
417 let num_tagged_fields = self.unknown_tagged_fields.len();
418 if num_tagged_fields > std::u32::MAX as usize {
419 bail!(
420 "Too many tagged fields to encode ({} fields)",
421 num_tagged_fields
422 );
423 }
424 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
425
426 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
427 }
428 Ok(())
429 }
430 fn compute_size(&self, version: i16) -> Result<usize> {
431 let mut total_size = 0;
432 if version >= 4 {
433 total_size += types::CompactString.compute_size(&self.topic)?;
434 } else {
435 total_size += types::String.compute_size(&self.topic)?;
436 }
437 if version >= 4 {
438 total_size +=
439 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
440 } else {
441 total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
442 }
443 if version >= 4 {
444 let num_tagged_fields = self.unknown_tagged_fields.len();
445 if num_tagged_fields > std::u32::MAX as usize {
446 bail!(
447 "Too many tagged fields to encode ({} fields)",
448 num_tagged_fields
449 );
450 }
451 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
452
453 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
454 }
455 Ok(total_size)
456 }
457}
458
459#[cfg(feature = "client")]
460impl Decodable for OffsetForLeaderTopicResult {
461 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
462 if version < 0 || version > 4 {
463 bail!("specified version not supported by this message type");
464 }
465 let topic = if version >= 4 {
466 types::CompactString.decode(buf)?
467 } else {
468 types::String.decode(buf)?
469 };
470 let partitions = if version >= 4 {
471 types::CompactArray(types::Struct { version }).decode(buf)?
472 } else {
473 types::Array(types::Struct { version }).decode(buf)?
474 };
475 let mut unknown_tagged_fields = BTreeMap::new();
476 if version >= 4 {
477 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
478 for _ in 0..num_tagged_fields {
479 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
480 let size: u32 = types::UnsignedVarInt.decode(buf)?;
481 let unknown_value = buf.try_get_bytes(size as usize)?;
482 unknown_tagged_fields.insert(tag as i32, unknown_value);
483 }
484 }
485 Ok(Self {
486 topic,
487 partitions,
488 unknown_tagged_fields,
489 })
490 }
491}
492
493impl Default for OffsetForLeaderTopicResult {
494 fn default() -> Self {
495 Self {
496 topic: Default::default(),
497 partitions: Default::default(),
498 unknown_tagged_fields: BTreeMap::new(),
499 }
500 }
501}
502
503impl Message for OffsetForLeaderTopicResult {
504 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
505 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
506}
507
508impl HeaderVersion for OffsetForLeaderEpochResponse {
509 fn header_version(version: i16) -> i16 {
510 if version >= 4 {
511 1
512 } else {
513 0
514 }
515 }
516}