crabka-protocol 0.1.2

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, get_i64, put_i32, put_i64};
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::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 8;
pub const MIN_VERSION: i16 = 2;
pub const MAX_VERSION: i16 = 10;
pub const FLEXIBLE_MIN: i16 = 8;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OffsetCommitRequest<'a> {
    pub group_id: &'a str,
    pub generation_id_or_member_epoch: i32,
    pub member_id: &'a str,
    pub group_instance_id: Option<&'a str>,
    pub retention_time_ms: i64,
    pub topics: Vec<OffsetCommitRequestTopic<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for OffsetCommitRequest<'a> {
    fn default() -> Self {
        Self {
            group_id: "",
            generation_id_or_member_epoch: -1i32,
            member_id: "",
            group_instance_id: None,
            retention_time_ms: -1i64,
            topics: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> OffsetCommitRequest<'a> {
    pub fn to_owned(&self) -> crate::owned::offset_commit_request::OffsetCommitRequest {
        crate::owned::offset_commit_request::OffsetCommitRequest {
            group_id: (self.group_id).to_string(),
            generation_id_or_member_epoch: (self.generation_id_or_member_epoch),
            member_id: (self.member_id).to_string(),
            group_instance_id: (self.group_instance_id).map(|s| s.to_string()),
            retention_time_ms: (self.retention_time_ms),
            topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for OffsetCommitRequest<'a> {
    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 { if flex { put_compact_string(buf, self.group_id) } else { put_string(buf, self.group_id) } }
        if version >= 1 { put_i32(buf, self.generation_id_or_member_epoch) }
        if version >= 1 { if flex { put_compact_string(buf, self.member_id) } else { put_string(buf, self.member_id) } }
        if version >= 7 { if flex { put_compact_nullable_string(buf, self.group_instance_id) } else { put_nullable_string(buf, self.group_instance_id) } }
        if version >= 2 && version <= 4 { put_i64(buf, self.retention_time_ms) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { 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 += if flex { compact_string_len(self.group_id) } else { string_len(self.group_id) }; }
        if version >= 1 { n += 4; }
        if version >= 1 { n += if flex { compact_string_len(self.member_id) } else { string_len(self.member_id) }; }
        if version >= 7 { n += if flex { compact_nullable_string_len(self.group_instance_id) } else { nullable_string_len(self.group_instance_id) }; }
        if version >= 2 && version <= 4 { n += 8; }
        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 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 OffsetCommitRequest<'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.group_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 1 { out.generation_id_or_member_epoch = get_i32(buf)?; }
        if version >= 1 { out.member_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 7 { out.group_instance_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
        if version >= 2 && version <= 4 { out.retention_time_ms = get_i64(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(OffsetCommitRequestTopic::decode_borrow(buf, version)?); } v }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OffsetCommitRequestTopic<'a> {
    pub name: &'a str,
    pub topic_id: crate::primitives::uuid::Uuid,
    pub partitions: Vec<OffsetCommitRequestPartition<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for OffsetCommitRequestTopic<'a> {
    fn default() -> Self {
        Self {
            name: "",
            topic_id: Default::default(),
            partitions: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> OffsetCommitRequestTopic<'a> {
    pub fn to_owned(&self) -> crate::owned::offset_commit_request::OffsetCommitRequestTopic {
        crate::owned::offset_commit_request::OffsetCommitRequestTopic {
            name: (self.name).to_string(),
            topic_id: (self.topic_id),
            partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for OffsetCommitRequestTopic<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 8;
        if version >= 0 && version <= 9 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
        if version >= 10 { 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 { 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 >= 8;
        let mut n: usize = 0;
        if version >= 0 && version <= 9 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
        if version >= 10 { 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(|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 OffsetCommitRequestTopic<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 8;
        let mut out = Self::default();
        if version >= 0 && version <= 9 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 10 { 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(OffsetCommitRequestPartition::decode_borrow(buf, version)?); } v }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OffsetCommitRequestPartition<'a> {
    pub partition_index: i32,
    pub committed_offset: i64,
    pub committed_leader_epoch: i32,
    pub committed_metadata: Option<&'a str>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for OffsetCommitRequestPartition<'a> {
    fn default() -> Self {
        Self {
            partition_index: 0i32,
            committed_offset: 0i64,
            committed_leader_epoch: -1i32,
            committed_metadata: None,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> OffsetCommitRequestPartition<'a> {
    pub fn to_owned(&self) -> crate::owned::offset_commit_request::OffsetCommitRequestPartition {
        crate::owned::offset_commit_request::OffsetCommitRequestPartition {
            partition_index: (self.partition_index),
            committed_offset: (self.committed_offset),
            committed_leader_epoch: (self.committed_leader_epoch),
            committed_metadata: (self.committed_metadata).map(|s| s.to_string()),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for OffsetCommitRequestPartition<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 8;
        if version >= 0 { put_i32(buf, self.partition_index) }
        if version >= 0 { put_i64(buf, self.committed_offset) }
        if version >= 6 { put_i32(buf, self.committed_leader_epoch) }
        if version >= 0 { if flex { put_compact_nullable_string(buf, self.committed_metadata) } else { put_nullable_string(buf, self.committed_metadata) } }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 8;
        let mut n: usize = 0;
        if version >= 0 { n += 4; }
        if version >= 0 { n += 8; }
        if version >= 6 { n += 4; }
        if version >= 0 { n += if flex { compact_nullable_string_len(self.committed_metadata) } else { nullable_string_len(self.committed_metadata) }; }
        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 OffsetCommitRequestPartition<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 8;
        let mut out = Self::default();
        if version >= 0 { out.partition_index = get_i32(buf)?; }
        if version >= 0 { out.committed_offset = get_i64(buf)?; }
        if version >= 6 { out.committed_leader_epoch = get_i32(buf)?; }
        if version >= 0 { out.committed_metadata = 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)
    }
}