crabka-protocol 0.3.3

Apache Kafka wire-protocol codec (4.3.0), with typed RecordBatch and zero-copy borrowed decode
Documentation
// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.

use bytes::BufMut;

use crate::primitives::fixed::{get_i32, put_i32};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 93;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;

#[inline]
fn is_flexible(version: i16) -> bool {
    version >= FLEXIBLE_MIN
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct GetReplicaLogInfoRequest {
    pub broker_id: i32,
    pub topic_partitions: Vec<TopicPartitions>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl GetReplicaLogInfoRequest {
    pub fn to_owned(&self) -> crate::owned::get_replica_log_info_request::GetReplicaLogInfoRequest {
        crate::owned::get_replica_log_info_request::GetReplicaLogInfoRequest {
            broker_id: (self.broker_id),
            topic_partitions: (self.topic_partitions)
                .iter()
                .map(TopicPartitions::to_owned)
                .collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for GetReplicaLogInfoRequest {
    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.broker_id);
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.topic_partitions).len(), flex);
                for it in &self.topic_partitions {
                    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 += {
                let prefix = crate::primitives::array::array_len_prefix_len(
                    (self.topic_partitions).len(),
                    flex,
                );
                let body: usize = (self.topic_partitions)
                    .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 GetReplicaLogInfoRequest {
    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.broker_id = get_i32(buf)?;
        }
        if version >= 0 {
            out.topic_partitions = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(TopicPartitions::decode_borrow(buf, version)?);
                }
                v
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl GetReplicaLogInfoRequest {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.broker_id = 1i32;
        }
        if version >= 0 {
            m.topic_partitions = vec![TopicPartitions::populated(version)];
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicPartitions {
    pub topic_id: crate::primitives::uuid::Uuid,
    pub partitions: Vec<i32>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl TopicPartitions {
    pub fn to_owned(&self) -> crate::owned::get_replica_log_info_request::TopicPartitions {
        crate::owned::get_replica_log_info_request::TopicPartitions {
            topic_id: (self.topic_id),
            partitions: (self.partitions).clone(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for TopicPartitions {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 0;
        if version >= 0 {
            crate::primitives::uuid::put_uuid(buf, self.topic_id);
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
                for it in &self.partitions {
                    put_i32(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 = version >= 0;
        let mut n: usize = 0;
        if version >= 0 {
            n += 16;
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
                let body: usize = (self.partitions).iter().map(|_| 4).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 TopicPartitions {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 0;
        let mut out = Self::default();
        if version >= 0 {
            out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
        }
        if version >= 0 {
            out.partitions = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(get_i32(buf)?);
                }
                v
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl TopicPartitions {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
        }
        if version >= 0 {
            m.partitions = vec![1i32];
        }
        m
    }
}