#![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 MetadataResponse {
pub throttle_time_ms: i32,
pub brokers: Vec<MetadataResponseBroker>,
pub cluster_id: Option<String>,
pub controller_id: i32,
pub topics: Vec<MetadataResponseTopic>,
pub cluster_authorized_operations: i32,
pub error_code: i16,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
brokers: Vec::new(),
cluster_id: None,
controller_id: -1,
topics: Vec::new(),
cluster_authorized_operations: -2147483648,
error_code: 0,
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataResponse {
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> {
if version >= 3 {
buf.put_i32(self.throttle_time_ms);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.brokers.len()));
} else {
wire::put_array_len(buf, Some(self.brokers.len()))?;
}
for item in &self.brokers {
item.encode(buf, version)?;
}
if version >= 2 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.cluster_id.as_deref());
} else {
wire::put_nullable_string(buf, self.cluster_id.as_deref())?;
}
}
if version >= 1 {
buf.put_i32(self.controller_id);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.topics.len()));
} else {
wire::put_array_len(buf, Some(self.topics.len()))?;
}
for item in &self.topics {
item.encode(buf, version)?;
}
if (8..=10).contains(&version) {
buf.put_i32(self.cluster_authorized_operations);
}
if version >= 13 {
buf.put_i16(self.error_code);
}
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 >= 3 {
this.throttle_time_ms = wire::get_i32(buf)?;
}
this.brokers = {
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 =
MetadataResponseBroker::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if version >= 2 {
this.cluster_id = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
if version >= 1 {
this.controller_id = wire::get_i32(buf)?;
}
this.topics = {
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 = MetadataResponseTopic::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if (8..=10).contains(&version) {
this.cluster_authorized_operations = wire::get_i32(buf)?;
}
if version >= 13 {
this.error_code = wire::get_i16(buf)?;
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
impl crate::Message for MetadataResponse {
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> {
MetadataResponse::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
MetadataResponse::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
MetadataResponse::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 MetadataResponseBroker {
pub node_id: i32,
pub host: String,
pub port: i32,
pub rack: Option<String>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataResponseBroker {
fn default() -> Self {
Self {
node_id: 0,
host: String::new(),
port: 0,
rack: None,
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataResponseBroker {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i32(self.node_id);
if is_flexible(version) {
wire::put_compact_string(buf, &self.host);
} else {
wire::put_string(buf, &self.host)?;
}
buf.put_i32(self.port);
if version >= 1 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.rack.as_deref());
} else {
wire::put_nullable_string(buf, self.rack.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();
this.node_id = wire::get_i32(buf)?;
this.host = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
this.port = wire::get_i32(buf)?;
if version >= 1 {
this.rack = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(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 MetadataResponseTopic {
pub error_code: i16,
pub name: Option<String>,
pub topic_id: [u8; 16],
pub is_internal: bool,
pub partitions: Vec<MetadataResponsePartition>,
pub topic_authorized_operations: i32,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataResponseTopic {
fn default() -> Self {
Self {
error_code: 0,
name: Some(String::new()),
topic_id: [0u8; 16],
is_internal: false,
partitions: Vec::new(),
topic_authorized_operations: -2147483648,
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataResponseTopic {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i16(self.error_code);
if self.name.is_none() && !(version >= 12) {
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 version >= 10 {
wire::put_uuid(buf, self.topic_id);
}
if version >= 1 {
buf.put_u8(u8::from(self.is_internal));
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.partitions.len()));
} else {
wire::put_array_len(buf, Some(self.partitions.len()))?;
}
for item in &self.partitions {
item.encode(buf, version)?;
}
if version >= 8 {
buf.put_i32(self.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.error_code = wire::get_i16(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 >= 12) {
return Err(DecodeError::InvalidLength(-1));
}
raw
};
if version >= 10 {
this.topic_id = wire::get_uuid(buf)?;
}
if version >= 1 {
this.is_internal = wire::get_bool(buf)?;
}
this.partitions = {
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 =
MetadataResponsePartition::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if version >= 8 {
this.topic_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 MetadataResponsePartition {
pub error_code: i16,
pub partition_index: i32,
pub leader_id: i32,
pub leader_epoch: i32,
pub replica_nodes: Vec<i32>,
pub isr_nodes: Vec<i32>,
pub offline_replicas: Vec<i32>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for MetadataResponsePartition {
fn default() -> Self {
Self {
error_code: 0,
partition_index: 0,
leader_id: 0,
leader_epoch: -1,
replica_nodes: Vec::new(),
isr_nodes: Vec::new(),
offline_replicas: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl MetadataResponsePartition {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i16(self.error_code);
buf.put_i32(self.partition_index);
buf.put_i32(self.leader_id);
if version >= 7 {
buf.put_i32(self.leader_epoch);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.replica_nodes.len()));
} else {
wire::put_array_len(buf, Some(self.replica_nodes.len()))?;
}
for item in &self.replica_nodes {
buf.put_i32(*item);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.isr_nodes.len()));
} else {
wire::put_array_len(buf, Some(self.isr_nodes.len()))?;
}
for item in &self.isr_nodes {
buf.put_i32(*item);
}
if version >= 5 {
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.offline_replicas.len()));
} else {
wire::put_array_len(buf, Some(self.offline_replicas.len()))?;
}
for item in &self.offline_replicas {
buf.put_i32(*item);
}
}
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)?;
this.partition_index = wire::get_i32(buf)?;
this.leader_id = wire::get_i32(buf)?;
if version >= 7 {
this.leader_epoch = wire::get_i32(buf)?;
}
this.replica_nodes = {
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 {
budget.push(&mut items, wire::get_i32(buf)?)?;
}
items
}
}
};
this.isr_nodes = {
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 {
budget.push(&mut items, wire::get_i32(buf)?)?;
}
items
}
}
};
if version >= 5 {
this.offline_replicas = {
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 {
budget.push(&mut items, wire::get_i32(buf)?)?;
}
items
}
}
};
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}