#![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 >= 9
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MetadataRequest {
pub topics: Option<Vec<MetadataRequestTopic>>,
pub allow_auto_topic_creation: bool,
pub include_cluster_authorized_operations: bool,
pub include_topic_authorized_operations: bool,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataRequest {
fn default() -> Self {
Self {
topics: Some(Vec::new()),
allow_auto_topic_creation: true,
include_cluster_authorized_operations: false,
include_topic_authorized_operations: false,
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataRequest {
pub const API_KEY: i16 = 3;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 13;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
match &self.topics {
Some(items) => {
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(items.len()));
} else {
wire::put_array_len(buf, Some(items.len()))?;
}
for item in items {
item.encode(buf, version)?;
}
}
None => {
if !(version >= 1) {
return Err(EncodeError::NullField("Topics"));
}
if is_flexible(version) {
wire::put_compact_array_len(buf, None);
} else {
wire::put_array_len(buf, None)?;
}
}
}
if version >= 4 {
buf.put_u8(u8::from(self.allow_auto_topic_creation));
}
if (8..=10).contains(&version) {
buf.put_u8(u8::from(self.include_cluster_authorized_operations));
}
if version >= 8 {
buf.put_u8(u8::from(self.include_topic_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.topics = {
let len = if is_flexible(version) {
wire::get_compact_array_len(buf)?
} else {
wire::get_array_len(buf)?
};
match len {
None => {
if !(version >= 1) {
return Err(DecodeError::InvalidLength(-1));
}
None
}
Some(n) => {
let mut items = Vec::new();
for _ in 0..n {
let mark = buf.remaining();
let item = MetadataRequestTopic::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
Some(items)
}
}
};
if version >= 4 {
this.allow_auto_topic_creation = wire::get_bool(buf)?;
}
if (8..=10).contains(&version) {
this.include_cluster_authorized_operations = wire::get_bool(buf)?;
}
if version >= 8 {
this.include_topic_authorized_operations = wire::get_bool(buf)?;
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
impl crate::Message for MetadataRequest {
const API_KEY: i16 = 3;
const MIN_VERSION: i16 = 0;
const MAX_VERSION: i16 = 13;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
MetadataRequest::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
MetadataRequest::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
MetadataRequest::decode_with_limits(buf, version, limits)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MetadataRequestTopic {
pub topic_id: [u8; 16],
pub name: Option<String>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataRequestTopic {
fn default() -> Self {
Self {
topic_id: [0u8; 16],
name: Some(String::new()),
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataRequestTopic {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 10 {
wire::put_uuid(buf, self.topic_id);
}
if self.name.is_none() && !(version >= 10) {
return Err(EncodeError::NullField("Name"));
}
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.name.as_deref());
} else {
wire::put_nullable_string(buf, self.name.as_deref())?;
}
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 >= 10 {
this.topic_id = wire::get_uuid(buf)?;
}
this.name = {
let raw = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
if raw.is_none() && !(version >= 10) {
return Err(DecodeError::InvalidLength(-1));
}
raw
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}