crabka-protocol 0.3.1

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_i8, get_i32, get_i64, put_i8, put_i32, put_i64};
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 = 2;
pub const MIN_VERSION: i16 = 1;
pub const MAX_VERSION: i16 = 11;
pub const FLEXIBLE_MIN: i16 = 6;

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ListOffsetsRequest<'a> {
    pub replica_id: i32,
    pub isolation_level: i8,
    pub topics: Vec<ListOffsetsTopic<'a>>,
    pub timeout_ms: i32,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl ListOffsetsRequest<'_> {
    pub fn to_owned(&self) -> crate::owned::list_offsets_request::ListOffsetsRequest {
        crate::owned::list_offsets_request::ListOffsetsRequest {
            replica_id: (self.replica_id),
            isolation_level: (self.isolation_level),
            topics: (self.topics)
                .iter()
                .map(ListOffsetsTopic::to_owned)
                .collect(),
            timeout_ms: (self.timeout_ms),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for ListOffsetsRequest<'_> {
    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.replica_id);
        }
        if version >= 2 {
            put_i8(buf, self.isolation_level);
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
                for it in &self.topics {
                    it.encode(buf, version)?;
                }
            }
        }
        if version >= 10 {
            put_i32(buf, self.timeout_ms);
        }
        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 >= 2 {
            n += 1;
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
                prefix + body
            };
        }
        if version >= 10 {
            n += 4;
        }
        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 ListOffsetsRequest<'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.replica_id = get_i32(buf)?;
        }
        if version >= 2 {
            out.isolation_level = get_i8(buf)?;
        }
        if version >= 0 {
            out.topics = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(ListOffsetsTopic::decode_borrow(buf, version)?);
                }
                v
            };
        }
        if version >= 10 {
            out.timeout_ms = get_i32(buf)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl ListOffsetsRequest<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.replica_id = 1i32;
        }
        if version >= 2 {
            m.isolation_level = 1i8;
        }
        if version >= 0 {
            m.topics = vec![ListOffsetsTopic::populated(version)];
        }
        if version >= 10 {
            m.timeout_ms = 1i32;
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ListOffsetsTopic<'a> {
    pub name: &'a str,
    pub partitions: Vec<ListOffsetsPartition>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl ListOffsetsTopic<'_> {
    pub fn to_owned(&self) -> crate::owned::list_offsets_request::ListOffsetsTopic {
        crate::owned::list_offsets_request::ListOffsetsTopic {
            name: (self.name).to_string(),
            partitions: (self.partitions)
                .iter()
                .map(ListOffsetsPartition::to_owned)
                .collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for ListOffsetsTopic<'_> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 6;
        if version >= 0 {
            if flex {
                put_compact_string(buf, self.name);
            } else {
                put_string(buf, self.name);
            }
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
                for it in &self.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 = version >= 6;
        let mut n: usize = 0;
        if version >= 0 {
            n += if flex {
                compact_string_len(self.name)
            } else {
                string_len(self.name)
            };
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
                let body: usize = (self.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 ListOffsetsTopic<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 6;
        let mut out = Self::default();
        if version >= 0 {
            out.name = if flex {
                get_compact_string_borrowed(buf)?
            } else {
                get_string_borrowed(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(ListOffsetsPartition::decode_borrow(buf, version)?);
                }
                v
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl ListOffsetsTopic<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.name = "x";
        }
        if version >= 0 {
            m.partitions = vec![ListOffsetsPartition::populated(version)];
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListOffsetsPartition {
    pub partition_index: i32,
    pub current_leader_epoch: i32,
    pub timestamp: i64,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ListOffsetsPartition {
    fn default() -> Self {
        Self {
            partition_index: 0i32,
            current_leader_epoch: -1i32,
            timestamp: 0i64,
            unknown_tagged_fields: Default::default(),
        }
    }
}
impl ListOffsetsPartition {
    pub fn to_owned(&self) -> crate::owned::list_offsets_request::ListOffsetsPartition {
        crate::owned::list_offsets_request::ListOffsetsPartition {
            partition_index: (self.partition_index),
            current_leader_epoch: (self.current_leader_epoch),
            timestamp: (self.timestamp),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for ListOffsetsPartition {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 6;
        if version >= 0 {
            put_i32(buf, self.partition_index);
        }
        if version >= 4 {
            put_i32(buf, self.current_leader_epoch);
        }
        if version >= 0 {
            put_i64(buf, self.timestamp);
        }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 6;
        let mut n: usize = 0;
        if version >= 0 {
            n += 4;
        }
        if version >= 4 {
            n += 4;
        }
        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 ListOffsetsPartition {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 6;
        let mut out = Self::default();
        if version >= 0 {
            out.partition_index = get_i32(buf)?;
        }
        if version >= 4 {
            out.current_leader_epoch = get_i32(buf)?;
        }
        if version >= 0 {
            out.timestamp = get_i64(buf)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl ListOffsetsPartition {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.partition_index = 1i32;
        }
        if version >= 4 {
            m.current_leader_epoch = 1i32;
        }
        if version >= 0 {
            m.timestamp = 1i64;
        }
        m
    }
}