use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i32, get_i64, get_i8, put_i32, put_i64, put_i8};
use crate::primitives::string_bytes::{
compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
string_len,
};
use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 1;
pub const MIN_VERSION: i16 = 4;
pub const MAX_VERSION: i16 = 18;
pub const FLEXIBLE_MIN: i16 = 12;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
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<String>,
pub replica_state: ReplicaState,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for FetchRequest {
fn default() -> Self {
Self {
replica_id: -1i32,
max_wait_ms: 0i32,
min_bytes: 0i32,
max_bytes: 2_147_483_647i32,
isolation_level: 0i8,
session_id: 0i32,
session_epoch: -1i32,
topics: Vec::new(),
forgotten_topics_data: Vec::new(),
rack_id: "".to_string(),
cluster_id: None,
replica_state: Default::default(),
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for FetchRequest {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
if version >= 0 && version <= 14 { put_i32(buf, self.replica_id) }
if version >= 0 { put_i32(buf, self.max_wait_ms) }
if version >= 0 { put_i32(buf, self.min_bytes) }
if version >= 3 { put_i32(buf, self.max_bytes) }
if version >= 4 { put_i8(buf, self.isolation_level) }
if version >= 7 { put_i32(buf, self.session_id) }
if version >= 7 { put_i32(buf, self.session_epoch) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
if version >= 7 { { crate::primitives::array::put_array_len(buf, (self.forgotten_topics_data).len(), flex); for it in &self.forgotten_topics_data { it.encode(buf, version)?; } } }
if version >= 11 { if flex { put_compact_string(buf, &self.rack_id) } else { put_string(buf, &self.rack_id) } }
if flex {
let mut tagged = WriteTaggedFields::new();
if !(self.cluster_id.is_none()) {
let payload = encode_to_bytes(if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }, |b| { if flex { put_compact_nullable_string(b, self.cluster_id.as_deref()) } else { put_nullable_string(b, self.cluster_id.as_deref()) }; Ok(()) });
tagged.add(0, payload);
}
if !(crate::codegen_helpers::is_default(&self.replica_state)) {
let payload = encode_to_bytes(self.replica_state.encoded_len(version), |b| { self.replica_state.encode(b, version)?; Ok(()) });
tagged.add(1, payload);
}
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
if version >= 0 && version <= 14 { n += 4; }
if version >= 0 { n += 4; }
if version >= 0 { n += 4; }
if version >= 3 { n += 4; }
if version >= 4 { n += 1; }
if version >= 7 { n += 4; }
if version >= 7 { n += 4; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.forgotten_topics_data).len(), flex); let body: usize = (self.forgotten_topics_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 11 { n += if flex { compact_string_len(&self.rack_id) } else { string_len(&self.rack_id) }; }
if flex {
let mut known_pairs: Vec<(u32, usize)> = Vec::new();
if !(self.cluster_id.is_none()) {
known_pairs.push((0, if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }));
}
if !(crate::codegen_helpers::is_default(&self.replica_state)) {
known_pairs.push((1, self.replica_state.encoded_len(version)));
}
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> Decode<'de> for FetchRequest {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 && version <= 14 { out.replica_id = get_i32(buf)?; }
if version >= 0 { out.max_wait_ms = get_i32(buf)?; }
if version >= 0 { out.min_bytes = get_i32(buf)?; }
if version >= 3 { out.max_bytes = get_i32(buf)?; }
if version >= 4 { out.isolation_level = get_i8(buf)?; }
if version >= 7 { out.session_id = get_i32(buf)?; }
if version >= 7 { out.session_epoch = get_i32(buf)?; }
if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(FetchTopic::decode(buf, version)?); } v }; }
if version >= 7 { out.forgotten_topics_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(ForgottenTopic::decode(buf, version)?); } v }; }
if version >= 11 { out.rack_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if flex {
let mut tag_cluster_id = None;
let mut tag_replica_state = None;
out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
match tag {
0 => { tag_cluster_id = Some({ let b: &mut &[u8] = payload; if flex { get_compact_nullable_string_owned(b)? } else { get_nullable_string_owned(b)? } }); Ok(true) }
1 => { tag_replica_state = Some({ let b: &mut &[u8] = payload; ReplicaState::decode(b, version)? }); Ok(true) }
_ => Ok(false),
}
})?;
if let Some(v) = tag_cluster_id { out.cluster_id = v; }
if let Some(v) = tag_replica_state { out.replica_state = v; }
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaState {
pub replica_id: i32,
pub replica_epoch: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ReplicaState {
fn default() -> Self {
Self {
replica_id: -1i32,
replica_epoch: -1i64,
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for ReplicaState {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 15 { put_i32(buf, self.replica_id) }
if version >= 15 { put_i64(buf, self.replica_epoch) }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 15 { n += 4; }
if version >= 15 { n += 8; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> Decode<'de> for ReplicaState {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 15 { out.replica_id = get_i32(buf)?; }
if version >= 15 { out.replica_epoch = get_i64(buf)?; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FetchTopic {
pub topic: String,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<FetchPartition>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for FetchTopic {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 0 && version <= 12 { if flex { put_compact_string(buf, &self.topic) } else { put_string(buf, &self.topic) } }
if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 0 && version <= 12 { n += if flex { compact_string_len(&self.topic) } else { string_len(&self.topic) }; }
if version >= 13 { n += 16; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> Decode<'de> for FetchTopic {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 0 && version <= 12 { out.topic = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(FetchPartition::decode(buf, version)?); } v }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
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: crate::primitives::uuid::Uuid,
pub high_watermark: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for FetchPartition {
fn default() -> Self {
Self {
partition: 0i32,
current_leader_epoch: -1i32,
fetch_offset: 0i64,
last_fetched_epoch: -1i32,
log_start_offset: -1i64,
partition_max_bytes: 0i32,
replica_directory_id: Default::default(),
high_watermark: 9_223_372_036_854_775_807i64,
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for FetchPartition {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 0 { put_i32(buf, self.partition) }
if version >= 9 { put_i32(buf, self.current_leader_epoch) }
if version >= 0 { put_i64(buf, self.fetch_offset) }
if version >= 12 { put_i32(buf, self.last_fetched_epoch) }
if version >= 5 { put_i64(buf, self.log_start_offset) }
if version >= 0 { put_i32(buf, self.partition_max_bytes) }
if flex {
let mut tagged = WriteTaggedFields::new();
if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
let payload = encode_to_bytes(16, |b| { crate::primitives::uuid::put_uuid(b, self.replica_directory_id); Ok(()) });
tagged.add(0, payload);
}
if !(self.high_watermark == 9_223_372_036_854_775_807i64) {
let payload = encode_to_bytes(8, |b| { put_i64(b, self.high_watermark); Ok(()) });
tagged.add(1, payload);
}
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 0 { n += 4; }
if version >= 9 { n += 4; }
if version >= 0 { n += 8; }
if version >= 12 { n += 4; }
if version >= 5 { n += 8; }
if version >= 0 { n += 4; }
if flex {
let mut known_pairs: Vec<(u32, usize)> = Vec::new();
if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
known_pairs.push((0, 16));
}
if !(self.high_watermark == 9_223_372_036_854_775_807i64) {
known_pairs.push((1, 8));
}
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> Decode<'de> for FetchPartition {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 0 { out.partition = get_i32(buf)?; }
if version >= 9 { out.current_leader_epoch = get_i32(buf)?; }
if version >= 0 { out.fetch_offset = get_i64(buf)?; }
if version >= 12 { out.last_fetched_epoch = get_i32(buf)?; }
if version >= 5 { out.log_start_offset = get_i64(buf)?; }
if version >= 0 { out.partition_max_bytes = get_i32(buf)?; }
if flex {
let mut tag_replica_directory_id = None;
let mut tag_high_watermark = None;
out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
match tag {
0 => { tag_replica_directory_id = Some({ let b: &mut &[u8] = payload; crate::primitives::uuid::get_uuid(b)? }); Ok(true) }
1 => { tag_high_watermark = Some({ let b: &mut &[u8] = payload; get_i64(b)? }); Ok(true) }
_ => Ok(false),
}
})?;
if let Some(v) = tag_replica_directory_id { out.replica_directory_id = v; }
if let Some(v) = tag_high_watermark { out.high_watermark = v; }
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ForgottenTopic {
pub topic: String,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<i32>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ForgottenTopic {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 7 && version <= 12 { if flex { put_compact_string(buf, &self.topic) } else { put_string(buf, &self.topic) } }
if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
if version >= 7 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 7 && version <= 12 { n += if flex { compact_string_len(&self.topic) } else { string_len(&self.topic) }; }
if version >= 13 { n += 16; }
if version >= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|_| 4).sum(); prefix + body }; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> Decode<'de> for ForgottenTopic {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 7 && version <= 12 { out.topic = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
if version >= 7 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
if version >= 12 {
obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
}
if version <= 14 {
obj.insert("replicaId".to_string(), ::serde_json::json!(-1));
}
if version >= 15 {
obj.insert("replicaState".to_string(), { let mut m = ::serde_json::Map::new(); m.insert("replicaId".to_string(), ::serde_json::json!(-1)); m.insert("replicaEpoch".to_string(), ::serde_json::json!(-1)); ::serde_json::Value::Object(m) });
}
obj.insert("maxWaitMs".to_string(), ::serde_json::json!(0));
obj.insert("minBytes".to_string(), ::serde_json::json!(0));
if version >= 3 {
obj.insert("maxBytes".to_string(), ::serde_json::json!(2147483647));
}
if version >= 4 {
obj.insert("isolationLevel".to_string(), ::serde_json::json!(0));
}
if version >= 7 {
obj.insert("sessionId".to_string(), ::serde_json::json!(0));
}
if version >= 7 {
obj.insert("sessionEpoch".to_string(), ::serde_json::json!(-1));
}
obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
if version >= 7 {
obj.insert("forgottenTopicsData".to_string(), ::serde_json::Value::Array(vec![]));
}
if version >= 11 {
obj.insert("rackId".to_string(), ::serde_json::Value::String("".to_string()));
}
::serde_json::Value::Object(obj)
}
impl crate::ProtocolRequest for FetchRequest {
const API_KEY: i16 = API_KEY;
const MIN_VERSION: i16 = MIN_VERSION;
const MAX_VERSION: i16 = MAX_VERSION;
const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
type Response = super::fetch_response::FetchResponse;
}