use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i32, get_i64, put_i32, put_i64};
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::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 8;
pub const MIN_VERSION: i16 = 2;
pub const MAX_VERSION: i16 = 10;
pub const FLEXIBLE_MIN: i16 = 8;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OffsetCommitRequest {
pub group_id: String,
pub generation_id_or_member_epoch: i32,
pub member_id: String,
pub group_instance_id: Option<String>,
pub retention_time_ms: i64,
pub topics: Vec<OffsetCommitRequestTopic>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for OffsetCommitRequest {
fn default() -> Self {
Self {
group_id: String::new(),
generation_id_or_member_epoch: -1i32,
member_id: String::new(),
group_instance_id: None,
retention_time_ms: -1i64,
topics: Vec::new(),
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for OffsetCommitRequest {
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 { if flex { put_compact_string(buf, &self.group_id) } else { put_string(buf, &self.group_id) } }
if version >= 1 { put_i32(buf, self.generation_id_or_member_epoch) }
if version >= 1 { if flex { put_compact_string(buf, &self.member_id) } else { put_string(buf, &self.member_id) } }
if version >= 7 { if flex { put_compact_nullable_string(buf, self.group_instance_id.as_deref()) } else { put_nullable_string(buf, self.group_instance_id.as_deref()) } }
if version >= 2 && version <= 4 { put_i64(buf, self.retention_time_ms) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { 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 = is_flexible(version);
let mut n: usize = 0;
if version >= 0 { n += if flex { compact_string_len(&self.group_id) } else { string_len(&self.group_id) }; }
if version >= 1 { n += 4; }
if version >= 1 { n += if flex { compact_string_len(&self.member_id) } else { string_len(&self.member_id) }; }
if version >= 7 { n += if flex { compact_nullable_string_len(self.group_instance_id.as_deref()) } else { nullable_string_len(self.group_instance_id.as_deref()) }; }
if version >= 2 && version <= 4 { n += 8; }
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 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 OffsetCommitRequest {
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 { out.group_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 1 { out.generation_id_or_member_epoch = get_i32(buf)?; }
if version >= 1 { out.member_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 7 { out.group_instance_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
if version >= 2 && version <= 4 { out.retention_time_ms = get_i64(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(OffsetCommitRequestTopic::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, Default)]
pub struct OffsetCommitRequestTopic {
pub name: String,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<OffsetCommitRequestPartition>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for OffsetCommitRequestTopic {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 8;
if version >= 0 && version <= 9 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
if version >= 10 { 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 >= 8;
let mut n: usize = 0;
if version >= 0 && version <= 9 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
if version >= 10 { 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 OffsetCommitRequestTopic {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 8;
let mut out = Self::default();
if version >= 0 && version <= 9 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 10 { 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(OffsetCommitRequestPartition::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 OffsetCommitRequestPartition {
pub partition_index: i32,
pub committed_offset: i64,
pub committed_leader_epoch: i32,
pub committed_metadata: Option<String>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for OffsetCommitRequestPartition {
fn default() -> Self {
Self {
partition_index: 0i32,
committed_offset: 0i64,
committed_leader_epoch: -1i32,
committed_metadata: None,
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for OffsetCommitRequestPartition {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 8;
if version >= 0 { put_i32(buf, self.partition_index) }
if version >= 0 { put_i64(buf, self.committed_offset) }
if version >= 6 { put_i32(buf, self.committed_leader_epoch) }
if version >= 0 { if flex { put_compact_nullable_string(buf, self.committed_metadata.as_deref()) } else { put_nullable_string(buf, self.committed_metadata.as_deref()) } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 8;
let mut n: usize = 0;
if version >= 0 { n += 4; }
if version >= 0 { n += 8; }
if version >= 6 { n += 4; }
if version >= 0 { n += if flex { compact_nullable_string_len(self.committed_metadata.as_deref()) } else { nullable_string_len(self.committed_metadata.as_deref()) }; }
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 OffsetCommitRequestPartition {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 8;
let mut out = Self::default();
if version >= 0 { out.partition_index = get_i32(buf)?; }
if version >= 0 { out.committed_offset = get_i64(buf)?; }
if version >= 6 { out.committed_leader_epoch = get_i32(buf)?; }
if version >= 0 { out.committed_metadata = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
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();
obj.insert("groupId".to_string(), ::serde_json::Value::String(String::new()));
if version >= 1 {
obj.insert("generationIdOrMemberEpoch".to_string(), ::serde_json::json!(-1));
}
if version >= 1 {
obj.insert("memberId".to_string(), ::serde_json::Value::String(String::new()));
}
if version >= 7 {
obj.insert("groupInstanceId".to_string(), ::serde_json::Value::Null);
}
if version >= 2 && version <= 4 {
obj.insert("retentionTimeMs".to_string(), ::serde_json::json!(-1));
}
obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
::serde_json::Value::Object(obj)
}
impl crate::ProtocolRequest for OffsetCommitRequest {
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::offset_commit_response::OffsetCommitResponse;
}