#![allow(
clippy::field_reassign_with_default,
clippy::nonminimal_bool,
clippy::derivable_impls,
clippy::borrow_deref_ref,
clippy::double_parens,
unused_parens
)]
use bytes::{Buf, BufMut};
use crate::budget::{Budget, Limits};
#[allow(unused_imports)]
use crate::error::{DecodeError, EncodeError};
use crate::wire::{self, RawTaggedField};
pub fn is_flexible(version: i16) -> bool {
let _ = version;
version >= 3
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FindCoordinatorResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub error_message: Option<String>,
pub node_id: i32,
pub host: String,
pub port: i32,
pub coordinators: Vec<Coordinator>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for FindCoordinatorResponse {
fn default() -> Self {
Self {
throttle_time_ms: 0,
error_code: 0,
error_message: Some(String::new()),
node_id: 0,
host: String::new(),
port: 0,
coordinators: Vec::new(),
unknown_tagged_fields: Vec::new(),
}
}
}
impl FindCoordinatorResponse {
pub const API_KEY: i16 = 10;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 6;
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 1 {
buf.put_i32(self.throttle_time_ms);
}
if version <= 3 {
buf.put_i16(self.error_code);
}
if (1..=3).contains(&version) {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.error_message.as_deref());
} else {
wire::put_nullable_string(buf, self.error_message.as_deref())?;
}
}
if version <= 3 {
buf.put_i32(self.node_id);
}
if version <= 3 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.host);
} else {
wire::put_string(buf, &self.host)?;
}
}
if version <= 3 {
buf.put_i32(self.port);
}
if version >= 4 {
if is_flexible(version) {
wire::put_compact_array_len(buf, Some(self.coordinators.len()));
} else {
wire::put_array_len(buf, Some(self.coordinators.len()))?;
}
for item in &self.coordinators {
item.encode(buf, version)?;
}
}
if is_flexible(version) {
wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
}
Ok(())
}
pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
Self::decode_with_limits(buf, version, Limits::default())
}
pub fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
let mut budget = limits.budget(buf.remaining());
Self::decode_with_budget(buf, version, &mut budget)
}
pub fn decode_with_budget(
buf: &mut impl Buf,
version: i16,
budget: &mut Budget,
) -> Result<Self, DecodeError> {
let mut this = Self::default();
if version >= 1 {
this.throttle_time_ms = wire::get_i32(buf)?;
}
if version <= 3 {
this.error_code = wire::get_i16(buf)?;
}
if (1..=3).contains(&version) {
this.error_message = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
if version <= 3 {
this.node_id = wire::get_i32(buf)?;
}
if version <= 3 {
this.host = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version <= 3 {
this.port = wire::get_i32(buf)?;
}
if version >= 4 {
this.coordinators = {
let len = if is_flexible(version) {
wire::get_compact_array_len(buf)?
} else {
wire::get_array_len(buf)?
};
match len {
None => return Err(DecodeError::InvalidLength(-1)),
Some(n) => {
let mut items = Vec::new();
for _ in 0..n {
let mark = buf.remaining();
let item = Coordinator::decode_with_budget(buf, version, budget)?;
budget.progress(mark, buf.remaining())?;
budget.push(&mut items, item)?;
}
items
}
}
};
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}
impl crate::Message for FindCoordinatorResponse {
const API_KEY: i16 = 10;
const MIN_VERSION: i16 = 0;
const MAX_VERSION: i16 = 6;
fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
FindCoordinatorResponse::encode(self, buf, version)
}
fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
FindCoordinatorResponse::decode(buf, version)
}
fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
FindCoordinatorResponse::decode_with_limits(buf, version, limits)
}
fn throttle_time_ms(&self) -> Option<i32> {
Some(self.throttle_time_ms)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Coordinator {
pub key: String,
pub node_id: i32,
pub host: String,
pub port: i32,
pub error_code: i16,
pub error_message: Option<String>,
pub unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for Coordinator {
fn default() -> Self {
Self {
key: String::new(),
node_id: 0,
host: String::new(),
port: 0,
error_code: 0,
error_message: Some(String::new()),
unknown_tagged_fields: Vec::new(),
}
}
}
impl Coordinator {
pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
if version >= 4 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.key);
} else {
wire::put_string(buf, &self.key)?;
}
}
if version >= 4 {
buf.put_i32(self.node_id);
}
if version >= 4 {
if is_flexible(version) {
wire::put_compact_string(buf, &self.host);
} else {
wire::put_string(buf, &self.host)?;
}
}
if version >= 4 {
buf.put_i32(self.port);
}
if version >= 4 {
buf.put_i16(self.error_code);
}
if version >= 4 {
if is_flexible(version) {
wire::put_compact_nullable_string(buf, self.error_message.as_deref());
} else {
wire::put_nullable_string(buf, self.error_message.as_deref())?;
}
}
if is_flexible(version) {
wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
}
Ok(())
}
pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
Self::decode_with_limits(buf, version, Limits::default())
}
pub fn decode_with_limits(
buf: &mut impl Buf,
version: i16,
limits: Limits,
) -> Result<Self, DecodeError> {
let mut budget = limits.budget(buf.remaining());
Self::decode_with_budget(buf, version, &mut budget)
}
pub fn decode_with_budget(
buf: &mut impl Buf,
version: i16,
budget: &mut Budget,
) -> Result<Self, DecodeError> {
let mut this = Self::default();
if version >= 4 {
this.key = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 4 {
this.node_id = wire::get_i32(buf)?;
}
if version >= 4 {
this.host = if is_flexible(version) {
wire::get_compact_string(buf)?
} else {
wire::get_string(buf)?
};
}
if version >= 4 {
this.port = wire::get_i32(buf)?;
}
if version >= 4 {
this.error_code = wire::get_i16(buf)?;
}
if version >= 4 {
this.error_message = if is_flexible(version) {
wire::get_compact_nullable_string(buf)?
} else {
wire::get_nullable_string(buf)?
};
}
if is_flexible(version) {
this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
}
Ok(this)
}
}