#![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 CreatePartitionsAssignment {
pub broker_ids: Vec<super::BrokerId>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl CreatePartitionsAssignment {
pub fn with_broker_ids(mut self, value: Vec<super::BrokerId>) -> Self {
self.broker_ids = 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 = "client")]
impl Encodable for CreatePartitionsAssignment {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 2 {
types::CompactArray(types::Int32).encode(buf, &self.broker_ids)?;
} else {
types::Array(types::Int32).encode(buf, &self.broker_ids)?;
}
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::CompactArray(types::Int32).compute_size(&self.broker_ids)?;
} else {
total_size += types::Array(types::Int32).compute_size(&self.broker_ids)?;
}
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 = "broker")]
impl Decodable for CreatePartitionsAssignment {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let broker_ids = if version >= 2 {
types::CompactArray(types::Int32).decode(buf)?
} else {
types::Array(types::Int32).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 {
broker_ids,
unknown_tagged_fields,
})
}
}
impl Default for CreatePartitionsAssignment {
fn default() -> Self {
Self {
broker_ids: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for CreatePartitionsAssignment {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct CreatePartitionsRequest {
pub topics: Vec<CreatePartitionsTopic>,
pub timeout_ms: i32,
pub validate_only: bool,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl CreatePartitionsRequest {
pub fn with_topics(mut self, value: Vec<CreatePartitionsTopic>) -> Self {
self.topics = value;
self
}
pub fn with_timeout_ms(mut self, value: i32) -> Self {
self.timeout_ms = value;
self
}
pub fn with_validate_only(mut self, value: bool) -> Self {
self.validate_only = 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 = "client")]
impl Encodable for CreatePartitionsRequest {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version >= 2 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.topics)?;
}
types::Int32.encode(buf, &self.timeout_ms)?;
types::Boolean.encode(buf, &self.validate_only)?;
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::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
} else {
total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
}
total_size += types::Int32.compute_size(&self.timeout_ms)?;
total_size += types::Boolean.compute_size(&self.validate_only)?;
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 = "broker")]
impl Decodable for CreatePartitionsRequest {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let topics = if version >= 2 {
types::CompactArray(types::Struct { version }).decode(buf)?
} else {
types::Array(types::Struct { version }).decode(buf)?
};
let timeout_ms = types::Int32.decode(buf)?;
let validate_only = 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 {
topics,
timeout_ms,
validate_only,
unknown_tagged_fields,
})
}
}
impl Default for CreatePartitionsRequest {
fn default() -> Self {
Self {
topics: Default::default(),
timeout_ms: 0,
validate_only: false,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for CreatePartitionsRequest {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct CreatePartitionsTopic {
pub name: super::TopicName,
pub count: i32,
pub assignments: Option<Vec<CreatePartitionsAssignment>>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl CreatePartitionsTopic {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_count(mut self, value: i32) -> Self {
self.count = value;
self
}
pub fn with_assignments(mut self, value: Option<Vec<CreatePartitionsAssignment>>) -> Self {
self.assignments = 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 = "client")]
impl Encodable for CreatePartitionsTopic {
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)?;
}
types::Int32.encode(buf, &self.count)?;
if version >= 2 {
types::CompactArray(types::Struct { version }).encode(buf, &self.assignments)?;
} else {
types::Array(types::Struct { version }).encode(buf, &self.assignments)?;
}
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)?;
}
total_size += types::Int32.compute_size(&self.count)?;
if version >= 2 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.assignments)?;
} else {
total_size +=
types::Array(types::Struct { version }).compute_size(&self.assignments)?;
}
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 = "broker")]
impl Decodable for CreatePartitionsTopic {
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 count = types::Int32.decode(buf)?;
let assignments = 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,
count,
assignments,
unknown_tagged_fields,
})
}
}
impl Default for CreatePartitionsTopic {
fn default() -> Self {
Self {
name: Default::default(),
count: 0,
assignments: Some(Default::default()),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for CreatePartitionsTopic {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for CreatePartitionsRequest {
fn header_version(version: i16) -> i16 {
if version >= 2 {
2
} else {
1
}
}
}