use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
use crate::primitives::string_bytes::{
compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
string_len,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
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, Default)]
pub struct OffsetCommitResponse {
pub throttle_time_ms: i32,
pub topics: Vec<OffsetCommitResponseTopic>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for OffsetCommitResponse {
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 >= 3 {
put_i32(buf, self.throttle_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 >= 3 {
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 flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for OffsetCommitResponse {
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 >= 3 {
out.throttle_time_ms = 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(OffsetCommitResponseTopic::decode(buf, version)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl OffsetCommitResponse {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 3 {
m.throttle_time_ms = 1i32;
}
if version >= 0 {
m.topics = vec![OffsetCommitResponseTopic::populated(version)];
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetCommitResponseTopic {
pub name: String,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<OffsetCommitResponsePartition>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for OffsetCommitResponseTopic {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 8;
if (0..=9).contains(&version) {
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 (0..=9).contains(&version) {
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 Decode<'_> for OffsetCommitResponseTopic {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 8;
let mut out = Self::default();
if (0..=9).contains(&version) {
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(OffsetCommitResponsePartition::decode(buf, version)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl OffsetCommitResponseTopic {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if (0..=9).contains(&version) {
m.name = "x".to_string();
}
if version >= 10 {
m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
}
if version >= 0 {
m.partitions = vec![OffsetCommitResponsePartition::populated(version)];
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetCommitResponsePartition {
pub partition_index: i32,
pub error_code: i16,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for OffsetCommitResponsePartition {
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_i16(buf, self.error_code);
}
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 += 2;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for OffsetCommitResponsePartition {
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.error_code = get_i16(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl OffsetCommitResponsePartition {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.partition_index = 1i32;
}
if version >= 0 {
m.error_code = 1i16;
}
m
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
if version >= 3 {
obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
}
obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
::serde_json::Value::Object(obj)
}