krabka-protocol 0.7.0

Apache Kafka wire-protocol codec (4.3.0), with typed RecordBatch and zero-copy borrowed decode
Documentation
// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
use bytes::BufMut;

use crate::{
    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
    primitives::{
        fixed::{get_i32, get_i64, put_i32, put_i64},
        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
    },
    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 4;
pub const FLEXIBLE_MIN: i16 = 4;
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
    version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OffsetCommitValue<'a> {
    pub offset: i64,
    pub leader_epoch: i32,
    pub metadata: &'a str,
    pub commit_timestamp: i64,
    pub expire_timestamp: i64,
    pub topic_id: crate::primitives::uuid::Uuid,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for OffsetCommitValue<'_> {
    fn default() -> Self {
        Self {
            offset: 0i64,
            leader_epoch: -1i32,
            metadata: "",
            commit_timestamp: 0i64,
            expire_timestamp: -1i64,
            topic_id: crate::primitives::uuid::Uuid::default(),
            unknown_tagged_fields: UnknownTaggedFields::default(),
        }
    }
}
impl OffsetCommitValue<'_> {
    /// # Panics
    ///
    /// Panics if a records field contains an invalid encoded record batch.
    #[must_use]
    pub fn to_owned(&self) -> crate::owned::offset_commit_value::OffsetCommitValue {
        crate::owned::offset_commit_value::OffsetCommitValue {
            offset: (self.offset),
            leader_epoch: (self.leader_epoch),
            metadata: (self.metadata).to_string(),
            commit_timestamp: (self.commit_timestamp),
            expire_timestamp: (self.expire_timestamp),
            topic_id: (self.topic_id),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for OffsetCommitValue<'_> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "OffsetCommitValue version out of range",
            ));
        }
        let flex = is_flexible(version);
        if version >= 0 {
            put_i64(buf, self.offset);
        }
        if version >= 3 {
            put_i32(buf, self.leader_epoch);
        }
        if version >= 0 {
            if flex {
                let () = put_compact_string(buf, self.metadata);
            } else {
                let () = put_string(buf, self.metadata);
            }
        }
        if version >= 0 {
            put_i64(buf, self.commit_timestamp);
        }
        if version == 1 {
            put_i64(buf, self.expire_timestamp);
        }
        if flex {
            let mut tagged = WriteTaggedFields::new();
            if !(crate::codegen_helpers::is_default(&self.topic_id)) {
                let payload = encode_to_bytes(16, |b| {
                    crate::primitives::uuid::put_uuid(b, self.topic_id);
                    Ok(())
                });
                tagged.add(0, payload);
            }
            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 += 8;
        }
        if version >= 3 {
            n += 4;
        }
        if version >= 0 {
            n += if flex {
                compact_string_len(self.metadata)
            } else {
                string_len(self.metadata)
            };
        }
        if version >= 0 {
            n += 8;
        }
        if version == 1 {
            n += 8;
        }
        if flex {
            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
            if !(crate::codegen_helpers::is_default(&self.topic_id)) {
                known_pairs.push((0, 16));
            }
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}
impl<'de> DecodeBorrow<'de> for OffsetCommitValue<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "OffsetCommitValue version out of range",
            ));
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 0 {
            out.offset = get_i64(buf)?;
        }
        if version >= 3 {
            out.leader_epoch = get_i32(buf)?;
        }
        if version >= 0 {
            out.metadata = if flex {
                get_compact_string_borrowed(buf)?
            } else {
                get_string_borrowed(buf)?
            };
        }
        if version >= 0 {
            out.commit_timestamp = get_i64(buf)?;
        }
        if version == 1 {
            out.expire_timestamp = get_i64(buf)?;
        }
        if flex {
            let mut tag_topic_id = None;
            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
                0 => {
                    tag_topic_id = Some({
                        let b: &mut &[u8] = payload;
                        crate::primitives::uuid::get_uuid(b)?
                    });
                    Ok(true)
                }
                _ => Ok(false),
            })?;
            if let Some(v) = tag_topic_id {
                out.topic_id = v;
            }
        }
        Ok(out)
    }
}
#[cfg(test)]
impl OffsetCommitValue<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.offset = 1i64;
        }
        if version >= 3 {
            m.leader_epoch = 1i32;
        }
        if version >= 0 {
            m.metadata = "x";
        }
        if version >= 0 {
            m.commit_timestamp = 1i64;
        }
        if version == 1 {
            m.expire_timestamp = 1i64;
        }
        if version >= 4 {
            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
        }
        m
    }
}