#![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 >= 5
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct DescribeGroupsResponse {
pub throttle_time_ms: i32,
pub groups: Vec<DescribedGroup>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for DescribeGroupsResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
groups: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl DescribeGroupsResponse {
pub const API_KEY: i16 = 15;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 6;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 1 {
buf.put_i32(self.throttle_time_ms);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.groups.len()));
} else {
wire::put_array_len(buf, Some(self.groups.len()))?;
}
for item in &self.groups {
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 >= 1 {
this.throttle_time_ms = wire::get_i32(buf)?;
}
this.groups = {
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 = DescribedGroup::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 DescribeGroupsResponse {
const API_KEY: i16 = 15;
const MIN_VERSION: i16 = 0;
const MAX_VERSION: i16 = 6;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
DescribeGroupsResponse::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
DescribeGroupsResponse::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
DescribeGroupsResponse::decode_with_limits(buf, version, limits)
}
fn throttle_time_ms(&self) -> Option<i32> {
Some(self.throttle_time_ms)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct DescribedGroup {
pub error_code: i16,
pub error_message: Option<String>,
pub group_id: String,
pub group_state: String,
pub protocol_type: String,
pub protocol_data: String,
pub members: Vec<DescribedGroupMember>,
pub authorized_operations: i32,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for DescribedGroup {
fn default() -> Self {
Self {
error_code: 0,
error_message: None,
group_id: String::new(),
group_state: String::new(),
protocol_type: String::new(),
protocol_data: String::new(),
members: Vec::new(),
authorized_operations: -2147483648,
unknown_tagged_fields: Vec::new(),
}
}
}
impl DescribedGroup {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i16(self.error_code);
if version >= 6 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.error_message.as_deref());
} else {
wire::put_nullable_string(buf, self.error_message.as_deref())?;
}
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.group_id);
} else {
wire::put_string(buf, &self.group_id)?;
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.group_state);
} else {
wire::put_string(buf, &self.group_state)?;
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.protocol_type);
} else {
wire::put_string(buf, &self.protocol_type)?;
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.protocol_data);
} else {
wire::put_string(buf, &self.protocol_data)?;
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.members.len()));
} else {
wire::put_array_len(buf, Some(self.members.len()))?;
}
for item in &self.members {
item.encode(buf, version)?;
}
if version >= 3 {
buf.put_i32(self.authorized_operations);
}
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.error_code = wire::get_i16(buf)?;
if version >= 6 {
this.error_message = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
this.group_id = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.group_state = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.protocol_type = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.protocol_data = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.members = {
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 = DescribedGroupMember::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if version >= 3 {
this.authorized_operations = wire::get_i32(buf)?;
}
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 DescribedGroupMember {
pub member_id: String,
pub group_instance_id: Option<String>,
pub client_id: String,
pub client_host: String,
pub member_metadata: bytes::Bytes,
pub member_assignment: bytes::Bytes,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for DescribedGroupMember {
fn default() -> Self {
Self {
member_id: String::new(),
group_instance_id: None,
client_id: String::new(),
client_host: String::new(),
member_metadata: bytes::Bytes::new(),
member_assignment: bytes::Bytes::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl DescribedGroupMember {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if is_flexible(version) {
wire::put_compact_string(buf, &self.member_id);
} else {
wire::put_string(buf, &self.member_id)?;
}
if version >= 4 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
} else {
wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
}
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.client_id);
} else {
wire::put_string(buf, &self.client_id)?;
}
if is_flexible(version) {
wire::put_compact_string(buf, &self.client_host);
} else {
wire::put_string(buf, &self.client_host)?;
}
if is_flexible(version) {
wire::put_compact_bytes(buf, &self.member_metadata);
} else {
wire::put_bytes(buf, &self.member_metadata)?;
}
if is_flexible(version) {
wire::put_compact_bytes(buf, &self.member_assignment);
} else {
wire::put_bytes(buf, &self.member_assignment)?;
}
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.member_id = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
if version >= 4 {
this.group_instance_id = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
this.client_id = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.client_host = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.member_metadata = {
let raw = if is_flexible(version) {
wire::get_compact_nullable_bytes(buf)?
} else {
wire::get_nullable_bytes(buf)?
};
match raw {
Some(v) => v,
None => return Err(DecodeError::InvalidLength(-1)),
}
};
this.member_assignment = {
let raw = if is_flexible(version) {
wire::get_compact_nullable_bytes(buf)?
} else {
wire::get_nullable_bytes(buf)?
};
match raw {
Some(v) => v,
None => return Err(DecodeError::InvalidLength(-1)),
}
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}