#![allow(unused)]
use std::borrow::Borrow;
use std::collections::BTreeMap;
use anyhow::{bail, Result};
use bytes::Bytes;
use uuid::Uuid;
use crate::protocol::{
buf::{ByteBuf, ByteBufMut},
compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct DescribeLogDirsPartition {
pub partition_index: i32,
pub partition_size: i64,
pub offset_lag: i64,
pub is_future_key: bool,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl DescribeLogDirsPartition {
pub fn with_partition_index(mut self, value: i32) -> Self {
self.partition_index = value;
self
}
pub fn with_partition_size(mut self, value: i64) -> Self {
self.partition_size = value;
self
}
pub fn with_offset_lag(mut self, value: i64) -> Self {
self.offset_lag = value;
self
}
pub fn with_is_future_key(mut self, value: bool) -> Self {
self.is_future_key = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for DescribeLogDirsPartition {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.partition_index)?;
types::Int64.encode(buf, &self.partition_size)?;
types::Int64.encode(buf, &self.offset_lag)?;
types::Boolean.encode(buf, &self.is_future_key)?;
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.partition_index)?;
total_size += types::Int64.compute_size(&self.partition_size)?;
total_size += types::Int64.compute_size(&self.offset_lag)?;
total_size += types::Boolean.compute_size(&self.is_future_key)?;
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for DescribeLogDirsPartition {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let partition_index = types::Int32.decode(buf)?;
let partition_size = types::Int64.decode(buf)?;
let offset_lag = types::Int64.decode(buf)?;
let is_future_key = types::Boolean.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 2 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
partition_index,
partition_size,
offset_lag,
is_future_key,
unknown_tagged_fields,
})
}
}
impl Default for DescribeLogDirsPartition {
fn default() -> Self {
Self {
partition_index: 0,
partition_size: 0,
offset_lag: 0,
is_future_key: false,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for DescribeLogDirsPartition {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct DescribeLogDirsResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub results: Vec<DescribeLogDirsResult>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl DescribeLogDirsResponse {
pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
self.throttle_time_ms = value;
self
}
pub fn with_error_code(mut self, value: i16) -> Self {
self.error_code = value;
self
}
pub fn with_results(mut self, value: Vec<DescribeLogDirsResult>) -> Self {
self.results = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for DescribeLogDirsResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.throttle_time_ms)?;
if version >= 3 {
types::Int16.encode(buf, &self.error_code)?;
}
if version >= 2 {
types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.results)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
if version >= 3 {
total_size += types::Int16.compute_size(&self.error_code)?;
}
if version >= 2 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.results)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for DescribeLogDirsResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let throttle_time_ms = types::Int32.decode(buf)?;
let error_code = if version >= 3 {
types::Int16.decode(buf)?
} else {
0
};
let results = if version >= 2 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 2 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
throttle_time_ms,
error_code,
results,
unknown_tagged_fields,
})
}
}
impl Default for DescribeLogDirsResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
error_code: 0,
results: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for DescribeLogDirsResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct DescribeLogDirsResult {
pub error_code: i16,
pub log_dir: StrBytes,
pub topics: Vec<DescribeLogDirsTopic>,
pub total_bytes: i64,
pub usable_bytes: i64,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl DescribeLogDirsResult {
pub fn with_error_code(mut self, value: i16) -> Self {
self.error_code = value;
self
}
pub fn with_log_dir(mut self, value: StrBytes) -> Self {
self.log_dir = value;
self
}
pub fn with_topics(mut self, value: Vec<DescribeLogDirsTopic>) -> Self {
self.topics = value;
self
}
pub fn with_total_bytes(mut self, value: i64) -> Self {
self.total_bytes = value;
self
}
pub fn with_usable_bytes(mut self, value: i64) -> Self {
self.usable_bytes = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for DescribeLogDirsResult {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int16.encode(buf, &self.error_code)?;
if version >= 2 {
types::CompactString.encode(buf, &self.log_dir)?;
} else {
types::String.encode(buf, &self.log_dir)?;
}
if version >= 2 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.topics)?;
}
if version >= 4 {
types::Int64.encode(buf, &self.total_bytes)?;
}
if version >= 4 {
types::Int64.encode(buf, &self.usable_bytes)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::Int16.compute_size(&self.error_code)?;
if version >= 2 {
total_size += types::CompactString.compute_size(&self.log_dir)?;
} else {
total_size += types::String.compute_size(&self.log_dir)?;
}
if version >= 2 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
}
if version >= 4 {
total_size += types::Int64.compute_size(&self.total_bytes)?;
}
if version >= 4 {
total_size += types::Int64.compute_size(&self.usable_bytes)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for DescribeLogDirsResult {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let error_code = types::Int16.decode(buf)?;
let log_dir = if version >= 2 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let topics = if version >= 2 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let total_bytes = if version >= 4 {
types::Int64.decode(buf)?
} else {
-1
};
let usable_bytes = if version >= 4 {
types::Int64.decode(buf)?
} else {
-1
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 2 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
error_code,
log_dir,
topics,
total_bytes,
usable_bytes,
unknown_tagged_fields,
})
}
}
impl Default for DescribeLogDirsResult {
fn default() -> Self {
Self {
error_code: 0,
log_dir: Default::default(),
topics: Default::default(),
total_bytes: -1,
usable_bytes: -1,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for DescribeLogDirsResult {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct DescribeLogDirsTopic {
pub name: super::TopicName,
pub partitions: Vec<DescribeLogDirsPartition>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl DescribeLogDirsTopic {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_partitions(mut self, value: Vec<DescribeLogDirsPartition>) -> Self {
self.partitions = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "broker")]
impl Encodable for DescribeLogDirsTopic {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 2 {
types::CompactString.encode(buf, &self.name)?;
} else {
types::String.encode(buf, &self.name)?;
}
if version >= 2 {
types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
}
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
if version >= 2 {
total_size += types::CompactString.compute_size(&self.name)?;
} else {
total_size += types::String.compute_size(&self.name)?;
}
if version >= 2 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
}
if version >= 2 {
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
}
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for DescribeLogDirsTopic {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = if version >= 2 {
types::CompactString.decode(buf)?
} else {
types::String.decode(buf)?
};
let partitions = if version >= 2 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let mut unknown_tagged_fields = BTreeMap::new();
if version >= 2 {
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
Ok(Self {
name,
partitions,
unknown_tagged_fields,
})
}
}
impl Default for DescribeLogDirsTopic {
fn default() -> Self {
Self {
name: Default::default(),
partitions: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for DescribeLogDirsTopic {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for DescribeLogDirsResponse {
fn header_version(version: i16) -> i16 {
if version >= 2 {
1
} else {
0
}
}
}