use bytes::BufMut;
use crate::{
DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
primitives::{
fixed::{get_i32, put_i32},
string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
},
};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 32767;
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
version == FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetCommitKey<'a> {
pub group: &'a str,
pub topic: &'a str,
pub partition: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl OffsetCommitKey<'_> {
#[must_use]
pub fn to_owned(&self) -> crate::owned::offset_commit_key::OffsetCommitKey {
crate::owned::offset_commit_key::OffsetCommitKey {
group: (self.group).to_string(),
topic: (self.topic).to_string(),
partition: (self.partition),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for OffsetCommitKey<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"OffsetCommitKey version out of range",
));
}
let flex = is_flexible(version);
if version == 0 {
if flex {
let () = put_compact_string(buf, self.group);
} else {
let () = put_string(buf, self.group);
}
}
if version == 0 {
if flex {
let () = put_compact_string(buf, self.topic);
} else {
let () = put_string(buf, self.topic);
}
}
if version == 0 {
put_i32(buf, self.partition);
}
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)
} else {
string_len(self.group)
};
}
if version == 0 {
n += if flex {
compact_string_len(self.topic)
} else {
string_len(self.topic)
};
}
if version == 0 {
n += 4;
}
n
}
}
impl<'de> DecodeBorrow<'de> for OffsetCommitKey<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"OffsetCommitKey version out of range",
));
}
let flex = is_flexible(version);
let mut out = Self::default();
if version == 0 {
out.group = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version == 0 {
out.topic = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version == 0 {
out.partition = get_i32(buf)?;
}
Ok(out)
}
}
#[cfg(test)]
impl OffsetCommitKey<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version == 0 {
m.group = "x";
}
if version == 0 {
m.topic = "x";
}
if version == 0 {
m.partition = 1i32;
}
m
}
}