#![allow(unused)]
use std::borrow::Borrow;
use std::collections::BTreeMap;
use anyhow::{bail, Result};
use bytes::Bytes;
use uuid::Uuid;
use crate::protocol::{
buf::{ByteBuf, ByteBufMut},
compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct AddPartitionsToTxnPartitionResult {
pub partition_index: i32,
pub partition_error_code: i16,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl AddPartitionsToTxnPartitionResult {
pub fn with_partition_index(mut self, value: i32) -> Self {
self.partition_index = value;
self
}
pub fn with_partition_error_code(mut self, value: i16) -> Self {
self.partition_error_code = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for AddPartitionsToTxnPartitionResult {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.partition_index)?;
types::Int16.encode(buf, &self.partition_error_code)?;
if version >= 3 {
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> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.partition_index)?;
total_size += types::Int16.compute_size(&self.partition_error_code)?;
if version >= 3 {
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)
}
}
#[cfg(feature = "client")]
impl Decodable for AddPartitionsToTxnPartitionResult {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let partition_index = types::Int32.decode(buf)?;
let partition_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 unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
partition_index,
partition_error_code,
unknown_tagged_fields,
})
}
}
impl Default for AddPartitionsToTxnPartitionResult {
fn default() -> Self {
Self {
partition_index: 0,
partition_error_code: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnPartitionResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct AddPartitionsToTxnResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub results_by_transaction: Vec<AddPartitionsToTxnResult>,
pub results_by_topic_v3_and_below: Vec<AddPartitionsToTxnTopicResult>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl AddPartitionsToTxnResponse {
pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
self.throttle_time_ms = value;
self
}
pub fn with_error_code(mut self, value: i16) -> Self {
self.error_code = value;
self
}
pub fn with_results_by_transaction(mut self, value: Vec<AddPartitionsToTxnResult>) -> Self {
self.results_by_transaction = value;
self
}
pub fn with_results_by_topic_v3_and_below(
mut self,
value: Vec<AddPartitionsToTxnTopicResult>,
) -> Self {
self.results_by_topic_v3_and_below = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for AddPartitionsToTxnResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.throttle_time_ms)?;
if version >= 4 {
types::Int16.encode(buf, &self.error_code)?;
}
if version >= 4 {
types::CompactArray(types::Struct { version })
.encode(buf, &self.results_by_transaction)?;
} else {
if !self.results_by_transaction.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
if version >= 3 {
types::CompactArray(types::Struct { version })
.encode(buf, &self.results_by_topic_v3_and_below)?;
} else {
types::Array(types::Struct { version })
.encode(buf, &self.results_by_topic_v3_and_below)?;
}
} else {
if !self.results_by_topic_v3_and_below.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
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> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
if version >= 4 {
total_size += types::Int16.compute_size(&self.error_code)?;
}
if version >= 4 {
total_size += types::CompactArray(types::Struct { version })
.compute_size(&self.results_by_transaction)?;
} else {
if !self.results_by_transaction.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
if version >= 3 {
total_size += types::CompactArray(types::Struct { version })
.compute_size(&self.results_by_topic_v3_and_below)?;
} else {
total_size += types::Array(types::Struct { version })
.compute_size(&self.results_by_topic_v3_and_below)?;
}
} else {
if !self.results_by_topic_v3_and_below.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
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)
}
}
#[cfg(feature = "client")]
impl Decodable for AddPartitionsToTxnResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let throttle_time_ms = types::Int32.decode(buf)?;
let error_code = if version >= 4 {
types::Int16.decode(buf)?
} else {
0
};
let results_by_transaction = if version >= 4 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
Default::default()
};
let results_by_topic_v3_and_below = if version <= 3 {
if version >= 3 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
}
} else {
Default::default()
};
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 unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
throttle_time_ms,
error_code,
results_by_transaction,
results_by_topic_v3_and_below,
unknown_tagged_fields,
})
}
}
impl Default for AddPartitionsToTxnResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
error_code: 0,
results_by_transaction: Default::default(),
results_by_topic_v3_and_below: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct AddPartitionsToTxnResult {
pub transactional_id: super::TransactionalId,
pub topic_results: Vec<AddPartitionsToTxnTopicResult>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl AddPartitionsToTxnResult {
pub fn with_transactional_id(mut self, value: super::TransactionalId) -> Self {
self.transactional_id = value;
self
}
pub fn with_topic_results(mut self, value: Vec<AddPartitionsToTxnTopicResult>) -> Self {
self.topic_results = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for AddPartitionsToTxnResult {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 4 {
types::CompactString.encode(buf, &self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topic_results)?;
} else {
if !self.topic_results.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
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> {
let mut total_size = 0;
if version >= 4 {
total_size += types::CompactString.compute_size(&self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topic_results)?;
} else {
if !self.topic_results.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
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)
}
}
#[cfg(feature = "client")]
impl Decodable for AddPartitionsToTxnResult {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let transactional_id = if version >= 4 {
types::CompactString.decode(buf)?
} else {
Default::default()
};
let topic_results = if version >= 4 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
Default::default()
};
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 unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
transactional_id,
topic_results,
unknown_tagged_fields,
})
}
}
impl Default for AddPartitionsToTxnResult {
fn default() -> Self {
Self {
transactional_id: Default::default(),
topic_results: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct AddPartitionsToTxnTopicResult {
pub name: super::TopicName,
pub results_by_partition: Vec<AddPartitionsToTxnPartitionResult>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl AddPartitionsToTxnTopicResult {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_results_by_partition(
mut self,
value: Vec<AddPartitionsToTxnPartitionResult>,
) -> Self {
self.results_by_partition = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for AddPartitionsToTxnTopicResult {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 3 {
types::CompactString.encode(buf, &self.name)?;
} else {
types::String.encode(buf, &self.name)?;
}
if version >= 3 {
types::CompactArray(types::Struct { version })
.encode(buf, &self.results_by_partition)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.results_by_partition)?;
}
if version >= 3 {
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> {
let mut total_size = 0;
if version >= 3 {
total_size += types::CompactString.compute_size(&self.name)?;
} else {
total_size += types::String.compute_size(&self.name)?;
}
if version >= 3 {
total_size += types::CompactArray(types::Struct { version })
.compute_size(&self.results_by_partition)?;
} else {
total_size +=
types::Array(types::Struct { version }).compute_size(&self.results_by_partition)?;
}
if version >= 3 {
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)
}
}
#[cfg(feature = "client")]
impl Decodable for AddPartitionsToTxnTopicResult {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = if version >= 3 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let results_by_partition = 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 unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
name,
results_by_partition,
unknown_tagged_fields,
})
}
}
impl Default for AddPartitionsToTxnTopicResult {
fn default() -> Self {
Self {
name: Default::default(),
results_by_partition: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for AddPartitionsToTxnTopicResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for AddPartitionsToTxnResponse {
fn header_version(version: i16) -> i16 {
if version >= 3 {
1
} else {
0
}
}
}