use bytes::{BufMut, Bytes};
use crate::{
DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
primitives::{
fixed::{get_i32, put_i32},
string_bytes::{
compact_nullable_string_len, compact_string_len, nullable_string_len, put_bytes,
put_compact_bytes, put_compact_nullable_string, put_compact_string,
put_nullable_string, put_string, string_len,
},
string_bytes_borrowed::{
get_bytes_borrowed, get_compact_bytes_borrowed, get_compact_nullable_string_borrowed,
get_compact_string_borrowed, get_nullable_string_borrowed, get_string_borrowed,
},
},
tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
};
pub const API_KEY: i16 = 11;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 9;
pub const LATEST_STABLE_VERSION: i16 = 9;
pub const FLEXIBLE_MIN: i16 = 6;
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinGroupRequest<'a> {
pub group_id: &'a str,
pub session_timeout_ms: i32,
pub rebalance_timeout_ms: i32,
pub member_id: &'a str,
pub group_instance_id: Option<&'a str>,
pub protocol_type: &'a str,
pub protocols: Vec<JoinGroupRequestProtocol<'a>>,
pub reason: Option<&'a str>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for JoinGroupRequest<'_> {
fn default() -> Self {
Self {
group_id: "",
session_timeout_ms: 0i32,
rebalance_timeout_ms: -1i32,
member_id: "",
group_instance_id: None,
protocol_type: "",
protocols: Vec::new(),
reason: None,
unknown_tagged_fields: UnknownTaggedFields::default(),
}
}
}
impl<'a> JoinGroupRequest<'a> {
#[must_use]
pub fn to_owned(&self) -> crate::owned::join_group_request::JoinGroupRequest {
crate::owned::join_group_request::JoinGroupRequest {
group_id: (self.group_id).to_string(),
session_timeout_ms: (self.session_timeout_ms),
rebalance_timeout_ms: (self.rebalance_timeout_ms),
member_id: (self.member_id).to_string(),
group_instance_id: (self.group_instance_id).map(std::string::ToString::to_string),
protocol_type: (self.protocol_type).to_string(),
protocols: (self.protocols)
.iter()
.map(JoinGroupRequestProtocol::to_owned)
.collect(),
reason: (self.reason).map(std::string::ToString::to_string),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
if version >= 0 {
if flex {
let () = put_compact_string(buf, self.group_id);
} else {
let () = put_string(buf, self.group_id);
}
}
}
fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
if version >= 0 {
put_i32(buf, self.session_timeout_ms);
}
}
fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
if version >= 1 {
put_i32(buf, self.rebalance_timeout_ms);
}
}
fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
if version >= 0 {
if flex {
let () = put_compact_string(buf, self.member_id);
} else {
let () = put_string(buf, self.member_id);
}
}
}
fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
if version >= 5 {
if flex {
let () = put_compact_nullable_string(buf, self.group_instance_id);
} else {
let () = put_nullable_string(buf, self.group_instance_id);
}
}
}
fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
if version >= 0 {
if flex {
let () = put_compact_string(buf, self.protocol_type);
} else {
let () = put_string(buf, self.protocol_type);
}
}
}
fn encode_field_6<B: BufMut>(
&self,
buf: &mut B,
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.protocols).len(), flex);
for it in &self.protocols {
it.encode(buf, version)?;
}
};
}
Ok(())
}
fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
if version >= 8 {
if flex {
let () = put_compact_nullable_string(buf, self.reason);
} else {
let () = put_nullable_string(buf, self.reason);
}
}
}
fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, _version: i16, flex: bool) {
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
}
fn decode_field_0(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
out.group_id = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
Ok(())
}
fn decode_field_1(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
_flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
out.session_timeout_ms = get_i32(buf)?;
}
Ok(())
}
fn decode_field_2(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
_flex: bool,
) -> Result<(), ProtocolError> {
if version >= 1 {
out.rebalance_timeout_ms = get_i32(buf)?;
}
Ok(())
}
fn decode_field_3(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
out.member_id = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
Ok(())
}
fn decode_field_4(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 5 {
out.group_instance_id = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
Ok(())
}
fn decode_field_5(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
out.protocol_type = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
Ok(())
}
fn decode_field_6(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 0 {
out.protocols = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(JoinGroupRequestProtocol::decode_borrow(buf, version)?);
}
v
};
}
Ok(())
}
fn decode_field_7(
out: &mut Self,
buf: &mut &'a [u8],
version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if version >= 8 {
out.reason = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
Ok(())
}
fn decode_tagged_fields(
out: &mut Self,
buf: &mut &'a [u8],
_version: i16,
flex: bool,
) -> Result<(), ProtocolError> {
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(())
}
}
impl Encode for JoinGroupRequest<'_> {
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);
self.encode_field_0(buf, version, flex);
self.encode_field_1(buf, version, flex);
self.encode_field_2(buf, version, flex);
self.encode_field_3(buf, version, flex);
self.encode_field_4(buf, version, flex);
self.encode_field_5(buf, version, flex);
self.encode_field_6(buf, version, flex)?;
self.encode_field_7(buf, version, flex);
self.encode_tagged_fields(buf, version, flex);
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_id)
} else {
string_len(self.group_id)
};
}
if version >= 0 {
n += 4;
}
if version >= 1 {
n += 4;
}
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 {
compact_string_len(self.protocol_type)
} else {
string_len(self.protocol_type)
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.protocols).len(), flex);
let body: usize = (self.protocols)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if version >= 8 {
n += if flex {
compact_nullable_string_len(self.reason)
} else {
nullable_string_len(self.reason)
};
}
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 JoinGroupRequest<'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();
Self::decode_field_0(&mut out, buf, version, flex)?;
Self::decode_field_1(&mut out, buf, version, flex)?;
Self::decode_field_2(&mut out, buf, version, flex)?;
Self::decode_field_3(&mut out, buf, version, flex)?;
Self::decode_field_4(&mut out, buf, version, flex)?;
Self::decode_field_5(&mut out, buf, version, flex)?;
Self::decode_field_6(&mut out, buf, version, flex)?;
Self::decode_field_7(&mut out, buf, version, flex)?;
Self::decode_tagged_fields(&mut out, buf, version, flex)?;
Ok(out)
}
}
#[cfg(test)]
impl JoinGroupRequest<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.group_id = "x";
}
if version >= 0 {
m.session_timeout_ms = 1i32;
}
if version >= 1 {
m.rebalance_timeout_ms = 1i32;
}
if version >= 0 {
m.member_id = "x";
}
if version >= 5 {
m.group_instance_id = Some("x");
}
if version >= 0 {
m.protocol_type = "x";
}
if version >= 0 {
m.protocols = vec![JoinGroupRequestProtocol::populated(version)];
}
if version >= 8 {
m.reason = Some("x");
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct JoinGroupRequestProtocol<'a> {
pub name: &'a str,
pub metadata: &'a [u8],
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl JoinGroupRequestProtocol<'_> {
#[must_use]
pub fn to_owned(&self) -> crate::owned::join_group_request::JoinGroupRequestProtocol {
crate::owned::join_group_request::JoinGroupRequestProtocol {
name: (self.name).to_string(),
metadata: Bytes::copy_from_slice(self.metadata),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for JoinGroupRequestProtocol<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 6;
if version >= 0 {
if flex {
let () = put_compact_string(buf, self.name);
} else {
let () = put_string(buf, self.name);
}
}
if version >= 0 {
if flex {
let () = put_compact_bytes(buf, self.metadata);
} else {
let () = 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.name)
} else {
string_len(self.name)
};
}
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 JoinGroupRequestProtocol<'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.name = if flex {
get_compact_string_borrowed(buf)?
} else {
get_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 JoinGroupRequestProtocol<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x";
}
if version >= 0 {
m.metadata = &b"x"[..];
}
m
}
}