#![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 >= 3
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListGroupsResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub groups: Vec<ListedGroup>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ListGroupsResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
error_code: 0,
groups: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl ListGroupsResponse {
pub const API_KEY: i16 = 16;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 5;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 1 {
buf.put_i32(self.throttle_time_ms);
}
buf.put_i16(self.error_code);
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.error_code = wire::get_i16(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 = ListedGroup::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 ListGroupsResponse {
const API_KEY: i16 = 16;
const MIN_VERSION: i16 = 0;
const MAX_VERSION: i16 = 5;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
ListGroupsResponse::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
ListGroupsResponse::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
ListGroupsResponse::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 ListedGroup {
pub group_id: String,
pub protocol_type: String,
pub group_state: String,
pub group_type: String,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ListedGroup {
fn default() -> Self {
Self {
group_id: String::new(),
protocol_type: String::new(),
group_state: String::new(),
group_type: String::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl ListedGroup {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
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.protocol_type);
} else {
wire::put_string(buf, &self.protocol_type)?;
}
if version >= 4 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.group_state);
} else {
wire::put_string(buf, &self.group_state)?;
}
}
if version >= 5 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.group_type);
} else {
wire::put_string(buf, &self.group_type)?;
}
}
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.group_id = 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)?
};
if version >= 4 {
this.group_state = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 5 {
this.group_type = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}