use bytes::BufMut;
use crate::primitives::fixed::{get_i8, put_i8};
use crate::primitives::string_bytes::{
compact_string_len, put_compact_string, put_string, string_len,
};
use crate::primitives::string_bytes_borrowed::{get_compact_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 = 10;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 6;
pub const FLEXIBLE_MIN: i16 = 3;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FindCoordinatorRequest<'a> {
pub key: &'a str,
pub key_type: i8,
pub coordinator_keys: Vec<&'a str>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl FindCoordinatorRequest<'_> {
pub fn to_owned(&self) -> crate::owned::find_coordinator_request::FindCoordinatorRequest {
crate::owned::find_coordinator_request::FindCoordinatorRequest {
key: (self.key).to_string(),
key_type: (self.key_type),
coordinator_keys: (self.coordinator_keys)
.iter()
.map(std::string::ToString::to_string)
.collect(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for FindCoordinatorRequest<'_> {
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 (0..=3).contains(&version) {
if flex {
put_compact_string(buf, self.key);
} else {
put_string(buf, self.key);
}
}
if version >= 1 {
put_i8(buf, self.key_type);
}
if version >= 4 {
{
crate::primitives::array::put_array_len(buf, (self.coordinator_keys).len(), flex);
for it in &self.coordinator_keys {
if flex {
put_compact_string(buf, it);
} else {
put_string(buf, it);
}
}
}
}
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 (0..=3).contains(&version) {
n += if flex {
compact_string_len(self.key)
} else {
string_len(self.key)
};
}
if version >= 1 {
n += 1;
}
if version >= 4 {
n += {
let prefix = crate::primitives::array::array_len_prefix_len(
(self.coordinator_keys).len(),
flex,
);
let body: usize = (self.coordinator_keys)
.iter()
.map(|it| {
if flex {
compact_string_len(it)
} else {
string_len(it)
}
})
.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 FindCoordinatorRequest<'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 (0..=3).contains(&version) {
out.key = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 1 {
out.key_type = get_i8(buf)?;
}
if version >= 4 {
out.coordinator_keys = {
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_borrowed(buf)?
} else {
get_string_borrowed(buf)?
});
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl FindCoordinatorRequest<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if (0..=3).contains(&version) {
m.key = "x";
}
if version >= 1 {
m.key_type = 1i8;
}
if version >= 4 {
m.coordinator_keys = vec!["x"];
}
m
}
}