#![allow(unused)]
use std::borrow::Borrow;
use std::collections::BTreeMap;
use anyhow::bail;
use bytes::Bytes;
use uuid::Uuid;
use crate::protocol::{
buf::{ByteBuf, ByteBufMut},
compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Builder, Decodable,
DecodeError, Decoder, Encodable, EncodeError, Encoder, HeaderVersion, MapDecodable,
MapEncodable, Message, StrBytes, VersionRange,
};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct OffsetCommitResponse {
pub throttle_time_ms: i32,
pub topics: Vec<OffsetCommitResponseTopic>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl Builder for OffsetCommitResponse {
type Builder = OffsetCommitResponseBuilder;
fn builder() -> Self::Builder {
OffsetCommitResponseBuilder::default()
}
}
impl Encodable for OffsetCommitResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<(), EncodeError> {
if version >= 3 {
types::Int32.encode(buf, &self.throttle_time_ms)?;
}
if version >= 8 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.topics)?;
}
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize, EncodeError> {
let mut total_size = 0;
if version >= 3 {
total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
}
if version >= 8 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
}
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
impl Decodable for OffsetCommitResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self, DecodeError> {
let throttle_time_ms = if version >= 3 {
types::Int32.decode(buf)?
} else {
0
};
let topics = if version >= 8 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 8 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
throttle_time_ms,
topics,
unknown_tagged_fields,
})
}
}
impl Default for OffsetCommitResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
topics: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for OffsetCommitResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 9 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct OffsetCommitResponsePartition {
pub partition_index: i32,
pub error_code: i16,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl Builder for OffsetCommitResponsePartition {
type Builder = OffsetCommitResponsePartitionBuilder;
fn builder() -> Self::Builder {
OffsetCommitResponsePartitionBuilder::default()
}
}
impl Encodable for OffsetCommitResponsePartition {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<(), EncodeError> {
types::Int32.encode(buf, &self.partition_index)?;
types::Int16.encode(buf, &self.error_code)?;
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize, EncodeError> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.partition_index)?;
total_size += types::Int16.compute_size(&self.error_code)?;
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
impl Decodable for OffsetCommitResponsePartition {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self, DecodeError> {
let partition_index = types::Int32.decode(buf)?;
let error_code = types::Int16.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 8 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
partition_index,
error_code,
unknown_tagged_fields,
})
}
}
impl Default for OffsetCommitResponsePartition {
fn default() -> Self {
Self {
partition_index: 0,
error_code: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for OffsetCommitResponsePartition {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 9 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct OffsetCommitResponseTopic {
pub name: super::TopicName,
pub partitions: Vec<OffsetCommitResponsePartition>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl Builder for OffsetCommitResponseTopic {
type Builder = OffsetCommitResponseTopicBuilder;
fn builder() -> Self::Builder {
OffsetCommitResponseTopicBuilder::default()
}
}
impl Encodable for OffsetCommitResponseTopic {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<(), EncodeError> {
if version >= 8 {
types::CompactString.encode(buf, &self.name)?;
} else {
types::String.encode(buf, &self.name)?;
}
if version >= 8 {
types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
}
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize, EncodeError> {
let mut total_size = 0;
if version >= 8 {
total_size += types::CompactString.compute_size(&self.name)?;
} else {
total_size += types::String.compute_size(&self.name)?;
}
if version >= 8 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
}
if version >= 8 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
impl Decodable for OffsetCommitResponseTopic {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self, DecodeError> {
let name = if version >= 8 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let partitions = if version >= 8 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 8 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
name,
partitions,
unknown_tagged_fields,
})
}
}
impl Default for OffsetCommitResponseTopic {
fn default() -> Self {
Self {
name: Default::default(),
partitions: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for OffsetCommitResponseTopic {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 9 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for OffsetCommitResponse {
fn header_version(version: i16) -> i16 {
if version >= 8 {
1
} else {
0
}
}
}