use bytes::{Buf, BufMut};
use crate::{
Decode, Encode, ProtocolError, UnknownTaggedFields,
primitives::{
fixed::{get_i32, put_i32},
string_bytes::{
compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
put_string, string_len,
},
},
};
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 {
pub group: String,
pub topic: String,
pub partition: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
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 Decode<'_> for OffsetCommitKey {
fn decode<B: Buf>(buf: &mut B, 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_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if version == 0 {
out.topic = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(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".to_string();
}
if version == 0 {
m.topic = "x".to_string();
}
if version == 0 {
m.partition = 1i32;
}
m
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
if version <= 0 {
obj.insert(
"group".to_string(),
::serde_json::Value::String(String::new()),
);
}
if version <= 0 {
obj.insert(
"topic".to_string(),
::serde_json::Value::String(String::new()),
);
}
if version <= 0 {
obj.insert("partition".to_string(), ::serde_json::json!(0));
}
::serde_json::Value::Object(obj)
}