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 crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
use bytes::{BufMut, Bytes};

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

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SaslAuthenticateRequest<'a> {
    pub auth_bytes: &'a [u8],
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl SaslAuthenticateRequest<'_> {
    pub fn to_owned(&self) -> crate::owned::sasl_authenticate_request::SaslAuthenticateRequest {
        crate::owned::sasl_authenticate_request::SaslAuthenticateRequest {
            auth_bytes: Bytes::copy_from_slice(self.auth_bytes),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for SaslAuthenticateRequest<'_> {
    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_bytes(buf, self.auth_bytes);
            } else {
                put_bytes(buf, self.auth_bytes);
            }
        }
        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 {
                crate::primitives::varint::uvarint_len(
                    u32::try_from((self.auth_bytes).len() + 1).unwrap(),
                ) + (self.auth_bytes).len()
            } else {
                4 + (self.auth_bytes).len()
            };
        }
        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 SaslAuthenticateRequest<'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.auth_bytes = if flex {
                get_compact_bytes_borrowed(buf)?
            } else {
                get_bytes_borrowed(buf)?
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl SaslAuthenticateRequest<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.auth_bytes = &b"x"[..];
        }
        m
    }
}