use bytes::{BufMut, Bytes};
use crate::primitives::fixed::{get_bool, get_i16, get_i32, put_bool, put_i16, put_i32};
use crate::primitives::string_bytes::{
compact_nullable_string_len, compact_string_len, nullable_string_len,
put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
};
use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
use crate::primitives::string_bytes_borrowed::{
get_compact_nullable_string_borrowed, get_compact_string_borrowed,
get_nullable_string_borrowed, get_string_borrowed,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 11;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 9;
pub const FLEXIBLE_MIN: i16 = 6;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinGroupResponse<'a> {
pub throttle_time_ms: i32,
pub error_code: i16,
pub generation_id: i32,
pub protocol_type: Option<&'a str>,
pub protocol_name: Option<&'a str>,
pub leader: &'a str,
pub skip_assignment: bool,
pub member_id: &'a str,
pub members: Vec<JoinGroupResponseMember<'a>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for JoinGroupResponse<'_> {
fn default() -> Self {
Self {
throttle_time_ms: 0i32,
error_code: 0i16,
generation_id: -1i32,
protocol_type: None,
protocol_name: None,
leader: "",
skip_assignment: false,
member_id: "",
members: Vec::new(),
unknown_tagged_fields: Default::default(),
}
}
}
impl JoinGroupResponse<'_> {
pub fn to_owned(&self) -> crate::owned::join_group_response::JoinGroupResponse {
crate::owned::join_group_response::JoinGroupResponse {
throttle_time_ms: (self.throttle_time_ms),
error_code: (self.error_code),
generation_id: (self.generation_id),
protocol_type: (self.protocol_type).map(std::string::ToString::to_string),
protocol_name: (self.protocol_name).map(std::string::ToString::to_string),
leader: (self.leader).to_string(),
skip_assignment: (self.skip_assignment),
member_id: (self.member_id).to_string(),
members: (self.members)
.iter()
.map(JoinGroupResponseMember::to_owned)
.collect(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for JoinGroupResponse<'_> {
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 >= 2 {
put_i32(buf, self.throttle_time_ms);
}
if version >= 0 {
put_i16(buf, self.error_code);
}
if version >= 0 {
put_i32(buf, self.generation_id);
}
if version >= 7 {
if flex {
put_compact_nullable_string(buf, self.protocol_type);
} else {
put_nullable_string(buf, self.protocol_type);
}
}
if version >= 0 {
if version >= 7 {
if flex {
put_compact_nullable_string(buf, self.protocol_name);
} else {
put_nullable_string(buf, self.protocol_name);
}
} else {
if flex {
put_compact_string(buf, (self.protocol_name).unwrap_or(""));
} else {
put_string(buf, (self.protocol_name).unwrap_or(""));
}
}
}
if version >= 0 {
if flex {
put_compact_string(buf, self.leader);
} else {
put_string(buf, self.leader);
}
}
if version >= 9 {
put_bool(buf, self.skip_assignment);
}
if version >= 0 {
if flex {
put_compact_string(buf, self.member_id);
} else {
put_string(buf, self.member_id);
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.members).len(), flex);
for it in &self.members {
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 >= 2 {
n += 4;
}
if version >= 0 {
n += 2;
}
if version >= 0 {
n += 4;
}
if version >= 7 {
n += if flex {
compact_nullable_string_len(self.protocol_type)
} else {
nullable_string_len(self.protocol_type)
};
}
if version >= 0 {
n += if version >= 7 {
if flex {
compact_nullable_string_len(self.protocol_name)
} else {
nullable_string_len(self.protocol_name)
}
} else {
if flex {
compact_string_len((self.protocol_name).unwrap_or(""))
} else {
string_len((self.protocol_name).unwrap_or(""))
}
};
}
if version >= 0 {
n += if flex {
compact_string_len(self.leader)
} else {
string_len(self.leader)
};
}
if version >= 9 {
n += 1;
}
if version >= 0 {
n += if flex {
compact_string_len(self.member_id)
} else {
string_len(self.member_id)
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.members).len(), flex);
let body: usize = (self.members)
.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> DecodeBorrow<'de> for JoinGroupResponse<'de> {
fn decode_borrow(buf: &mut &'de [u8], 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 >= 2 {
out.throttle_time_ms = get_i32(buf)?;
}
if version >= 0 {
out.error_code = get_i16(buf)?;
}
if version >= 0 {
out.generation_id = get_i32(buf)?;
}
if version >= 7 {
out.protocol_type = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
if version >= 0 {
out.protocol_name = if version >= 7 {
if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
}
} else {
Some(if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
})
};
}
if version >= 0 {
out.leader = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 9 {
out.skip_assignment = get_bool(buf)?;
}
if version >= 0 {
out.member_id = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.members = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(JoinGroupResponseMember::decode_borrow(buf, version)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl JoinGroupResponse<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 2 {
m.throttle_time_ms = 1i32;
}
if version >= 0 {
m.error_code = 1i16;
}
if version >= 0 {
m.generation_id = 1i32;
}
if version >= 7 {
m.protocol_type = Some("x");
}
if version >= 0 {
m.protocol_name = Some("x");
}
if version >= 0 {
m.leader = "x";
}
if version >= 9 {
m.skip_assignment = true;
}
if version >= 0 {
m.member_id = "x";
}
if version >= 0 {
m.members = vec![JoinGroupResponseMember::populated(version)];
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct JoinGroupResponseMember<'a> {
pub member_id: &'a str,
pub group_instance_id: Option<&'a str>,
pub metadata: &'a [u8],
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl JoinGroupResponseMember<'_> {
pub fn to_owned(&self) -> crate::owned::join_group_response::JoinGroupResponseMember {
crate::owned::join_group_response::JoinGroupResponseMember {
member_id: (self.member_id).to_string(),
group_instance_id: (self.group_instance_id).map(std::string::ToString::to_string),
metadata: Bytes::copy_from_slice(self.metadata),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for JoinGroupResponseMember<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 6;
if version >= 0 {
if flex {
put_compact_string(buf, self.member_id);
} else {
put_string(buf, self.member_id);
}
}
if version >= 5 {
if flex {
put_compact_nullable_string(buf, self.group_instance_id);
} else {
put_nullable_string(buf, self.group_instance_id);
}
}
if version >= 0 {
if flex {
put_compact_bytes(buf, self.metadata);
} else {
put_bytes(buf, self.metadata);
}
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 6;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.member_id)
} else {
string_len(self.member_id)
};
}
if version >= 5 {
n += if flex {
compact_nullable_string_len(self.group_instance_id)
} else {
nullable_string_len(self.group_instance_id)
};
}
if version >= 0 {
n += if flex {
crate::primitives::varint::uvarint_len(
u32::try_from((self.metadata).len() + 1).unwrap(),
) + (self.metadata).len()
} else {
4 + (self.metadata).len()
};
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for JoinGroupResponseMember<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 6;
let mut out = Self::default();
if version >= 0 {
out.member_id = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 5 {
out.group_instance_id = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
if version >= 0 {
out.metadata = if flex {
get_compact_bytes_borrowed(buf)?
} else {
get_bytes_borrowed(buf)?
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl JoinGroupResponseMember<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.member_id = "x";
}
if version >= 5 {
m.group_instance_id = Some("x");
}
if version >= 0 {
m.metadata = &b"x"[..];
}
m
}
}