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::{Buf, BufMut};

use crate::primitives::fixed::{get_i32, put_i32};
use crate::primitives::string_bytes::{
    compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned,
    nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes,
};
use crate::primitives::string_bytes::{
    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
    string_len,
};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 3;
pub const FLEXIBLE_MIN: i16 = 32767;

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ConsumerProtocolAssignment {
    pub assigned_partitions: Vec<TopicPartition>,
    pub user_data: Option<::bytes::Bytes>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ConsumerProtocolAssignment {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "ConsumerProtocolAssignment version out of range",
            ));
        }
        let flex = is_flexible(version);
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(
                    buf,
                    (self.assigned_partitions).len(),
                    flex,
                );
                for it in &self.assigned_partitions {
                    it.encode(buf, version)?;
                }
            }
        }
        if version >= 0 {
            if flex {
                put_compact_nullable_bytes(buf, self.user_data.as_deref());
            } else {
                put_nullable_bytes(buf, self.user_data.as_deref());
            }
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = is_flexible(version);
        let mut n: usize = 0;
        if version >= 0 {
            n += {
                let prefix = crate::primitives::array::array_len_prefix_len(
                    (self.assigned_partitions).len(),
                    flex,
                );
                let body: usize = (self.assigned_partitions)
                    .iter()
                    .map(|it| it.encoded_len(version))
                    .sum();
                prefix + body
            };
        }
        if version >= 0 {
            n += if flex {
                compact_nullable_bytes_len(self.user_data.as_deref())
            } else {
                nullable_bytes_len(self.user_data.as_deref())
            };
        }
        n
    }
}
impl Decode<'_> for ConsumerProtocolAssignment {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "ConsumerProtocolAssignment version out of range",
            ));
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 0 {
            out.assigned_partitions = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(TopicPartition::decode(buf, version)?);
                }
                v
            };
        }
        if version >= 0 {
            out.user_data = if flex {
                get_compact_nullable_bytes_owned(buf)?
            } else {
                get_nullable_bytes_owned(buf)?
            };
        }
        Ok(out)
    }
}
#[cfg(test)]
impl ConsumerProtocolAssignment {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.assigned_partitions = vec![TopicPartition::populated(version)];
        }
        if version >= 0 {
            m.user_data = Some(::bytes::Bytes::from_static(b"x"));
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicPartition {
    pub topic: String,
    pub partitions: Vec<i32>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for TopicPartition {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 32767;
        if version >= 0 {
            if flex {
                put_compact_string(buf, &self.topic);
            } else {
                put_string(buf, &self.topic);
            }
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
                for it in &self.partitions {
                    put_i32(buf, *it);
                }
            }
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 32767;
        let mut n: usize = 0;
        if version >= 0 {
            n += if flex {
                compact_string_len(&self.topic)
            } else {
                string_len(&self.topic)
            };
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
                let body: usize = (self.partitions).iter().map(|_| 4).sum();
                prefix + body
            };
        }
        n
    }
}
impl Decode<'_> for TopicPartition {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 32767;
        let mut out = Self::default();
        if version >= 0 {
            out.topic = if flex {
                get_compact_string_owned(buf)?
            } else {
                get_string_owned(buf)?
            };
        }
        if version >= 0 {
            out.partitions = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(get_i32(buf)?);
                }
                v
            };
        }
        Ok(out)
    }
}
#[cfg(test)]
impl TopicPartition {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.topic = "x".to_string();
        }
        if version >= 0 {
            m.partitions = vec![1i32];
        }
        m
    }
}

/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
/// Only includes fields valid for the given version.
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
    let mut obj = ::serde_json::Map::new();
    obj.insert(
        "assignedPartitions".to_string(),
        ::serde_json::Value::Array(vec![]),
    );
    obj.insert("userData".to_string(), ::serde_json::Value::Null);
    ::serde_json::Value::Object(obj)
}