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_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64,
};
use crate::primitives::string_bytes::{
    compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
    put_nullable_string,
};
use crate::primitives::string_bytes_borrowed::{
    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 22;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 6;
pub const FLEXIBLE_MIN: i16 = 2;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InitProducerIdRequest<'a> {
    pub transactional_id: Option<&'a str>,
    pub transaction_timeout_ms: i32,
    pub producer_id: i64,
    pub producer_epoch: i16,
    pub enable2_pc: bool,
    pub keep_prepared_txn: bool,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for InitProducerIdRequest<'_> {
    fn default() -> Self {
        Self {
            transactional_id: None,
            transaction_timeout_ms: 0i32,
            producer_id: -1i64,
            producer_epoch: -1i16,
            enable2_pc: false,
            keep_prepared_txn: false,
            unknown_tagged_fields: Default::default(),
        }
    }
}
impl InitProducerIdRequest<'_> {
    pub fn to_owned(&self) -> crate::owned::init_producer_id_request::InitProducerIdRequest {
        crate::owned::init_producer_id_request::InitProducerIdRequest {
            transactional_id: (self.transactional_id).map(std::string::ToString::to_string),
            transaction_timeout_ms: (self.transaction_timeout_ms),
            producer_id: (self.producer_id),
            producer_epoch: (self.producer_epoch),
            enable2_pc: (self.enable2_pc),
            keep_prepared_txn: (self.keep_prepared_txn),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for InitProducerIdRequest<'_> {
    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_nullable_string(buf, self.transactional_id);
            } else {
                put_nullable_string(buf, self.transactional_id);
            }
        }
        if version >= 0 {
            put_i32(buf, self.transaction_timeout_ms);
        }
        if version >= 3 {
            put_i64(buf, self.producer_id);
        }
        if version >= 3 {
            put_i16(buf, self.producer_epoch);
        }
        if version >= 6 {
            put_bool(buf, self.enable2_pc);
        }
        if version >= 6 {
            put_bool(buf, self.keep_prepared_txn);
        }
        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_nullable_string_len(self.transactional_id)
            } else {
                nullable_string_len(self.transactional_id)
            };
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 3 {
            n += 8;
        }
        if version >= 3 {
            n += 2;
        }
        if version >= 6 {
            n += 1;
        }
        if version >= 6 {
            n += 1;
        }
        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 InitProducerIdRequest<'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.transactional_id = if flex {
                get_compact_nullable_string_borrowed(buf)?
            } else {
                get_nullable_string_borrowed(buf)?
            };
        }
        if version >= 0 {
            out.transaction_timeout_ms = get_i32(buf)?;
        }
        if version >= 3 {
            out.producer_id = get_i64(buf)?;
        }
        if version >= 3 {
            out.producer_epoch = get_i16(buf)?;
        }
        if version >= 6 {
            out.enable2_pc = get_bool(buf)?;
        }
        if version >= 6 {
            out.keep_prepared_txn = get_bool(buf)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl InitProducerIdRequest<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.transactional_id = Some("x");
        }
        if version >= 0 {
            m.transaction_timeout_ms = 1i32;
        }
        if version >= 3 {
            m.producer_id = 1i64;
        }
        if version >= 3 {
            m.producer_epoch = 1i16;
        }
        if version >= 6 {
            m.enable2_pc = true;
        }
        if version >= 6 {
            m.keep_prepared_txn = true;
        }
        m
    }
}