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

pub const API_KEY: i16 = 85;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 1;
pub const FLEXIBLE_MIN: i16 = 0;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WriteShareGroupStateResponse<'a> {
    pub results: Vec<WriteStateResult<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for WriteShareGroupStateResponse<'a> {
    fn default() -> Self {
        Self {
            results: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> WriteShareGroupStateResponse<'a> {
    pub fn to_owned(&self) -> crate::owned::write_share_group_state_response::WriteShareGroupStateResponse {
        crate::owned::write_share_group_state_response::WriteShareGroupStateResponse {
            results: (self.results).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for WriteShareGroupStateResponse<'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 { { crate::primitives::array::put_array_len(buf, (self.results).len(), flex); for it in &self.results { 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 += { let prefix = crate::primitives::array::array_len_prefix_len((self.results).len(), flex); let body: usize = (self.results).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 WriteShareGroupStateResponse<'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.results = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(WriteStateResult::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 WriteStateResult<'a> {
    pub topic_id: crate::primitives::uuid::Uuid,
    pub partitions: Vec<PartitionResult<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

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

impl<'a> WriteStateResult<'a> {
    pub fn to_owned(&self) -> crate::owned::write_share_group_state_response::WriteStateResult {
        crate::owned::write_share_group_state_response::WriteStateResult {
            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 WriteStateResult<'a> {
    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 { 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 >= 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(|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 WriteStateResult<'de> {
    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(PartitionResult::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 PartitionResult<'a> {
    pub partition: i32,
    pub error_code: i16,
    pub error_message: Option<&'a str>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for PartitionResult<'a> {
    fn default() -> Self {
        Self {
            partition: 0i32,
            error_code: 0i16,
            error_message: None,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> PartitionResult<'a> {
    pub fn to_owned(&self) -> crate::owned::write_share_group_state_response::PartitionResult {
        crate::owned::write_share_group_state_response::PartitionResult {
            partition: (self.partition),
            error_code: (self.error_code),
            error_message: (self.error_message).map(|s| s.to_string()),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for PartitionResult<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 0;
        if version >= 0 { put_i32(buf, self.partition) }
        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 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 += 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 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 PartitionResult<'de> {
    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.partition = 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 flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}