#![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 FetchSnapshotResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub topics: Vec<TopicSnapshot>,
pub node_endpoints: Vec<NodeEndpoint>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl FetchSnapshotResponse {
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_topics(mut self, value: Vec<TopicSnapshot>) -> Self {
self.topics = value;
self
}
pub fn with_node_endpoints(mut self, value: Vec<NodeEndpoint>) -> Self {
self.node_endpoints = 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 FetchSnapshotResponse {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
types::Int32.encode(buf, &self.throttle_time_ms)?;
types::Int16.encode(buf, &self.error_code)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
let mut num_tagged_fields = self.unknown_tagged_fields.len();
if version >= 1 {
if !self.node_endpoints.is_empty() {
num_tagged_fields += 1;
}
}
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)?;
if version >= 1 {
if !self.node_endpoints.is_empty() {
let computed_size = types::CompactArray(types::Struct { version })
.compute_size(&self.node_endpoints)?;
if computed_size > std::u32::MAX as usize {
bail!(
"Tagged field is too large to encode ({} bytes)",
computed_size
);
}
types::UnsignedVarInt.encode(buf, 0)?;
types::UnsignedVarInt.encode(buf, computed_size as u32)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.node_endpoints)?;
}
}
write_unknown_tagged_fields(buf, 1.., &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)?;
total_size += types::Int16.compute_size(&self.error_code)?;
total_size += types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
let mut num_tagged_fields = self.unknown_tagged_fields.len();
if version >= 1 {
if !self.node_endpoints.is_empty() {
num_tagged_fields += 1;
}
}
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)?;
if version >= 1 {
if !self.node_endpoints.is_empty() {
let computed_size = types::CompactArray(types::Struct { version })
.compute_size(&self.node_endpoints)?;
if computed_size > std::u32::MAX as usize {
bail!(
"Tagged field is too large to encode ({} bytes)",
computed_size
);
}
total_size += types::UnsignedVarInt.compute_size(0)?;
total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
total_size += computed_size;
}
}
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for FetchSnapshotResponse {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let throttle_time_ms = types::Int32.decode(buf)?;
let error_code = types::Int16.decode(buf)?;
let topics = types::CompactArray(types::Struct { version }).decode(buf)?;
let mut node_endpoints = Default::default();
let mut unknown_tagged_fields = BTreeMap::new();
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)?;
match tag {
0 => {
if version >= 1 {
node_endpoints =
types::CompactArray(types::Struct { version }).decode(buf)?;
} else {
bail!("Tag {} is not valid for version {}", tag, version);
}
}
_ => {
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,
topics,
node_endpoints,
unknown_tagged_fields,
})
}
}
impl Default for FetchSnapshotResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
error_code: 0,
topics: Default::default(),
node_endpoints: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for FetchSnapshotResponse {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct LeaderIdAndEpoch {
pub leader_id: super::BrokerId,
pub leader_epoch: i32,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl LeaderIdAndEpoch {
pub fn with_leader_id(mut self, value: super::BrokerId) -> Self {
self.leader_id = value;
self
}
pub fn with_leader_epoch(mut self, value: i32) -> Self {
self.leader_epoch = 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 LeaderIdAndEpoch {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
types::Int32.encode(buf, &self.leader_id)?;
types::Int32.encode(buf, &self.leader_epoch)?;
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.leader_id)?;
total_size += types::Int32.compute_size(&self.leader_epoch)?;
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 LeaderIdAndEpoch {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let leader_id = types::Int32.decode(buf)?;
let leader_epoch = types::Int32.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 {
leader_id,
leader_epoch,
unknown_tagged_fields,
})
}
}
impl Default for LeaderIdAndEpoch {
fn default() -> Self {
Self {
leader_id: (0).into(),
leader_epoch: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for LeaderIdAndEpoch {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct NodeEndpoint {
pub node_id: super::BrokerId,
pub host: StrBytes,
pub port: u16,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl NodeEndpoint {
pub fn with_node_id(mut self, value: super::BrokerId) -> Self {
self.node_id = value;
self
}
pub fn with_host(mut self, value: StrBytes) -> Self {
self.host = value;
self
}
pub fn with_port(mut self, value: u16) -> Self {
self.port = 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 NodeEndpoint {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
if version >= 1 {
types::Int32.encode(buf, &self.node_id)?;
} else {
if self.node_id != 0 {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
types::CompactString.encode(buf, &self.host)?;
} else {
if !self.host.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
types::UInt16.encode(buf, &self.port)?;
} else {
if self.port != 0 {
bail!("A field is set that is not available on the selected protocol version");
}
}
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 >= 1 {
total_size += types::Int32.compute_size(&self.node_id)?;
} else {
if self.node_id != 0 {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
total_size += types::CompactString.compute_size(&self.host)?;
} else {
if !self.host.is_empty() {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
total_size += types::UInt16.compute_size(&self.port)?;
} else {
if self.port != 0 {
bail!("A field is set that is not available on the selected protocol version");
}
}
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 NodeEndpoint {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let node_id = if version >= 1 {
types::Int32.decode(buf)?
} else {
(0).into()
};
let host = if version >= 1 {
types::CompactString.decode(buf)?
} else {
Default::default()
};
let port = if version >= 1 {
types::UInt16.decode(buf)?
} else {
0
};
let mut unknown_tagged_fields = BTreeMap::new();
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 {
node_id,
host,
port,
unknown_tagged_fields,
})
}
}
impl Default for NodeEndpoint {
fn default() -> Self {
Self {
node_id: (0).into(),
host: Default::default(),
port: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for NodeEndpoint {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct PartitionSnapshot {
pub index: i32,
pub error_code: i16,
pub snapshot_id: SnapshotId,
pub current_leader: LeaderIdAndEpoch,
pub size: i64,
pub position: i64,
pub unaligned_records: Bytes,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl PartitionSnapshot {
pub fn with_index(mut self, value: i32) -> Self {
self.index = value;
self
}
pub fn with_error_code(mut self, value: i16) -> Self {
self.error_code = value;
self
}
pub fn with_snapshot_id(mut self, value: SnapshotId) -> Self {
self.snapshot_id = value;
self
}
pub fn with_current_leader(mut self, value: LeaderIdAndEpoch) -> Self {
self.current_leader = value;
self
}
pub fn with_size(mut self, value: i64) -> Self {
self.size = value;
self
}
pub fn with_position(mut self, value: i64) -> Self {
self.position = value;
self
}
pub fn with_unaligned_records(mut self, value: Bytes) -> Self {
self.unaligned_records = 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 PartitionSnapshot {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
types::Int32.encode(buf, &self.index)?;
types::Int16.encode(buf, &self.error_code)?;
types::Struct { version }.encode(buf, &self.snapshot_id)?;
types::Int64.encode(buf, &self.size)?;
types::Int64.encode(buf, &self.position)?;
types::CompactBytes.encode(buf, &self.unaligned_records)?;
let mut num_tagged_fields = self.unknown_tagged_fields.len();
if &self.current_leader != &Default::default() {
num_tagged_fields += 1;
}
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)?;
if &self.current_leader != &Default::default() {
let computed_size = types::Struct { version }.compute_size(&self.current_leader)?;
if computed_size > std::u32::MAX as usize {
bail!(
"Tagged field is too large to encode ({} bytes)",
computed_size
);
}
types::UnsignedVarInt.encode(buf, 0)?;
types::UnsignedVarInt.encode(buf, computed_size as u32)?;
types::Struct { version }.encode(buf, &self.current_leader)?;
}
write_unknown_tagged_fields(buf, 1.., &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.index)?;
total_size += types::Int16.compute_size(&self.error_code)?;
total_size += types::Struct { version }.compute_size(&self.snapshot_id)?;
total_size += types::Int64.compute_size(&self.size)?;
total_size += types::Int64.compute_size(&self.position)?;
total_size += types::CompactBytes.compute_size(&self.unaligned_records)?;
let mut num_tagged_fields = self.unknown_tagged_fields.len();
if &self.current_leader != &Default::default() {
num_tagged_fields += 1;
}
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)?;
if &self.current_leader != &Default::default() {
let computed_size = types::Struct { version }.compute_size(&self.current_leader)?;
if computed_size > std::u32::MAX as usize {
bail!(
"Tagged field is too large to encode ({} bytes)",
computed_size
);
}
total_size += types::UnsignedVarInt.compute_size(0)?;
total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
total_size += computed_size;
}
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
Ok(total_size)
}
}
#[cfg(feature = "client")]
impl Decodable for PartitionSnapshot {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let index = types::Int32.decode(buf)?;
let error_code = types::Int16.decode(buf)?;
let snapshot_id = types::Struct { version }.decode(buf)?;
let mut current_leader = Default::default();
let size = types::Int64.decode(buf)?;
let position = types::Int64.decode(buf)?;
let unaligned_records = types::CompactBytes.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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)?;
match tag {
0 => {
current_leader = types::Struct { version }.decode(buf)?;
}
_ => {
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
}
}
Ok(Self {
index,
error_code,
snapshot_id,
current_leader,
size,
position,
unaligned_records,
unknown_tagged_fields,
})
}
}
impl Default for PartitionSnapshot {
fn default() -> Self {
Self {
index: 0,
error_code: 0,
snapshot_id: Default::default(),
current_leader: Default::default(),
size: 0,
position: 0,
unaligned_records: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for PartitionSnapshot {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct SnapshotId {
pub end_offset: i64,
pub epoch: i32,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl SnapshotId {
pub fn with_end_offset(mut self, value: i64) -> Self {
self.end_offset = value;
self
}
pub fn with_epoch(mut self, value: i32) -> Self {
self.epoch = 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 SnapshotId {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
types::Int64.encode(buf, &self.end_offset)?;
types::Int32.encode(buf, &self.epoch)?;
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::Int64.compute_size(&self.end_offset)?;
total_size += types::Int32.compute_size(&self.epoch)?;
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 SnapshotId {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let end_offset = types::Int64.decode(buf)?;
let epoch = types::Int32.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 {
end_offset,
epoch,
unknown_tagged_fields,
})
}
}
impl Default for SnapshotId {
fn default() -> Self {
Self {
end_offset: 0,
epoch: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for SnapshotId {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct TopicSnapshot {
pub name: super::TopicName,
pub partitions: Vec<PartitionSnapshot>,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl TopicSnapshot {
pub fn with_name(mut self, value: super::TopicName) -> Self {
self.name = value;
self
}
pub fn with_partitions(mut self, value: Vec<PartitionSnapshot>) -> 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 TopicSnapshot {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
types::CompactString.encode(buf, &self.name)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
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::CompactString.compute_size(&self.name)?;
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
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 TopicSnapshot {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
if version < 0 || version > 1 {
bail!("specified version not supported by this message type");
}
let name = types::CompactString.decode(buf)?;
let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
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 TopicSnapshot {
fn default() -> Self {
Self {
name: Default::default(),
partitions: Default::default(),
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for TopicSnapshot {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for FetchSnapshotResponse {
fn header_version(version: i16) -> i16 {
1
}
}