use bytes::BufMut;
use crate::primitives::fixed::{get_f64, get_i16, get_i32, put_f64, 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_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 = 48;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 1;
pub const FLEXIBLE_MIN: i16 = 1;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct DescribeClientQuotasResponse<'a> {
pub throttle_time_ms: i32,
pub error_code: i16,
pub error_message: Option<&'a str>,
pub entries: Option<Vec<EntryData<'a>>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl DescribeClientQuotasResponse<'_> {
pub fn to_owned(
&self,
) -> crate::owned::describe_client_quotas_response::DescribeClientQuotasResponse {
crate::owned::describe_client_quotas_response::DescribeClientQuotasResponse {
throttle_time_ms: (self.throttle_time_ms),
error_code: (self.error_code),
error_message: (self.error_message).map(std::string::ToString::to_string),
entries: (self.entries)
.as_ref()
.map(|v| v.iter().map(EntryData::to_owned).collect()),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for DescribeClientQuotasResponse<'_> {
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 >= 0 {
put_i32(buf, self.throttle_time_ms);
}
if version >= 0 {
put_i16(buf, self.error_code);
}
if version >= 0 {
if flex {
put_compact_nullable_string(buf, self.error_message);
} else {
put_nullable_string(buf, self.error_message);
}
}
if version >= 0 {
{
let len = (self.entries).as_ref().map(Vec::len);
crate::primitives::array::put_nullable_array_len(buf, len, flex);
if let Some(v) = &self.entries {
for it in v {
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 >= 0 {
n += 4;
}
if version >= 0 {
n += 2;
}
if version >= 0 {
n += if flex {
compact_nullable_string_len(self.error_message)
} else {
nullable_string_len(self.error_message)
};
}
if version >= 0 {
n += {
let opt: Option<&Vec<_>> = (self.entries).as_ref();
let prefix = crate::primitives::array::nullable_array_len_prefix_len(
opt.map(std::vec::Vec::len),
flex,
);
let body: usize =
opt.map_or(0, |v| v.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 DescribeClientQuotasResponse<'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 >= 0 {
out.throttle_time_ms = get_i32(buf)?;
}
if version >= 0 {
out.error_code = get_i16(buf)?;
}
if version >= 0 {
out.error_message = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
if version >= 0 {
out.entries = {
let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
match opt {
None => None,
Some(n) => {
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(EntryData::decode_borrow(buf, version)?);
}
Some(v)
}
}
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl DescribeClientQuotasResponse<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.throttle_time_ms = 1i32;
}
if version >= 0 {
m.error_code = 1i16;
}
if version >= 0 {
m.error_message = Some("x");
}
if version >= 0 {
m.entries = Some(vec![EntryData::populated(version)]);
}
m
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EntryData<'a> {
pub entity: Vec<EntityData<'a>>,
pub values: Vec<ValueData<'a>>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl EntryData<'_> {
pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::EntryData {
crate::owned::describe_client_quotas_response::EntryData {
entity: (self.entity).iter().map(EntityData::to_owned).collect(),
values: (self.values).iter().map(ValueData::to_owned).collect(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for EntryData<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 1;
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.entity).len(), flex);
for it in &self.entity {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.values).len(), flex);
for it in &self.values {
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 = version >= 1;
let mut n: usize = 0;
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.entity).len(), flex);
let body: usize = (self.entity).iter().map(|it| it.encoded_len(version)).sum();
prefix + body
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.values).len(), flex);
let body: usize = (self.values).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 EntryData<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 1;
let mut out = Self::default();
if version >= 0 {
out.entity = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(EntityData::decode_borrow(buf, version)?);
}
v
};
}
if version >= 0 {
out.values = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(ValueData::decode_borrow(buf, version)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl EntryData<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.entity = vec![EntityData::populated(version)];
}
if version >= 0 {
m.values = vec![ValueData::populated(version)];
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EntityData<'a> {
pub entity_type: &'a str,
pub entity_name: Option<&'a str>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl EntityData<'_> {
pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::EntityData {
crate::owned::describe_client_quotas_response::EntityData {
entity_type: (self.entity_type).to_string(),
entity_name: (self.entity_name).map(std::string::ToString::to_string),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for EntityData<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 1;
if version >= 0 {
if flex {
put_compact_string(buf, self.entity_type);
} else {
put_string(buf, self.entity_type);
}
}
if version >= 0 {
if flex {
put_compact_nullable_string(buf, self.entity_name);
} else {
put_nullable_string(buf, self.entity_name);
}
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 1;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.entity_type)
} else {
string_len(self.entity_type)
};
}
if version >= 0 {
n += if flex {
compact_nullable_string_len(self.entity_name)
} else {
nullable_string_len(self.entity_name)
};
}
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 EntityData<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 1;
let mut out = Self::default();
if version >= 0 {
out.entity_type = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.entity_name = if flex {
get_compact_nullable_string_borrowed(buf)?
} else {
get_nullable_string_borrowed(buf)?
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl EntityData<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.entity_type = "x";
}
if version >= 0 {
m.entity_name = Some("x");
}
m
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ValueData<'a> {
pub key: &'a str,
pub value: f64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ValueData<'_> {
fn default() -> Self {
Self {
key: "",
value: 0.0f64,
unknown_tagged_fields: Default::default(),
}
}
}
impl ValueData<'_> {
pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::ValueData {
crate::owned::describe_client_quotas_response::ValueData {
key: (self.key).to_string(),
value: (self.value),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for ValueData<'_> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 1;
if version >= 0 {
if flex {
put_compact_string(buf, self.key);
} else {
put_string(buf, self.key);
}
}
if version >= 0 {
put_f64(buf, self.value);
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 1;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(self.key)
} else {
string_len(self.key)
};
}
if version >= 0 {
n += 8;
}
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 ValueData<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 1;
let mut out = Self::default();
if version >= 0 {
out.key = if flex {
get_compact_string_borrowed(buf)?
} else {
get_string_borrowed(buf)?
};
}
if version >= 0 {
out.value = get_f64(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl ValueData<'_> {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.key = "x";
}
if version >= 0 {
m.value = 1.0f64;
}
m
}
}