use byteorder::WriteBytesExt;
use crate::codec::*;
use crate::IoResult;
#[derive(Debug, Default, Clone)]
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 Encodable for FindCoordinatorResponse {
fn write<B: WriteBytesExt>(&self, buf: &mut B, version: i16) -> IoResult<()> {
if version >= 1 {
Int32.encode(buf, self.throttle_time_ms)?;
}
if version <= 3 {
Int16.encode(buf, self.error_code)?;
}
if (1..=3).contains(&version) {
NullableString(version >= 3).encode(buf, self.error_message.as_deref())?;
}
if version <= 3 {
Int32.encode(buf, self.node_id)?;
}
if version <= 3 {
NullableString(version >= 3).encode(buf, self.host.as_str())?;
}
if version <= 3 {
Int32.encode(buf, self.port)?;
}
if version >= 4 {
NullableArray(Struct(version), true).encode(buf, self.coordinators.as_slice())?;
}
if version >= 3 {
RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
}
Ok(())
}
fn calculate_size(&self, version: i16) -> usize {
let mut res = 0;
if version >= 1 {
res += Int32::SIZE; }
if version <= 3 {
res += Int16::SIZE; }
if (1..=3).contains(&version) {
res += NullableString(version >= 3).calculate_size(self.error_message.as_deref());
}
if version <= 3 {
res += Int32::SIZE; }
if version <= 3 {
res += NullableString(version >= 3).calculate_size(self.host.as_str());
}
if version <= 3 {
res += Int32::SIZE; }
if version >= 4 {
res +=
NullableArray(Struct(version), true).calculate_size(self.coordinators.as_slice());
}
if version >= 3 {
res += RawTaggedFieldList.calculate_size(&self.unknown_tagged_fields);
}
res
}
}
#[derive(Debug, Default, Clone)]
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 Encodable for Coordinator {
fn write<B: WriteBytesExt>(&self, buf: &mut B, version: i16) -> IoResult<()> {
if version > 4 {
Err(err_encode_message_unsupported(version, "Coordinator"))?
}
NullableString(true).encode(buf, self.key.as_str())?;
Int32.encode(buf, self.node_id)?;
NullableString(true).encode(buf, self.host.as_str())?;
Int32.encode(buf, self.port)?;
Int16.encode(buf, self.error_code)?;
NullableString(true).encode(buf, self.error_message.as_deref())?;
RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
Ok(())
}
fn calculate_size(&self, _version: i16) -> usize {
let mut res = 0;
res += NullableString(true).calculate_size(self.key.as_str());
res += Int32::SIZE; res += NullableString(true).calculate_size(self.host.as_str());
res += Int32::SIZE; res += Int16::SIZE; res += NullableString(true).calculate_size(self.error_message.as_deref());
res += RawTaggedFieldList.calculate_size(&self.unknown_tagged_fields);
res
}
}