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_i16, get_i32, put_i16, put_i32};
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 MIN_VERSION: i16 = 1;
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 RequestHeader<'a> {
    pub request_api_key: i16,
    pub request_api_version: i16,
    pub correlation_id: i32,
    pub client_id: Option<&'a str>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl RequestHeader<'_> {
    pub fn to_owned(&self) -> crate::owned::request_header::RequestHeader {
        crate::owned::request_header::RequestHeader {
            request_api_key: (self.request_api_key),
            request_api_version: (self.request_api_version),
            correlation_id: (self.correlation_id),
            client_id: (self.client_id).map(std::string::ToString::to_string),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for RequestHeader<'_> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "RequestHeader version out of range",
            ));
        }
        let flex = is_flexible(version);
        if version >= 0 {
            put_i16(buf, self.request_api_key);
        }
        if version >= 0 {
            put_i16(buf, self.request_api_version);
        }
        if version >= 0 {
            put_i32(buf, self.correlation_id);
        }
        if version >= 1 {
            {
                let flex = false;
                if flex {
                    put_compact_nullable_string(buf, self.client_id);
                } else {
                    put_nullable_string(buf, self.client_id);
                }
            }
        }
        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 += 2;
        }
        if version >= 0 {
            n += 2;
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 1 {
            n += {
                let flex = false;
                if flex {
                    compact_nullable_string_len(self.client_id)
                } else {
                    nullable_string_len(self.client_id)
                }
            };
        }
        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 RequestHeader<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "RequestHeader version out of range",
            ));
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 0 {
            out.request_api_key = get_i16(buf)?;
        }
        if version >= 0 {
            out.request_api_version = get_i16(buf)?;
        }
        if version >= 0 {
            out.correlation_id = get_i32(buf)?;
        }
        if version >= 1 {
            out.client_id = {
                let flex = false;
                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)
    }
}
#[cfg(test)]
impl RequestHeader<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.request_api_key = 1i16;
        }
        if version >= 0 {
            m.request_api_version = 1i16;
        }
        if version >= 0 {
            m.correlation_id = 1i32;
        }
        if version >= 1 {
            m.client_id = Some("x");
        }
        m
    }
}