#![allow(
clippy::field_reassign_with_default,
clippy::nonminimal_bool,
clippy::derivable_impls,
clippy::borrow_deref_ref,
clippy::double_parens,
unused_parens
)]
use bytes::{Buf, BufMut};
use crate::budget::{Budget, Limits};
#[allow(unused_imports)]
use crate::error::{DecodeError, EncodeError};
use crate::wire::{self, RawTaggedField};
pub fn is_flexible(version: i16) -> bool {
let _ = version;
version >= 9
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ProduceRequest {
pub transactional_id: Option<String>,
pub acks: i16,
pub timeout_ms: i32,
pub topic_data: Vec<TopicProduceData>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ProduceRequest {
fn default() -> Self {
Self {
transactional_id: None,
acks: 0,
timeout_ms: 0,
topic_data: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl ProduceRequest {
pub const API_KEY: i16 = 0;
pub const MIN_VERSION: i16 = 3;
pub const MAX_VERSION: i16 = 13;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.transactional_id.as_deref());
} else {
wire::put_nullable_string(buf, self.transactional_id.as_deref())?;
}
buf.put_i16(self.acks);
buf.put_i32(self.timeout_ms);
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.topic_data.len()));
} else {
wire::put_array_len(buf, Some(self.topic_data.len()))?;
}
for item in &self.topic_data {
item.encode(buf, version)?;
}
if is_flexible(version) {
wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
}
Ok(())
}
pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
Self::decode_with_limits(buf, version, Limits::default())
}
pub fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
let mut budget = limits.budget(buf.remaining());
Self::decode_with_budget(buf, version, &mut budget)
}
pub fn decode_with_budget(
buf: &mut impl Buf,
version: i16,
budget: &mut Budget,
) -> Result<Self, DecodeError> {
let mut this = Self::default();
this.transactional_id = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
this.acks = wire::get_i16(buf)?;
this.timeout_ms = wire::get_i32(buf)?;
this.topic_data = {
let len = if is_flexible(version) {
wire::get_compact_array_len(buf)?
} else {
wire::get_array_len(buf)?
};
match len {
None => return Err(DecodeError::InvalidLength(-1)),
Some(n) => {
let mut items = Vec::new();
for _ in 0..n {
let mark = buf.remaining();
let item = TopicProduceData::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
impl crate::Message for ProduceRequest {
const API_KEY: i16 = 0;
const MIN_VERSION: i16 = 3;
const MAX_VERSION: i16 = 13;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
ProduceRequest::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
ProduceRequest::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
ProduceRequest::decode_with_limits(buf, version, limits)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TopicProduceData {
pub name: String,
pub topic_id: [u8; 16],
pub partition_data: Vec<PartitionProduceData>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for TopicProduceData {
fn default() -> Self {
Self {
name: String::new(),
topic_id: [0u8; 16],
partition_data: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl TopicProduceData {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version <= 12 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.name);
} else {
wire::put_string(buf, &self.name)?;
}
}
if version >= 13 {
wire::put_uuid(buf, self.topic_id);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.partition_data.len()));
} else {
wire::put_array_len(buf, Some(self.partition_data.len()))?;
}
for item in &self.partition_data {
item.encode(buf, version)?;
}
if is_flexible(version) {
wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
}
Ok(())
}
pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
Self::decode_with_limits(buf, version, Limits::default())
}
pub fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
let mut budget = limits.budget(buf.remaining());
Self::decode_with_budget(buf, version, &mut budget)
}
pub fn decode_with_budget(
buf: &mut impl Buf,
version: i16,
budget: &mut Budget,
) -> Result<Self, DecodeError> {
let mut this = Self::default();
if version <= 12 {
this.name = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 13 {
this.topic_id = wire::get_uuid(buf)?;
}
this.partition_data = {
let len = if is_flexible(version) {
wire::get_compact_array_len(buf)?
} else {
wire::get_array_len(buf)?
};
match len {
None => return Err(DecodeError::InvalidLength(-1)),
Some(n) => {
let mut items = Vec::new();
for _ in 0..n {
let mark = buf.remaining();
let item = PartitionProduceData::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct PartitionProduceData {
pub index: i32,
pub records: Option<bytes::Bytes>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for PartitionProduceData {
fn default() -> Self {
Self {
index: 0,
records: Some(bytes::Bytes::new()),
unknown_tagged_fields: Vec::new(),
}
}
}
impl PartitionProduceData {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i32(self.index);
if is_flexible(version) {
wire::put_compact_nullable_bytes(buf, self.records.as_deref());
} else {
wire::put_nullable_bytes(buf, self.records.as_deref())?;
}
if is_flexible(version) {
wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
}
Ok(())
}
pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
Self::decode_with_limits(buf, version, Limits::default())
}
pub fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
let mut budget = limits.budget(buf.remaining());
Self::decode_with_budget(buf, version, &mut budget)
}
pub fn decode_with_budget(
buf: &mut impl Buf,
version: i16,
budget: &mut Budget,
) -> Result<Self, DecodeError> {
let mut this = Self::default();
this.index = wire::get_i32(buf)?;
this.records = if is_flexible(version) {
wire::get_compact_nullable_bytes(buf)?
} else {
wire::get_nullable_bytes(buf)?
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}