crabka-protocol 0.2.0

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_i32, put_i32};
use crate::primitives::string_bytes::{
    compact_string_len, put_compact_string, put_string, string_len,
};
use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 47;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 32767;

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetDeleteRequest<'a> {
    pub group_id: &'a str,
    pub topics: Vec<OffsetDeleteRequestTopic<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl OffsetDeleteRequest<'_> {
    pub fn to_owned(&self) -> crate::owned::offset_delete_request::OffsetDeleteRequest {
        crate::owned::offset_delete_request::OffsetDeleteRequest {
            group_id: (self.group_id).to_string(),
            topics: (self.topics)
                .iter()
                .map(OffsetDeleteRequestTopic::to_owned)
                .collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for OffsetDeleteRequest<'_> {
    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_string(buf, self.group_id);
            } else {
                put_string(buf, self.group_id);
            }
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
                for it in &self.topics {
                    it.encode(buf, version)?;
                }
            }
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = is_flexible(version);
        let mut n: usize = 0;
        if version >= 0 {
            n += if flex {
                compact_string_len(self.group_id)
            } else {
                string_len(self.group_id)
            };
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
                prefix + body
            };
        }
        n
    }
}
impl<'de> DecodeBorrow<'de> for OffsetDeleteRequest<'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.group_id = if flex {
                get_compact_string_borrowed(buf)?
            } else {
                get_string_borrowed(buf)?
            };
        }
        if version >= 0 {
            out.topics = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(OffsetDeleteRequestTopic::decode_borrow(buf, version)?);
                }
                v
            };
        }
        Ok(out)
    }
}
#[cfg(test)]
impl OffsetDeleteRequest<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.group_id = "x";
        }
        if version >= 0 {
            m.topics = vec![OffsetDeleteRequestTopic::populated(version)];
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetDeleteRequestTopic<'a> {
    pub name: &'a str,
    pub partitions: Vec<OffsetDeleteRequestPartition>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl OffsetDeleteRequestTopic<'_> {
    pub fn to_owned(&self) -> crate::owned::offset_delete_request::OffsetDeleteRequestTopic {
        crate::owned::offset_delete_request::OffsetDeleteRequestTopic {
            name: (self.name).to_string(),
            partitions: (self.partitions)
                .iter()
                .map(OffsetDeleteRequestPartition::to_owned)
                .collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for OffsetDeleteRequestTopic<'_> {
    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.name);
            } else {
                put_string(buf, self.name);
            }
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
                for it in &self.partitions {
                    it.encode(buf, version)?;
                }
            }
        }
        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.name)
            } else {
                string_len(self.name)
            };
        }
        if version >= 0 {
            n += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
                let body: usize = (self.partitions)
                    .iter()
                    .map(|it| it.encoded_len(version))
                    .sum();
                prefix + body
            };
        }
        n
    }
}
impl<'de> DecodeBorrow<'de> for OffsetDeleteRequestTopic<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 32767;
        let mut out = Self::default();
        if version >= 0 {
            out.name = if flex {
                get_compact_string_borrowed(buf)?
            } else {
                get_string_borrowed(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(OffsetDeleteRequestPartition::decode_borrow(buf, version)?);
                }
                v
            };
        }
        Ok(out)
    }
}
#[cfg(test)]
impl OffsetDeleteRequestTopic<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.name = "x";
        }
        if version >= 0 {
            m.partitions = vec![OffsetDeleteRequestPartition::populated(version)];
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OffsetDeleteRequestPartition {
    pub partition_index: i32,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl OffsetDeleteRequestPartition {
    pub fn to_owned(&self) -> crate::owned::offset_delete_request::OffsetDeleteRequestPartition {
        crate::owned::offset_delete_request::OffsetDeleteRequestPartition {
            partition_index: (self.partition_index),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for OffsetDeleteRequestPartition {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 32767;
        if version >= 0 {
            put_i32(buf, self.partition_index);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 32767;
        let mut n: usize = 0;
        if version >= 0 {
            n += 4;
        }
        n
    }
}
impl<'de> DecodeBorrow<'de> for OffsetDeleteRequestPartition {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 32767;
        let mut out = Self::default();
        if version >= 0 {
            out.partition_index = get_i32(buf)?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl OffsetDeleteRequestPartition {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.partition_index = 1i32;
        }
        m
    }
}