#![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 PartitionProduceData {
pub index: i32,
pub records: Option<Bytes>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl PartitionProduceData {
pub fn with_index(mut self, value: i32) -> Self {
self.index = value;
self
}
pub fn with_records(mut self, value: Option<Bytes>) -> Self {
self.records = 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 = "client")]
impl Encodable for PartitionProduceData {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.index)?;
if version >= 9 {
types::CompactBytes.encode(buf, &self.records)?;
} else {
types::Bytes.encode(buf, &self.records)?;
}
if version >= 9 {
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.index)?;
if version >= 9 {
total_size += types::CompactBytes.compute_size(&self.records)?;
} else {
total_size += types::Bytes.compute_size(&self.records)?;
}
if version >= 9 {
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 = "broker")]
impl Decodable for PartitionProduceData {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let index = types::Int32.decode(buf)?;
let records = if version >= 9 {
types::CompactBytes.decode(buf)?
} else {
types::Bytes.decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 9 {
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 {
index,
records,
unknown_tagged_fields,
})
}
}
impl Default for PartitionProduceData {
fn default() -> Self {
Self {
index: 0,
records: Some(Default::default()),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for PartitionProduceData {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 11 };
const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 6 });
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct ProduceRequest {
pub transactional_id: Option<super::TransactionalId>,
pub acks: i16,
pub timeout_ms: i32,
pub topic_data: Vec<TopicProduceData>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl ProduceRequest {
pub fn with_transactional_id(mut self, value: Option<super::TransactionalId>) -> Self {
self.transactional_id = value;
self
}
pub fn with_acks(mut self, value: i16) -> Self {
self.acks = value;
self
}
pub fn with_timeout_ms(mut self, value: i32) -> Self {
self.timeout_ms = value;
self
}
pub fn with_topic_data(mut self, value: Vec<TopicProduceData>) -> Self {
self.topic_data = 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 = "client")]
impl Encodable for ProduceRequest {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 3 {
if version >= 9 {
types::CompactString.encode(buf, &self.transactional_id)?;
} else {
types::String.encode(buf, &self.transactional_id)?;
}
} else {
if !self.transactional_id.is_none() {
bail!("A field is set that is not available on the selected protocol version");
}
}
types::Int16.encode(buf, &self.acks)?;
types::Int32.encode(buf, &self.timeout_ms)?;
if version >= 9 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topic_data)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.topic_data)?;
}
if version >= 9 {
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 {
if version >= 9 {
total_size += types::CompactString.compute_size(&self.transactional_id)?;
} else {
total_size += types::String.compute_size(&self.transactional_id)?;
}
} else {
if !self.transactional_id.is_none() {
bail!("A field is set that is not available on the selected protocol version");
}
}
total_size += types::Int16.compute_size(&self.acks)?;
total_size += types::Int32.compute_size(&self.timeout_ms)?;
if version >= 9 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topic_data)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.topic_data)?;
}
if version >= 9 {
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 = "broker")]
impl Decodable for ProduceRequest {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let transactional_id = if version >= 3 {
if version >= 9 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
}
} else {
None
};
let acks = types::Int16.decode(buf)?;
let timeout_ms = types::Int32.decode(buf)?;
let topic_data = if version >= 9 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 9 {
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,
acks,
timeout_ms,
topic_data,
unknown_tagged_fields,
})
}
}
impl Default for ProduceRequest {
fn default() -> Self {
Self {
transactional_id: None,
acks: 0,
timeout_ms: 0,
topic_data: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for ProduceRequest {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 11 };
const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 6 });
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct TopicProduceData {
pub name: super::TopicName,
pub partition_data: Vec<PartitionProduceData>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl TopicProduceData {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_partition_data(mut self, value: Vec<PartitionProduceData>) -> Self {
self.partition_data = 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 = "client")]
impl Encodable for TopicProduceData {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 9 {
types::CompactString.encode(buf, &self.name)?;
} else {
types::String.encode(buf, &self.name)?;
}
if version >= 9 {
types::CompactArray(types::Struct { version }).encode(buf, &self.partition_data)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.partition_data)?;
}
if version >= 9 {
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 >= 9 {
total_size += types::CompactString.compute_size(&self.name)?;
} else {
total_size += types::String.compute_size(&self.name)?;
}
if version >= 9 {
total_size += types::CompactArray(types::Struct { version })
.compute_size(&self.partition_data)?;
} else {
total_size +=
types::Array(types::Struct { version }).compute_size(&self.partition_data)?;
}
if version >= 9 {
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 = "broker")]
impl Decodable for TopicProduceData {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = if version >= 9 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let partition_data = if version >= 9 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 9 {
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,
partition_data,
unknown_tagged_fields,
})
}
}
impl Default for TopicProduceData {
fn default() -> Self {
Self {
name: Default::default(),
partition_data: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for TopicProduceData {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 11 };
const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 6 });
}
impl HeaderVersion for ProduceRequest {
fn header_version(version: i16) -> i16 {
if version >= 9 {
2
} else {
1
}
}
}