#![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 DescribeProducersResponse {
pub throttle_time_ms: i32,
pub topics: Vec<TopicResponse>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl DescribeProducersResponse {
pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
self.throttle_time_ms = value;
self
}
pub fn with_topics(mut self, value: Vec<TopicResponse>) -> Self {
self.topics = 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 DescribeProducersResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.throttle_time_ms)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
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)?;
total_size += types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
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 DescribeProducersResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let throttle_time_ms = types::Int32.decode(buf)?;
let topics = types::CompactArray(types::Struct { version }).decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 DescribeProducersResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
topics: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for DescribeProducersResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct PartitionResponse {
pub partition_index: i32,
pub error_code: i16,
pub error_message: Option<StrBytes>,
pub active_producers: Vec<ProducerState>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl PartitionResponse {
pub fn with_partition_index(mut self, value: i32) -> Self {
self.partition_index = value;
self
}
pub fn with_error_code(mut self, value: i16) -> Self {
self.error_code = value;
self
}
pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
self.error_message = value;
self
}
pub fn with_active_producers(mut self, value: Vec<ProducerState>) -> Self {
self.active_producers = 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 PartitionResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.partition_index)?;
types::Int16.encode(buf, &self.error_code)?;
types::CompactString.encode(buf, &self.error_message)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.active_producers)?;
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.error_code)?;
total_size += types::CompactString.compute_size(&self.error_message)?;
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.active_producers)?;
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 PartitionResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let partition_index = types::Int32.decode(buf)?;
let error_code = types::Int16.decode(buf)?;
let error_message = types::CompactString.decode(buf)?;
let active_producers = types::CompactArray(types::Struct { version }).decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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,
error_message,
active_producers,
unknown_tagged_fields,
})
}
}
impl Default for PartitionResponse {
fn default() -> Self {
Self {
partition_index: 0,
error_code: 0,
error_message: None,
active_producers: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for PartitionResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct ProducerState {
pub producer_id: super::ProducerId,
pub producer_epoch: i32,
pub last_sequence: i32,
pub last_timestamp: i64,
pub coordinator_epoch: i32,
pub current_txn_start_offset: i64,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl ProducerState {
pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
self.producer_id = value;
self
}
pub fn with_producer_epoch(mut self, value: i32) -> Self {
self.producer_epoch = value;
self
}
pub fn with_last_sequence(mut self, value: i32) -> Self {
self.last_sequence = value;
self
}
pub fn with_last_timestamp(mut self, value: i64) -> Self {
self.last_timestamp = value;
self
}
pub fn with_coordinator_epoch(mut self, value: i32) -> Self {
self.coordinator_epoch = value;
self
}
pub fn with_current_txn_start_offset(mut self, value: i64) -> Self {
self.current_txn_start_offset = 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 ProducerState {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int64.encode(buf, &self.producer_id)?;
types::Int32.encode(buf, &self.producer_epoch)?;
types::Int32.encode(buf, &self.last_sequence)?;
types::Int64.encode(buf, &self.last_timestamp)?;
types::Int32.encode(buf, &self.coordinator_epoch)?;
types::Int64.encode(buf, &self.current_txn_start_offset)?;
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::Int64.compute_size(&self.producer_id)?;
total_size += types::Int32.compute_size(&self.producer_epoch)?;
total_size += types::Int32.compute_size(&self.last_sequence)?;
total_size += types::Int64.compute_size(&self.last_timestamp)?;
total_size += types::Int32.compute_size(&self.coordinator_epoch)?;
total_size += types::Int64.compute_size(&self.current_txn_start_offset)?;
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 ProducerState {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let producer_id = types::Int64.decode(buf)?;
let producer_epoch = types::Int32.decode(buf)?;
let last_sequence = types::Int32.decode(buf)?;
let last_timestamp = types::Int64.decode(buf)?;
let coordinator_epoch = types::Int32.decode(buf)?;
let current_txn_start_offset = types::Int64.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 {
producer_id,
producer_epoch,
last_sequence,
last_timestamp,
coordinator_epoch,
current_txn_start_offset,
unknown_tagged_fields,
})
}
}
impl Default for ProducerState {
fn default() -> Self {
Self {
producer_id: (0).into(),
producer_epoch: 0,
last_sequence: -1,
last_timestamp: -1,
coordinator_epoch: 0,
current_txn_start_offset: -1,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for ProducerState {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct TopicResponse {
pub name: super::TopicName,
pub partitions: Vec<PartitionResponse>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl TopicResponse {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_partitions(mut self, value: Vec<PartitionResponse>) -> Self {
self.partitions = 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 TopicResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::CompactString.encode(buf, &self.name)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
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::CompactString.compute_size(&self.name)?;
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
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 TopicResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = types::CompactString.decode(buf)?;
let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 TopicResponse {
fn default() -> Self {
Self {
name: Default::default(),
partitions: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for TopicResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for DescribeProducersResponse {
fn header_version(version: i16) -> i16 {
1
}
}