use bytes::{BufMut, Bytes};
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::{put_compact_nullable_bytes, put_nullable_bytes};
use crate::primitives::string_bytes_borrowed::{
get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed,
};
use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 3;
pub const FLEXIBLE_MIN: i16 = 32767;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ConsumerProtocolAssignment<'a> {
pub assigned_partitions: Vec<TopicPartition<'a>>,
pub user_data: Option<&'a [u8]>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl ConsumerProtocolAssignment<'_> {
pub fn to_owned(
&self,
) -> crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
assigned_partitions: (self.assigned_partitions)
.iter()
.map(TopicPartition::to_owned)
.collect(),
user_data: (self.user_data).map(Bytes::copy_from_slice),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for ConsumerProtocolAssignment<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"ConsumerProtocolAssignment version out of range",
));
}
let flex = is_flexible(version);
if version >= 0 {
{
crate::primitives::array::put_array_len(
buf,
(self.assigned_partitions).len(),
flex,
);
for it in &self.assigned_partitions {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
if flex {
put_compact_nullable_bytes(buf, self.user_data);
} else {
put_nullable_bytes(buf, self.user_data);
}
}
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.assigned_partitions).len(),
flex,
);
let body: usize = (self.assigned_partitions)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if version >= 0 {
n += match self.user_data {
None => {
if flex {
1
} else {
4
}
}
Some(b) => {
if flex {
crate::primitives::varint::uvarint_len(u32::try_from(b.len() + 1).unwrap())
+ b.len()
} else {
4 + b.len()
}
}
};
}
n
}
}
impl<'de> DecodeBorrow<'de> for ConsumerProtocolAssignment<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"ConsumerProtocolAssignment version out of range",
));
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 {
out.assigned_partitions = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(TopicPartition::decode_borrow(buf, version)?);
}
v
};
}
if version >= 0 {
out.user_data = if flex {
get_compact_nullable_bytes_borrowed(buf)?
} else {
get_nullable_bytes_borrowed(buf)?
};
}
Ok(out)
}
}
#[cfg(test)]
impl ConsumerProtocolAssignment<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.assigned_partitions = vec![TopicPartition::populated(version)];
}
if version >= 0 {
m.user_data = Some(&b"x"[..]);
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicPartition<'a> {
pub topic: &'a str,
pub partitions: Vec<i32>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl TopicPartition<'_> {
pub fn to_owned(&self) -> crate::owned::consumer_protocol_assignment::TopicPartition {
crate::owned::consumer_protocol_assignment::TopicPartition {
topic: (self.topic).to_string(),
partitions: (self.partitions).clone(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for TopicPartition<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 32767;
if version >= 0 {
if flex {
put_compact_string(buf, self.topic);
} else {
put_string(buf, self.topic);
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
for it in &self.partitions {
put_i32(buf, *it);
}
}
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 32767;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.topic)
} else {
string_len(self.topic)
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
let body: usize = (self.partitions).iter().map(|_| 4).sum();
prefix + body
};
}
n
}
}
impl<'de> DecodeBorrow<'de> for TopicPartition<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 32767;
let mut out = Self::default();
if version >= 0 {
out.topic = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.partitions = {
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
};
}
Ok(out)
}
}
#[cfg(test)]
impl TopicPartition<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.topic = "x";
}
if version >= 0 {
m.partitions = vec![1i32];
}
m
}
}