use bytes::BufMut;
use crate::primitives::fixed::{get_i8, get_i32, put_i32};
use crate::primitives::string_bytes::{
compact_string_len, put_compact_string, put_string, string_len,
};
use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 75;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DescribeTopicPartitionsRequest<'a> {
pub topics: Vec<TopicRequest<'a>>,
pub response_partition_limit: i32,
pub cursor: Option<Cursor<'a>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for DescribeTopicPartitionsRequest<'_> {
fn default() -> Self {
Self {
topics: Vec::new(),
response_partition_limit: 2_000i32,
cursor: None,
unknown_tagged_fields: Default::default(),
}
}
}
impl DescribeTopicPartitionsRequest<'_> {
pub fn to_owned(
&self,
) -> crate::owned::describe_topic_partitions_request::DescribeTopicPartitionsRequest {
crate::owned::describe_topic_partitions_request::DescribeTopicPartitionsRequest {
topics: (self.topics).iter().map(TopicRequest::to_owned).collect(),
response_partition_limit: (self.response_partition_limit),
cursor: (self.cursor).as_ref().map(Cursor::to_owned),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for DescribeTopicPartitionsRequest<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion {
api_key: API_KEY,
version,
});
}
let flex = is_flexible(version);
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
for it in &self.topics {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
put_i32(buf, self.response_partition_limit);
}
if version >= 0 {
match &self.cursor {
None => {
buf.put_i8(-1);
}
Some(v) => {
buf.put_i8(1);
v.encode(buf, version)?;
}
}
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
prefix + body
};
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += 1 + self.cursor.as_ref().map_or(0, |v| v.encoded_len(version));
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for DescribeTopicPartitionsRequest<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion {
api_key: API_KEY,
version,
});
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 {
out.topics = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(TopicRequest::decode_borrow(buf, version)?);
}
v
};
}
if version >= 0 {
out.response_partition_limit = get_i32(buf)?;
}
if version >= 0 {
out.cursor = if get_i8(buf)? < 0 {
None
} else {
Some(Cursor::decode_borrow(buf, version)?)
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl DescribeTopicPartitionsRequest<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.topics = vec![TopicRequest::populated(version)];
}
if version >= 0 {
m.response_partition_limit = 1i32;
}
if version >= 0 {
m.cursor = Some(Cursor::populated(version));
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicRequest<'a> {
pub name: &'a str,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl TopicRequest<'_> {
pub fn to_owned(&self) -> crate::owned::describe_topic_partitions_request::TopicRequest {
crate::owned::describe_topic_partitions_request::TopicRequest {
name: (self.name).to_string(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for TopicRequest<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
if flex {
put_compact_string(buf, self.name);
} else {
put_string(buf, self.name);
}
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.name)
} else {
string_len(self.name)
};
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for TopicRequest<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.name = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl TopicRequest<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x";
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Cursor<'a> {
pub topic_name: &'a str,
pub partition_index: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Cursor<'_> {
pub fn to_owned(&self) -> crate::owned::describe_topic_partitions_request::Cursor {
crate::owned::describe_topic_partitions_request::Cursor {
topic_name: (self.topic_name).to_string(),
partition_index: (self.partition_index),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for Cursor<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
if flex {
put_compact_string(buf, self.topic_name);
} else {
put_string(buf, self.topic_name);
}
}
if version >= 0 {
put_i32(buf, self.partition_index);
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.topic_name)
} else {
string_len(self.topic_name)
};
}
if version >= 0 {
n += 4;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for Cursor<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.topic_name = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.partition_index = get_i32(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl Cursor<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.topic_name = "x";
}
if version >= 0 {
m.partition_index = 1i32;
}
m
}
}