#![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 >= 12
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FetchRequest {
pub replica_id: i32,
pub max_wait_ms: i32,
pub min_bytes: i32,
pub max_bytes: i32,
pub isolation_level: i8,
pub session_id: i32,
pub session_epoch: i32,
pub topics: Vec<FetchTopic>,
pub forgotten_topics_data: Vec<ForgottenTopic>,
pub rack_id: String,
pub cluster_id: Option<Option<String>>,
pub replica_state: Option<ReplicaState>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for FetchRequest {
fn default() -> Self {
Self {
replica_id: -1,
max_wait_ms: 0,
min_bytes: 0,
max_bytes: 2147483647,
isolation_level: 0,
session_id: 0,
session_epoch: -1,
topics: Vec::new(),
forgotten_topics_data: Vec::new(),
rack_id: "".to_string(),
cluster_id: None,
replica_state: None,
unknown_tagged_fields: Vec::new(),
}
}
}
impl FetchRequest {
pub const API_KEY: i16 = 1;
pub const MIN_VERSION: i16 = 4;
pub const MAX_VERSION: i16 = 18;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version <= 14 {
buf.put_i32(self.replica_id);
}
buf.put_i32(self.max_wait_ms);
buf.put_i32(self.min_bytes);
buf.put_i32(self.max_bytes);
buf.put_i8(self.isolation_level);
if version >= 7 {
buf.put_i32(self.session_id);
}
if version >= 7 {
buf.put_i32(self.session_epoch);
}
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 version >= 7 {
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.forgotten_topics_data.len()));
} else {
wire::put_array_len(buf, Some(self.forgotten_topics_data.len()))?;
}
for item in &self.forgotten_topics_data {
item.encode(buf, version)?;
}
}
if version >= 11 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.rack_id);
} else {
wire::put_string(buf, &self.rack_id)?;
}
}
if is_flexible(version) {
let mut tagged = self.unknown_tagged_fields.clone();
if version >= 12 {
if let Some(v) = &self.cluster_id {
let mut tag_data = bytes::BytesMut::new();
{
let buf = &mut tag_data;
if is_flexible(version) {
wire::put_compact_nullable_string(buf, (*v).as_deref());
} else {
wire::put_nullable_string(buf, (*v).as_deref())?;
}
}
tagged.push(RawTaggedField {
tag: 0,
data: tag_data.freeze(),
});
}
}
if version >= 15 {
if let Some(v) = &self.replica_state {
let mut tag_data = bytes::BytesMut::new();
{
let buf = &mut tag_data;
(*v).encode(buf, version)?;
}
tagged.push(RawTaggedField {
tag: 1,
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();
if version <= 14 {
this.replica_id = wire::get_i32(buf)?;
}
this.max_wait_ms = wire::get_i32(buf)?;
this.min_bytes = wire::get_i32(buf)?;
this.max_bytes = wire::get_i32(buf)?;
this.isolation_level = wire::get_i8(buf)?;
if version >= 7 {
this.session_id = wire::get_i32(buf)?;
}
if version >= 7 {
this.session_epoch = 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 = FetchTopic::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
if version >= 7 {
this.forgotten_topics_data = {
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 = ForgottenTopic::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
}
if version >= 11 {
this.rack_id = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if is_flexible(version) {
for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
if raw.tag == 0 && (version >= 12) {
let mut tag_data = raw.data;
let buf = &mut tag_data;
this.cluster_id = Some(if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
});
if buf.has_remaining() {
return Err(DecodeError::InvalidLength(
i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
));
}
} else if raw.tag == 1 && (version >= 15) {
let mut tag_data = raw.data;
let buf = &mut tag_data;
this.replica_state =
Some(ReplicaState::decode_with_budget(buf, version, budget)?);
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)
}
}
impl crate::Message for FetchRequest {
const API_KEY: i16 = 1;
const MIN_VERSION: i16 = 4;
const MAX_VERSION: i16 = 18;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
FetchRequest::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
FetchRequest::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
FetchRequest::decode_with_limits(buf, version, limits)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ReplicaState {
pub replica_id: i32,
pub replica_epoch: i64,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ReplicaState {
fn default() -> Self {
Self {
replica_id: -1,
replica_epoch: -1,
unknown_tagged_fields: Vec::new(),
}
}
}
impl ReplicaState {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 15 {
buf.put_i32(self.replica_id);
}
if version >= 15 {
buf.put_i64(self.replica_epoch);
}
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 >= 15 {
this.replica_id = wire::get_i32(buf)?;
}
if version >= 15 {
this.replica_epoch = wire::get_i64(buf)?;
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FetchTopic {
pub topic: String,
pub topic_id: [u8; 16],
pub partitions: Vec<FetchPartition>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for FetchTopic {
fn default() -> Self {
Self {
topic: String::new(),
topic_id: [0u8; 16],
partitions: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl FetchTopic {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version <= 12 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.topic);
} else {
wire::put_string(buf, &self.topic)?;
}
}
if version >= 13 {
wire::put_uuid(buf, self.topic_id);
}
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.partitions.len()));
} else {
wire::put_array_len(buf, Some(self.partitions.len()))?;
}
for item in &self.partitions {
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();
if version <= 12 {
this.topic = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 13 {
this.topic_id = wire::get_uuid(buf)?;
}
this.partitions = {
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 = FetchPartition::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)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FetchPartition {
pub partition: i32,
pub current_leader_epoch: i32,
pub fetch_offset: i64,
pub last_fetched_epoch: i32,
pub log_start_offset: i64,
pub partition_max_bytes: i32,
pub replica_directory_id: Option<[u8; 16]>,
pub high_watermark: Option<i64>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for FetchPartition {
fn default() -> Self {
Self {
partition: 0,
current_leader_epoch: -1,
fetch_offset: 0,
last_fetched_epoch: -1,
log_start_offset: -1,
partition_max_bytes: 0,
replica_directory_id: None,
high_watermark: None,
unknown_tagged_fields: Vec::new(),
}
}
}
impl FetchPartition {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
buf.put_i32(self.partition);
if version >= 9 {
buf.put_i32(self.current_leader_epoch);
}
buf.put_i64(self.fetch_offset);
if version >= 12 {
buf.put_i32(self.last_fetched_epoch);
}
if version >= 5 {
buf.put_i64(self.log_start_offset);
}
buf.put_i32(self.partition_max_bytes);
if is_flexible(version) {
let mut tagged = self.unknown_tagged_fields.clone();
if version >= 17 {
if let Some(v) = &self.replica_directory_id {
let mut tag_data = bytes::BytesMut::new();
{
let buf = &mut tag_data;
wire::put_uuid(buf, (*v));
}
tagged.push(RawTaggedField {
tag: 0,
data: tag_data.freeze(),
});
}
}
if version >= 18 {
if let Some(v) = &self.high_watermark {
let mut tag_data = bytes::BytesMut::new();
{
let buf = &mut tag_data;
buf.put_i64((*v));
}
tagged.push(RawTaggedField {
tag: 1,
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.partition = wire::get_i32(buf)?;
if version >= 9 {
this.current_leader_epoch = wire::get_i32(buf)?;
}
this.fetch_offset = wire::get_i64(buf)?;
if version >= 12 {
this.last_fetched_epoch = wire::get_i32(buf)?;
}
if version >= 5 {
this.log_start_offset = wire::get_i64(buf)?;
}
this.partition_max_bytes = wire::get_i32(buf)?;
if is_flexible(version) {
for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
if raw.tag == 0 && (version >= 17) {
let mut tag_data = raw.data;
let buf = &mut tag_data;
this.replica_directory_id = Some(wire::get_uuid(buf)?);
if buf.has_remaining() {
return Err(DecodeError::InvalidLength(
i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
));
}
} else if raw.tag == 1 && (version >= 18) {
let mut tag_data = raw.data;
let buf = &mut tag_data;
this.high_watermark = Some(wire::get_i64(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 ForgottenTopic {
pub topic: String,
pub topic_id: [u8; 16],
pub partitions: Vec<i32>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ForgottenTopic {
fn default() -> Self {
Self {
topic: String::new(),
topic_id: [0u8; 16],
partitions: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl ForgottenTopic {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if (7..=12).contains(&version) {
if is_flexible(version) {
wire::put_compact_string(buf, &self.topic);
} else {
wire::put_string(buf, &self.topic)?;
}
}
if version >= 13 {
wire::put_uuid(buf, self.topic_id);
}
if version >= 7 {
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.partitions.len()));
} else {
wire::put_array_len(buf, Some(self.partitions.len()))?;
}
for item in &self.partitions {
buf.put_i32(*item);
}
}
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 (7..=12).contains(&version) {
this.topic = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 13 {
this.topic_id = wire::get_uuid(buf)?;
}
if version >= 7 {
this.partitions = {
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 {
budget.push(&mut items, wire::get_i32(buf)?)?;
}
items
}
}
};
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}