#![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 >= 5
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CreateTopicsResponse {
pub throttle_time_ms: i32,
pub topics: Vec<CreatableTopicResult>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for CreateTopicsResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
topics: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl CreateTopicsResponse {
pub const API_KEY: i16 = 19;
pub const MIN_VERSION: i16 = 2;
pub const MAX_VERSION: i16 = 7;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i32(self.throttle_time_ms);
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 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.throttle_time_ms = 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 = CreatableTopicResult::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
impl crate::Message for CreateTopicsResponse {
const API_KEY: i16 = 19;
const MIN_VERSION: i16 = 2;
const MAX_VERSION: i16 = 7;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
CreateTopicsResponse::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
CreateTopicsResponse::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
CreateTopicsResponse::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 CreatableTopicResult {
pub name: String,
pub topic_id: [u8; 16],
pub error_code: i16,
pub error_message: Option<String>,
pub num_partitions: i32,
pub replication_factor: i16,
pub configs: Option<Vec<CreatableTopicConfigs>>,
pub topic_config_error_code: Option<i16>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for CreatableTopicResult {
fn default() -> Self {
Self {
name: String::new(),
topic_id: [0u8; 16],
error_code: 0,
error_message: Some(String::new()),
num_partitions: -1,
replication_factor: -1,
configs: Some(Vec::new()),
topic_config_error_code: None,
unknown_tagged_fields: Vec::new(),
}
}
}
impl CreatableTopicResult {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if is_flexible(version) {
wire::put_compact_string(buf, &self.name);
} else {
wire::put_string(buf, &self.name)?;
}
if version >= 7 {
wire::put_uuid(buf, self.topic_id);
}
buf.put_i16(self.error_code);
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.error_message.as_deref());
} else {
wire::put_nullable_string(buf, self.error_message.as_deref())?;
}
if version >= 5 {
buf.put_i32(self.num_partitions);
}
if version >= 5 {
buf.put_i16(self.replication_factor);
}
if version >= 5 {
match &self.configs {
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 is_flexible(version) {
wire::put_compact_array_len(buf, None);
} else {
wire::put_array_len(buf, None)?;
}
}
}
}
if is_flexible(version) {
let mut tagged = self.unknown_tagged_fields.clone();
if version >= 5 {
if let Some(v) = &self.topic_config_error_code {
let mut tag_data = bytes::BytesMut::new();
{
let buf = &mut tag_data;
buf.put_i16((*v));
}
tagged.push(RawTaggedField {
tag: 0,
data: tag_data.freeze(),
});
}
}
tagged.sort_by_key(|f| f.tag);
wire::put_tagged_fields(buf, &tagged);
}
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.name = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
if version >= 7 {
this.topic_id = wire::get_uuid(buf)?;
}
this.error_code = wire::get_i16(buf)?;
this.error_message = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
if version >= 5 {
this.num_partitions = wire::get_i32(buf)?;
}
if version >= 5 {
this.replication_factor = wire::get_i16(buf)?;
}
if version >= 5 {
this.configs = {
let len = if is_flexible(version) {
wire::get_compact_array_len(buf)?
} else {
wire::get_array_len(buf)?
};
match len {
None => None,
Some(n) => {
let mut items = Vec::new();
for _ in 0..n {
let mark = buf.remaining();
let item =
CreatableTopicConfigs::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
Some(items)
}
}
};
}
if is_flexible(version) {
for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
if raw.tag == 0 && (version >= 5) {
let mut tag_data = raw.data;
let buf = &mut tag_data;
this.topic_config_error_code = Some(wire::get_i16(buf)?);
if buf.has_remaining() {
return Err(DecodeError::InvalidLength(
i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
));
}
} else {
budget.push(&mut this.unknown_tagged_fields, raw)?;
}
}
}
Ok(this)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CreatableTopicConfigs {
pub name: String,
pub value: Option<String>,
pub read_only: bool,
pub config_source: i8,
pub is_sensitive: bool,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for CreatableTopicConfigs {
fn default() -> Self {
Self {
name: String::new(),
value: Some(String::new()),
read_only: false,
config_source: -1,
is_sensitive: false,
unknown_tagged_fields: Vec::new(),
}
}
}
impl CreatableTopicConfigs {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 5 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.name);
} else {
wire::put_string(buf, &self.name)?;
}
}
if version >= 5 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.value.as_deref());
} else {
wire::put_nullable_string(buf, self.value.as_deref())?;
}
}
if version >= 5 {
buf.put_u8(u8::from(self.read_only));
}
if version >= 5 {
buf.put_i8(self.config_source);
}
if version >= 5 {
buf.put_u8(u8::from(self.is_sensitive));
}
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 >= 5 {
this.name = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 5 {
this.value = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
if version >= 5 {
this.read_only = wire::get_bool(buf)?;
}
if version >= 5 {
this.config_source = wire::get_i8(buf)?;
}
if version >= 5 {
this.is_sensitive = wire::get_bool(buf)?;
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}