use std::io;
use bytes::Buf;
use crate::{codec::*, err_decode_message_null, err_decode_message_unsupported};
#[derive(Debug, Default)]
pub struct MetadataRequest {
pub topics: Vec<MetadataRequestTopic>,
pub allow_auto_topic_creation: bool,
pub include_cluster_authorized_operations: bool,
pub include_topic_authorized_operations: bool,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Decodable for MetadataRequest {
fn decode<B: Buf>(buf: &mut B, version: i16) -> io::Result<Self> {
let mut this = MetadataRequest::default();
if version >= 4 {
this.allow_auto_topic_creation = Bool.decode(buf)?;
} else {
this.allow_auto_topic_creation = true;
};
this.topics = NullableArray(Struct(version), version >= 9)
.decode(buf)?
.or_else(|| if version >= 1 { Some(vec![]) } else { None })
.ok_or_else(|| err_decode_message_null("topics"))?;
if (8..=10).contains(&version) {
this.include_cluster_authorized_operations = Bool.decode(buf)?;
}
if version >= 9 {
this.include_topic_authorized_operations = Bool.decode(buf)?;
}
if version >= 9 {
this.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
}
Ok(this)
}
}
#[derive(Debug, Default)]
pub struct MetadataRequestTopic {
pub topic_id: uuid::Uuid,
pub name: Option<String>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Decodable for MetadataRequestTopic {
fn decode<B: Buf>(buf: &mut B, version: i16) -> io::Result<Self> {
if version > 12 {
Err(err_decode_message_unsupported(
version,
"MetadataRequestTopic",
))?
}
let mut this = MetadataRequestTopic::default();
if version >= 10 {
this.topic_id = Uuid.decode(buf)?;
}
this.name = if version >= 10 {
NullableString(true).decode(buf)?
} else {
Some(
NullableString(version >= 9)
.decode(buf)?
.ok_or_else(|| err_decode_message_null("name"))?,
)
};
if version >= 9 {
this.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
}
Ok(this)
}
}