use bytes::BufMut;
use crate::primitives::fixed::{get_bool, get_i16, get_i32, put_bool, 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::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 3;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 13;
pub const FLEXIBLE_MIN: i16 = 9;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataResponse<'a> {
pub throttle_time_ms: i32,
pub brokers: Vec<MetadataResponseBroker<'a>>,
pub cluster_id: Option<&'a str>,
pub controller_id: i32,
pub topics: Vec<MetadataResponseTopic<'a>>,
pub cluster_authorized_operations: i32,
pub error_code: i16,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for MetadataResponse<'a> {
fn default() -> Self {
Self {
throttle_time_ms: 0i32,
brokers: Vec::new(),
cluster_id: None,
controller_id: -1i32,
topics: Vec::new(),
cluster_authorized_operations: -2_147_483_648i32,
error_code: 0i16,
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> MetadataResponse<'a> {
pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponse {
crate::owned::metadata_response::MetadataResponse {
throttle_time_ms: (self.throttle_time_ms),
brokers: (self.brokers).iter().map(|it| it.to_owned()).collect(),
cluster_id: (self.cluster_id).map(|s| s.to_string()),
controller_id: (self.controller_id),
topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
cluster_authorized_operations: (self.cluster_authorized_operations),
error_code: (self.error_code),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for MetadataResponse<'a> {
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 >= 3 { put_i32(buf, self.throttle_time_ms) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.brokers).len(), flex); for it in &self.brokers { it.encode(buf, version)?; } } }
if version >= 2 { if flex { put_compact_nullable_string(buf, self.cluster_id) } else { put_nullable_string(buf, self.cluster_id) } }
if version >= 1 { put_i32(buf, self.controller_id) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
if version >= 8 && version <= 10 { put_i32(buf, self.cluster_authorized_operations) }
if version >= 13 { put_i16(buf, self.error_code) }
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 >= 3 { n += 4; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.brokers).len(), flex); let body: usize = (self.brokers).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 2 { n += if flex { compact_nullable_string_len(self.cluster_id) } else { nullable_string_len(self.cluster_id) }; }
if version >= 1 { n += 4; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 8 && version <= 10 { n += 4; }
if version >= 13 { n += 2; }
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 MetadataResponse<'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 >= 3 { out.throttle_time_ms = get_i32(buf)?; }
if version >= 0 { out.brokers = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(MetadataResponseBroker::decode_borrow(buf, version)?); } v }; }
if version >= 2 { out.cluster_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
if version >= 1 { out.controller_id = get_i32(buf)?; }
if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(MetadataResponseTopic::decode_borrow(buf, version)?); } v }; }
if version >= 8 && version <= 10 { out.cluster_authorized_operations = get_i32(buf)?; }
if version >= 13 { out.error_code = get_i16(buf)?; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataResponseBroker<'a> {
pub node_id: i32,
pub host: &'a str,
pub port: i32,
pub rack: Option<&'a str>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for MetadataResponseBroker<'a> {
fn default() -> Self {
Self {
node_id: 0i32,
host: "",
port: 0i32,
rack: None,
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> MetadataResponseBroker<'a> {
pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponseBroker {
crate::owned::metadata_response::MetadataResponseBroker {
node_id: (self.node_id),
host: (self.host).to_string(),
port: (self.port),
rack: (self.rack).map(|s| s.to_string()),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for MetadataResponseBroker<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 9;
if version >= 0 { put_i32(buf, self.node_id) }
if version >= 0 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
if version >= 0 { put_i32(buf, self.port) }
if version >= 1 { if flex { put_compact_nullable_string(buf, self.rack) } else { put_nullable_string(buf, self.rack) } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 9;
let mut n: usize = 0;
if version >= 0 { n += 4; }
if version >= 0 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
if version >= 0 { n += 4; }
if version >= 1 { n += if flex { compact_nullable_string_len(self.rack) } else { nullable_string_len(self.rack) }; }
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 MetadataResponseBroker<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 9;
let mut out = Self::default();
if version >= 0 { out.node_id = get_i32(buf)?; }
if version >= 0 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 0 { out.port = get_i32(buf)?; }
if version >= 1 { out.rack = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataResponseTopic<'a> {
pub error_code: i16,
pub name: Option<&'a str>,
pub topic_id: crate::primitives::uuid::Uuid,
pub is_internal: bool,
pub partitions: Vec<MetadataResponsePartition>,
pub topic_authorized_operations: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for MetadataResponseTopic<'a> {
fn default() -> Self {
Self {
error_code: 0i16,
name: None,
topic_id: Default::default(),
is_internal: false,
partitions: Vec::new(),
topic_authorized_operations: -2_147_483_648i32,
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> MetadataResponseTopic<'a> {
pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponseTopic {
crate::owned::metadata_response::MetadataResponseTopic {
error_code: (self.error_code),
name: (self.name).map(|s| s.to_string()),
topic_id: (self.topic_id),
is_internal: (self.is_internal),
partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
topic_authorized_operations: (self.topic_authorized_operations),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for MetadataResponseTopic<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 9;
if version >= 0 { put_i16(buf, self.error_code) }
if version >= 0 { if version >= 12 { if flex { put_compact_nullable_string(buf, self.name) } else { put_nullable_string(buf, self.name) } } else { if flex { put_compact_string(buf, (self.name).unwrap_or("")) } else { put_string(buf, (self.name).unwrap_or("")) } } }
if version >= 10 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
if version >= 1 { put_bool(buf, self.is_internal) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
if version >= 8 { put_i32(buf, self.topic_authorized_operations) }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 9;
let mut n: usize = 0;
if version >= 0 { n += 2; }
if version >= 0 { n += if version >= 12 { if flex { compact_nullable_string_len(self.name) } else { nullable_string_len(self.name) } } else { if flex { compact_string_len((self.name).unwrap_or("")) } else { string_len((self.name).unwrap_or("")) } }; }
if version >= 10 { n += 16; }
if version >= 1 { n += 1; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 8 { n += 4; }
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 MetadataResponseTopic<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 9;
let mut out = Self::default();
if version >= 0 { out.error_code = get_i16(buf)?; }
if version >= 0 { out.name = if version >= 12 { if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? } } else { Some(if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }) }; }
if version >= 10 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
if version >= 1 { out.is_internal = get_bool(buf)?; }
if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(MetadataResponsePartition::decode_borrow(buf, version)?); } v }; }
if version >= 8 { out.topic_authorized_operations = get_i32(buf)?; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
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: UnknownTaggedFields,
}
impl Default for MetadataResponsePartition {
fn default() -> Self {
Self {
error_code: 0i16,
partition_index: 0i32,
leader_id: 0i32,
leader_epoch: -1i32,
replica_nodes: Vec::new(),
isr_nodes: Vec::new(),
offline_replicas: Vec::new(),
unknown_tagged_fields: Default::default(),
}
}
}
impl MetadataResponsePartition {
pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponsePartition {
crate::owned::metadata_response::MetadataResponsePartition {
error_code: (self.error_code),
partition_index: (self.partition_index),
leader_id: (self.leader_id),
leader_epoch: (self.leader_epoch),
replica_nodes: (self.replica_nodes).clone(),
isr_nodes: (self.isr_nodes).clone(),
offline_replicas: (self.offline_replicas).clone(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for MetadataResponsePartition {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 9;
if version >= 0 { put_i16(buf, self.error_code) }
if version >= 0 { put_i32(buf, self.partition_index) }
if version >= 0 { put_i32(buf, self.leader_id) }
if version >= 7 { put_i32(buf, self.leader_epoch) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.replica_nodes).len(), flex); for it in &self.replica_nodes { put_i32(buf, *it); } } }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.isr_nodes).len(), flex); for it in &self.isr_nodes { put_i32(buf, *it); } } }
if version >= 5 { { crate::primitives::array::put_array_len(buf, (self.offline_replicas).len(), flex); for it in &self.offline_replicas { put_i32(buf, *it); } } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 9;
let mut n: usize = 0;
if version >= 0 { n += 2; }
if version >= 0 { n += 4; }
if version >= 0 { n += 4; }
if version >= 7 { n += 4; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.replica_nodes).len(), flex); let body: usize = (self.replica_nodes).iter().map(|_| 4).sum(); prefix + body }; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.isr_nodes).len(), flex); let body: usize = (self.isr_nodes).iter().map(|_| 4).sum(); prefix + body }; }
if version >= 5 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.offline_replicas).len(), flex); let body: usize = (self.offline_replicas).iter().map(|_| 4).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 MetadataResponsePartition {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 9;
let mut out = Self::default();
if version >= 0 { out.error_code = get_i16(buf)?; }
if version >= 0 { out.partition_index = get_i32(buf)?; }
if version >= 0 { out.leader_id = get_i32(buf)?; }
if version >= 7 { out.leader_epoch = get_i32(buf)?; }
if version >= 0 { out.replica_nodes = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
if version >= 0 { out.isr_nodes = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
if version >= 5 { out.offline_replicas = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}