#![allow(unused)]
use std::borrow::Borrow;
use std::collections::BTreeMap;
use bytes::Bytes;
use log::error;
use uuid::Uuid;
use crate::protocol::{
Encodable, Decodable, MapEncodable, MapDecodable, Encoder, Decoder, EncodeError, DecodeError, Message, HeaderVersion, VersionRange,
types, write_unknown_tagged_fields, compute_unknown_tagged_fields_size, StrBytes, buf::{ByteBuf, ByteBufMut}, Builder
};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct AddPartitionsToTxnPartitionResult {
pub error_code: i16,
pub unknown_tagged_fields: BTreeMap<i32, Vec<u8>>,
}
impl Builder for AddPartitionsToTxnPartitionResult {
type Builder = AddPartitionsToTxnPartitionResultBuilder;
fn builder() -> Self::Builder{
AddPartitionsToTxnPartitionResultBuilder::default()
}
}
impl MapEncodable for AddPartitionsToTxnPartitionResult {
type Key = i32;
fn encode<B: ByteBufMut>(&self, key: &Self::Key, buf: &mut B, version: i16) -> Result<(), EncodeError> {
types::Int32.encode(buf, key)?;
types::Int16.encode(buf, &self.error_code)?;
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, key: &Self::Key, version: i16) -> Result<usize, EncodeError> {
let mut total_size = 0;
total_size += types::Int32.compute_size(key)?;
total_size += types::Int16.compute_size(&self.error_code)?;
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
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 MapDecodable for AddPartitionsToTxnPartitionResult {
type Key = i32;
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<(Self::Key, Self), DecodeError> {
let key_field = types::Int32.decode(buf)?;
let error_code = types::Int16.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 3 {
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 mut unknown_value = vec![0; size as usize];
buf.try_copy_to_slice(&mut unknown_value)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok((key_field, Self {
error_code,
unknown_tagged_fields,
}))
}
}
impl Default for AddPartitionsToTxnPartitionResult {
fn default() -> Self {
Self {
error_code: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnPartitionResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct AddPartitionsToTxnTopicResult {
pub results: indexmap::IndexMap<i32, AddPartitionsToTxnPartitionResult>,
pub unknown_tagged_fields: BTreeMap<i32, Vec<u8>>,
}
impl Builder for AddPartitionsToTxnTopicResult {
type Builder = AddPartitionsToTxnTopicResultBuilder;
fn builder() -> Self::Builder{
AddPartitionsToTxnTopicResultBuilder::default()
}
}
impl MapEncodable for AddPartitionsToTxnTopicResult {
type Key = super::TopicName;
fn encode<B: ByteBufMut>(&self, key: &Self::Key, buf: &mut B, version: i16) -> Result<(), EncodeError> {
if version >= 3 {
types::CompactString.encode(buf, key)?;
} else {
types::String.encode(buf, key)?;
}
if version >= 3 {
types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.results)?;
}
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, key: &Self::Key, version: i16) -> Result<usize, EncodeError> {
let mut total_size = 0;
if version >= 3 {
total_size += types::CompactString.compute_size(key)?;
} else {
total_size += types::String.compute_size(key)?;
}
if version >= 3 {
total_size += types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.results)?;
}
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
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 MapDecodable for AddPartitionsToTxnTopicResult {
type Key = super::TopicName;
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<(Self::Key, Self), DecodeError> {
let key_field = if version >= 3 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let results = if version >= 3 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 3 {
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 mut unknown_value = vec![0; size as usize];
buf.try_copy_to_slice(&mut unknown_value)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok((key_field, Self {
results,
unknown_tagged_fields,
}))
}
}
impl Default for AddPartitionsToTxnTopicResult {
fn default() -> Self {
Self {
results: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnTopicResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, derive_builder::Builder)]
#[builder(default)]
pub struct AddPartitionsToTxnResponse {
pub throttle_time_ms: i32,
pub results: indexmap::IndexMap<super::TopicName, AddPartitionsToTxnTopicResult>,
pub unknown_tagged_fields: BTreeMap<i32, Vec<u8>>,
}
impl Builder for AddPartitionsToTxnResponse {
type Builder = AddPartitionsToTxnResponseBuilder;
fn builder() -> Self::Builder{
AddPartitionsToTxnResponseBuilder::default()
}
}
impl Encodable for AddPartitionsToTxnResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<(), EncodeError> {
types::Int32.encode(buf, &self.throttle_time_ms)?;
if version >= 3 {
types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.results)?;
}
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
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.throttle_time_ms)?;
if version >= 3 {
total_size += types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.results)?;
}
if version >= 3 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
error!("Too many tagged fields to encode ({} fields)", num_tagged_fields);
return Err(EncodeError);
}
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 AddPartitionsToTxnResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self, DecodeError> {
let throttle_time_ms = types::Int32.decode(buf)?;
let results = if version >= 3 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 3 {
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 mut unknown_value = vec![0; size as usize];
buf.try_copy_to_slice(&mut unknown_value)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
throttle_time_ms,
results,
unknown_tagged_fields,
})
}
}
impl Default for AddPartitionsToTxnResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
results: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
}
impl HeaderVersion for AddPartitionsToTxnResponse {
fn header_version(version: i16) -> i16 {
if version >= 3 {
1
} else {
0
}
}
}