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_i64, put_i16, put_i64};
use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnapshotHeaderRecord {
    pub version: i16,
    pub last_contained_log_timestamp: i64,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for SnapshotHeaderRecord {
    fn default() -> Self {
        Self {
            version: 0i16,
            last_contained_log_timestamp: 0i64,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl SnapshotHeaderRecord {
    pub fn to_owned(&self) -> crate::owned::snapshot_header_record::SnapshotHeaderRecord {
        crate::owned::snapshot_header_record::SnapshotHeaderRecord {
            version: (self.version),
            last_contained_log_timestamp: (self.last_contained_log_timestamp),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for SnapshotHeaderRecord {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch("SnapshotHeaderRecord version out of range"));
        }
        let flex = is_flexible(version);
        if version >= 0 { put_i16(buf, self.version) }
        if version >= 0 { put_i64(buf, self.last_contained_log_timestamp) }
        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 += 8; }
        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 SnapshotHeaderRecord {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch("SnapshotHeaderRecord version out of range"));
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 0 { out.version = get_i16(buf)?; }
        if version >= 0 { out.last_contained_log_timestamp = get_i64(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}