use std::io;
use bytes::BufMut;
use crate::{codec::*, err_encode_message_unsupported};
#[derive(Debug, Default)]
pub struct CreateTopicsResponse {
pub throttle_time_ms: i32,
pub topics: Vec<CreatableTopicResult>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Encodable for CreateTopicsResponse {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> io::Result<()> {
if version >= 2 {
Int32.encode(buf, self.throttle_time_ms)?;
}
NullableArray(Struct(version), version >= 5).encode(buf, self.topics.as_slice())?;
if version >= 5 {
RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct CreatableTopicResult {
pub name: String,
pub topic_id: uuid::Uuid,
pub error_code: i16,
pub error_message: Option<String>,
pub topic_config_error_code: i16,
pub num_partitions: i32,
pub replication_factor: i16,
pub configs: Vec<CreatableTopicConfigs>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Encodable for CreatableTopicResult {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> io::Result<()> {
NullableString(version >= 5).encode(buf, self.name.as_str())?;
if version >= 7 {
Uuid.encode(buf, self.topic_id)?;
}
Int16.encode(buf, self.error_code)?;
if version >= 1 {
NullableString(version >= 5).encode(buf, self.error_message.as_deref())?;
}
if version >= 5 {
Int32.encode(buf, self.num_partitions)?;
Int16.encode(buf, self.replication_factor)?;
NullableArray(Struct(version), true).encode(buf, self.configs.as_slice())?;
}
if version >= 5 {
let mut unknown_tagged_fields = vec![];
if self.topic_config_error_code != 0 {
unknown_tagged_fields.push(RawTaggedField {
tag: 0,
data: Int16.encode_alloc(self.topic_config_error_code)?,
})
}
unknown_tagged_fields.append(&mut self.unknown_tagged_fields.clone());
RawTaggedFieldList.encode(buf, &unknown_tagged_fields)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
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 Encodable for CreatableTopicConfigs {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> io::Result<()> {
if version < 5 {
Err(err_encode_message_unsupported(
version,
"CreatableTopicConfigs",
))?
}
NullableString(true).encode(buf, self.name.as_str())?;
NullableString(true).encode(buf, self.value.as_deref())?;
Bool.encode(buf, self.read_only)?;
Int8.encode(buf, self.config_source)?;
Bool.encode(buf, self.is_sensitive)?;
RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
Ok(())
}
}