use bytes::BufMut;
use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
use crate::primitives::string_bytes::{
compact_nullable_string_len, compact_string_len, nullable_string_len,
put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
};
use crate::primitives::string_bytes_borrowed::{
get_compact_nullable_string_borrowed, get_compact_string_borrowed,
get_nullable_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 = 88;
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, Default)]
pub struct StreamsGroupHeartbeatResponse<'a> {
pub throttle_time_ms: i32,
pub error_code: i16,
pub error_message: Option<&'a str>,
pub member_id: &'a str,
pub member_epoch: i32,
pub heartbeat_interval_ms: i32,
pub acceptable_recovery_lag: i32,
pub task_offset_interval_ms: i32,
pub status: Option<Vec<super::common::streams_group_heartbeat_response::status::Status<'a>>>,
pub active_tasks:
Option<Vec<super::common::streams_group_heartbeat_response::task_ids::TaskIds<'a>>>,
pub standby_tasks:
Option<Vec<super::common::streams_group_heartbeat_response::task_ids::TaskIds<'a>>>,
pub warmup_tasks:
Option<Vec<super::common::streams_group_heartbeat_response::task_ids::TaskIds<'a>>>,
pub endpoint_information_epoch: i32,
pub partitions_by_user_endpoint: Option<Vec<EndpointToPartitions<'a>>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl StreamsGroupHeartbeatResponse<'_> {
pub fn to_owned(
&self,
) -> crate::owned::streams_group_heartbeat_response::StreamsGroupHeartbeatResponse {
crate::owned::streams_group_heartbeat_response::StreamsGroupHeartbeatResponse {
throttle_time_ms: (self.throttle_time_ms),
error_code: (self.error_code),
error_message: (self.error_message).map(std::string::ToString::to_string),
member_id: (self.member_id).to_string(),
member_epoch: (self.member_epoch),
heartbeat_interval_ms: (self.heartbeat_interval_ms),
acceptable_recovery_lag: (self.acceptable_recovery_lag),
task_offset_interval_ms: (self.task_offset_interval_ms),
status: (self.status)
.as_ref()
.map(|v| v.iter().map(super::common::streams_group_heartbeat_response::status::Status::to_owned).collect()),
active_tasks: (self.active_tasks)
.as_ref()
.map(|v| v.iter().map(super::common::streams_group_heartbeat_response::task_ids::TaskIds::to_owned).collect()),
standby_tasks: (self.standby_tasks)
.as_ref()
.map(|v| v.iter().map(super::common::streams_group_heartbeat_response::task_ids::TaskIds::to_owned).collect()),
warmup_tasks: (self.warmup_tasks)
.as_ref()
.map(|v| v.iter().map(super::common::streams_group_heartbeat_response::task_ids::TaskIds::to_owned).collect()),
endpoint_information_epoch: (self.endpoint_information_epoch),
partitions_by_user_endpoint: (self.partitions_by_user_endpoint)
.as_ref()
.map(|v| v.iter().map(EndpointToPartitions::to_owned).collect()),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for StreamsGroupHeartbeatResponse<'_> {
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.throttle_time_ms);
}
if version >= 0 {
put_i16(buf, self.error_code);
}
if version >= 0 {
if flex {
put_compact_nullable_string(buf, self.error_message);
} else {
put_nullable_string(buf, self.error_message);
}
}
if version >= 0 {
if flex {
put_compact_string(buf, self.member_id);
} else {
put_string(buf, self.member_id);
}
}
if version >= 0 {
put_i32(buf, self.member_epoch);
}
if version >= 0 {
put_i32(buf, self.heartbeat_interval_ms);
}
if version >= 0 {
put_i32(buf, self.acceptable_recovery_lag);
}
if version >= 0 {
put_i32(buf, self.task_offset_interval_ms);
}
if version >= 0 {
{
let len = (self.status).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.status {
for it in v {
it.encode(buf, version)?;
}
}
}
}
if version >= 0 {
{
let len = (self.active_tasks).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.active_tasks {
for it in v {
it.encode(buf, version)?;
}
}
}
}
if version >= 0 {
{
let len = (self.standby_tasks).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.standby_tasks {
for it in v {
it.encode(buf, version)?;
}
}
}
}
if version >= 0 {
{
let len = (self.warmup_tasks).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.warmup_tasks {
for it in v {
it.encode(buf, version)?;
}
}
}
}
if version >= 0 {
put_i32(buf, self.endpoint_information_epoch);
}
if version >= 0 {
{
let len = (self.partitions_by_user_endpoint).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.partitions_by_user_endpoint {
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 += 2;
}
if version >= 0 {
n += if flex {
compact_nullable_string_len(self.error_message)
} else {
nullable_string_len(self.error_message)
};
}
if version >= 0 {
n += if flex {
compact_string_len(self.member_id)
} else {
string_len(self.member_id)
};
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.status).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 version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.active_tasks).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 version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.standby_tasks).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 version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.warmup_tasks).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 version >= 0 {
n += 4;
}
if version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.partitions_by_user_endpoint).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 StreamsGroupHeartbeatResponse<'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.throttle_time_ms = get_i32(buf)?;
}
if version >= 0 {
out.error_code = get_i16(buf)?;
}
if version >= 0 {
out.error_message = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
if version >= 0 {
out.member_id = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.member_epoch = get_i32(buf)?;
}
if version >= 0 {
out.heartbeat_interval_ms = get_i32(buf)?;
}
if version >= 0 {
out.acceptable_recovery_lag = get_i32(buf)?;
}
if version >= 0 {
out.task_offset_interval_ms = get_i32(buf)?;
}
if version >= 0 {
out.status = {
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 (super :: common :: streams_group_heartbeat_response :: status :: Status :: decode_borrow (buf , version) ?) ;
}
Some(v)
}
}
};
}
if version >= 0 {
out.active_tasks = {
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 (super :: common :: streams_group_heartbeat_response :: task_ids :: TaskIds :: decode_borrow (buf , version) ?) ;
}
Some(v)
}
}
};
}
if version >= 0 {
out.standby_tasks = {
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 (super :: common :: streams_group_heartbeat_response :: task_ids :: TaskIds :: decode_borrow (buf , version) ?) ;
}
Some(v)
}
}
};
}
if version >= 0 {
out.warmup_tasks = {
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 (super :: common :: streams_group_heartbeat_response :: task_ids :: TaskIds :: decode_borrow (buf , version) ?) ;
}
Some(v)
}
}
};
}
if version >= 0 {
out.endpoint_information_epoch = get_i32(buf)?;
}
if version >= 0 {
out.partitions_by_user_endpoint = {
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(EndpointToPartitions::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 StreamsGroupHeartbeatResponse<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.throttle_time_ms = 1i32;
}
if version >= 0 {
m.error_code = 1i16;
}
if version >= 0 {
m.error_message = Some("x");
}
if version >= 0 {
m.member_id = "x";
}
if version >= 0 {
m.member_epoch = 1i32;
}
if version >= 0 {
m.heartbeat_interval_ms = 1i32;
}
if version >= 0 {
m.acceptable_recovery_lag = 1i32;
}
if version >= 0 {
m.task_offset_interval_ms = 1i32;
}
if version >= 0 {
m.status = Some(vec![
super::common::streams_group_heartbeat_response::status::Status::populated(version),
]);
}
if version >= 0 {
m.active_tasks = Some(vec![
super::common::streams_group_heartbeat_response::task_ids::TaskIds::populated(
version,
),
]);
}
if version >= 0 {
m.standby_tasks = Some(vec![
super::common::streams_group_heartbeat_response::task_ids::TaskIds::populated(
version,
),
]);
}
if version >= 0 {
m.warmup_tasks = Some(vec![
super::common::streams_group_heartbeat_response::task_ids::TaskIds::populated(
version,
),
]);
}
if version >= 0 {
m.endpoint_information_epoch = 1i32;
}
if version >= 0 {
m.partitions_by_user_endpoint = Some(vec![EndpointToPartitions::populated(version)]);
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EndpointToPartitions<'a> {
pub user_endpoint: super::common::streams_group_heartbeat_response::endpoint::Endpoint<'a>,
pub active_partitions:
Vec<super::common::streams_group_heartbeat_response::topic_partition::TopicPartition<'a>>,
pub standby_partitions:
Vec<super::common::streams_group_heartbeat_response::topic_partition::TopicPartition<'a>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl EndpointToPartitions<'_> {
pub fn to_owned(&self) -> crate::owned::streams_group_heartbeat_response::EndpointToPartitions {
crate::owned::streams_group_heartbeat_response::EndpointToPartitions {
user_endpoint: (self.user_endpoint).to_owned(),
active_partitions: (self.active_partitions)
.iter()
.map(super::common::streams_group_heartbeat_response::topic_partition::TopicPartition::to_owned)
.collect(),
standby_partitions: (self.standby_partitions)
.iter()
.map(super::common::streams_group_heartbeat_response::topic_partition::TopicPartition::to_owned)
.collect(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for EndpointToPartitions<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
self.user_endpoint.encode(buf, version)?;
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.active_partitions).len(), flex);
for it in &self.active_partitions {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.standby_partitions).len(), flex);
for it in &self.standby_partitions {
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 = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += self.user_endpoint.encoded_len(version);
}
if version >= 0 {
n += {
let prefix = crate::primitives::array::array_len_prefix_len(
(self.active_partitions).len(),
flex,
);
let body: usize = (self.active_partitions)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if version >= 0 {
n += {
let prefix = crate::primitives::array::array_len_prefix_len(
(self.standby_partitions).len(),
flex,
);
let body: usize = (self.standby_partitions)
.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 EndpointToPartitions<'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.user_endpoint =
super::common::streams_group_heartbeat_response::endpoint::Endpoint::decode_borrow(
buf, version,
)?;
}
if version >= 0 {
out.active_partitions = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v . push (super :: common :: streams_group_heartbeat_response :: topic_partition :: TopicPartition :: decode_borrow (buf , version) ?) ;
}
v
};
}
if version >= 0 {
out.standby_partitions = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v . push (super :: common :: streams_group_heartbeat_response :: topic_partition :: TopicPartition :: decode_borrow (buf , version) ?) ;
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl EndpointToPartitions<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.user_endpoint =
super::common::streams_group_heartbeat_response::endpoint::Endpoint::populated(
version,
);
}
if version >= 0 {
m . active_partitions = vec ! [super :: common :: streams_group_heartbeat_response :: topic_partition :: TopicPartition :: populated (version)] ;
}
if version >= 0 {
m . standby_partitions = vec ! [super :: common :: streams_group_heartbeat_response :: topic_partition :: TopicPartition :: populated (version)] ;
}
m
}
}