use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i32, put_i32};
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::primitives::string_bytes::{compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned, nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 3;
pub const FLEXIBLE_MIN: i16 = 32767;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerProtocolSubscription {
pub topics: Vec<String>,
pub user_data: Option<::bytes::Bytes>,
pub owned_partitions: Vec<TopicPartition>,
pub generation_id: i32,
pub rack_id: Option<String>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ConsumerProtocolSubscription {
fn default() -> Self {
Self {
topics: Vec::new(),
user_data: None,
owned_partitions: Vec::new(),
generation_id: -1i32,
rack_id: None,
unknown_tagged_fields: Default::default(),
}
}
}
impl Encode for ConsumerProtocolSubscription {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
}
let flex = is_flexible(version);
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { if flex { put_compact_string(buf, &*it) } else { put_string(buf, &*it) }; } } }
if version >= 0 { if flex { put_compact_nullable_bytes(buf, self.user_data.as_deref()) } else { put_nullable_bytes(buf, self.user_data.as_deref()) } }
if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.owned_partitions).len(), flex); for it in &self.owned_partitions { it.encode(buf, version)?; } } }
if version >= 2 { put_i32(buf, self.generation_id) }
if version >= 3 { if flex { put_compact_nullable_string(buf, self.rack_id.as_deref()) } else { put_nullable_string(buf, self.rack_id.as_deref()) } }
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
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| if flex { compact_string_len(&*it) } else { string_len(&*it) }).sum(); prefix + body }; }
if version >= 0 { n += if flex { compact_nullable_bytes_len(self.user_data.as_deref()) } else { nullable_bytes_len(self.user_data.as_deref()) }; }
if version >= 1 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.owned_partitions).len(), flex); let body: usize = (self.owned_partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 2 { n += 4; }
if version >= 3 { n += if flex { compact_nullable_string_len(self.rack_id.as_deref()) } else { nullable_string_len(self.rack_id.as_deref()) }; }
n
}
}
impl<'de> Decode<'de> for ConsumerProtocolSubscription {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
}
let flex = is_flexible(version);
let mut out = Self::default();
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(if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }); } v }; }
if version >= 0 { out.user_data = if flex { get_compact_nullable_bytes_owned(buf)? } else { get_nullable_bytes_owned(buf)? }; }
if version >= 1 { out.owned_partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicPartition::decode(buf, version)?); } v }; }
if version >= 2 { out.generation_id = get_i32(buf)?; }
if version >= 3 { out.rack_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicPartition {
pub topic: String,
pub partitions: Vec<i32>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for TopicPartition {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 32767;
if version >= 1 { if flex { put_compact_string(buf, &self.topic) } else { put_string(buf, &self.topic) } }
if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 32767;
let mut n: usize = 0;
if version >= 1 { n += if flex { compact_string_len(&self.topic) } else { string_len(&self.topic) }; }
if version >= 1 { 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 }; }
n
}
}
impl<'de> Decode<'de> for TopicPartition {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 32767;
let mut out = Self::default();
if version >= 1 { out.topic = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
if version >= 1 { 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 }; }
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("topics".to_string(), ::serde_json::Value::Array(vec![]));
obj.insert("userData".to_string(), ::serde_json::Value::Null);
if version >= 1 {
obj.insert("ownedPartitions".to_string(), ::serde_json::Value::Array(vec![]));
}
if version >= 2 {
obj.insert("generationId".to_string(), ::serde_json::json!(-1));
}
if version >= 3 {
obj.insert("rackId".to_string(), ::serde_json::Value::Null);
}
::serde_json::Value::Object(obj)
}