use bytes::BufMut;
use crate::primitives::fixed::{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 = 46;
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 ListPartitionReassignmentsRequest<'a> {
pub timeout_ms: i32,
pub topics: Option<Vec<ListPartitionReassignmentsTopics<'a>>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ListPartitionReassignmentsRequest<'_> {
fn default() -> Self {
Self {
timeout_ms: 60_000i32,
topics: None,
unknown_tagged_fields: Default::default(),
}
}
}
impl ListPartitionReassignmentsRequest<'_> {
pub fn to_owned(
&self,
) -> crate::owned::list_partition_reassignments_request::ListPartitionReassignmentsRequest {
crate::owned::list_partition_reassignments_request::ListPartitionReassignmentsRequest {
timeout_ms: (self.timeout_ms),
topics: (self.topics).as_ref().map(|v| {
v.iter()
.map(ListPartitionReassignmentsTopics::to_owned)
.collect()
}),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for ListPartitionReassignmentsRequest<'_> {
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 {
put_i32(buf, self.timeout_ms);
}
if version >= 0 {
{
let len = (self.topics).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.topics {
for it in v {
it.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 += 4;
}
if version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.topics).as_ref();
let prefix = crate::primitives::array::nullable_array_len_prefix_len(
opt.map(std::vec::Vec::len),
flex,
);
let body: usize =
opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
prefix + body
};
}
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 ListPartitionReassignmentsRequest<'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.timeout_ms = get_i32(buf)?;
}
if version >= 0 {
out.topics = {
let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
match opt {
None => None,
Some(n) => {
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(ListPartitionReassignmentsTopics::decode_borrow(
buf, version,
)?);
}
Some(v)
}
}
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl ListPartitionReassignmentsRequest<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.timeout_ms = 1i32;
}
if version >= 0 {
m.topics = Some(vec![ListPartitionReassignmentsTopics::populated(version)]);
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ListPartitionReassignmentsTopics<'a> {
pub name: &'a str,
pub partition_indexes: Vec<i32>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl ListPartitionReassignmentsTopics<'_> {
pub fn to_owned(
&self,
) -> crate::owned::list_partition_reassignments_request::ListPartitionReassignmentsTopics {
crate::owned::list_partition_reassignments_request::ListPartitionReassignmentsTopics {
name: (self.name).to_string(),
partition_indexes: (self.partition_indexes).clone(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for ListPartitionReassignmentsTopics<'_> {
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 version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.partition_indexes).len(), flex);
for it in &self.partition_indexes {
put_i32(buf, *it);
}
}
}
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 version >= 0 {
n += {
let prefix = crate::primitives::array::array_len_prefix_len(
(self.partition_indexes).len(),
flex,
);
let body: usize = (self.partition_indexes).iter().map(|_| 4).sum();
prefix + body
};
}
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 ListPartitionReassignmentsTopics<'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 version >= 0 {
out.partition_indexes = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(get_i32(buf)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl ListPartitionReassignmentsTopics<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x";
}
if version >= 0 {
m.partition_indexes = vec![1i32];
}
m
}
}