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, put_i32},
        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, read_tagged_fields, tagged_fields_len},
};
pub const API_KEY: i16 = 93;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const LATEST_STABLE_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
    version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StreamsGroupTopologyDescriptionUpdateRequest<'a> {
    pub group_id: &'a str,
    pub member_id: &'a str,
    pub topology_epoch: i32,
    pub topology_description: super::common::streams_group_topology_description_update_request::topology_description::TopologyDescription<'a>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl StreamsGroupTopologyDescriptionUpdateRequest<'_> {
    /// # Panics
    ///
    /// Panics if a records field contains an invalid encoded record batch.
    #[must_use]
    pub fn to_owned(&self) -> crate::owned::streams_group_topology_description_update_request::StreamsGroupTopologyDescriptionUpdateRequest{
        crate::owned::streams_group_topology_description_update_request::StreamsGroupTopologyDescriptionUpdateRequest {
            group_id: (self.group_id).to_string(),
            member_id: (self.member_id).to_string(),
            topology_epoch: (self.topology_epoch),
            topology_description: (self.topology_description).to_owned(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for StreamsGroupTopologyDescriptionUpdateRequest<'_> {
    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 {
                let () = put_compact_string(buf, self.group_id);
            } else {
                let () = put_string(buf, self.group_id);
            }
        }
        if version >= 0 {
            if flex {
                let () = put_compact_string(buf, self.member_id);
            } else {
                let () = put_string(buf, self.member_id);
            }
        }
        if version >= 0 {
            put_i32(buf, self.topology_epoch);
        }
        if version >= 0 {
            self.topology_description.encode(buf, version)?;
        }
        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 += if flex {
                compact_string_len(self.group_id)
            } else {
                string_len(self.group_id)
            };
        }
        if version >= 0 {
            n += if flex {
                compact_string_len(self.member_id)
            } else {
                string_len(self.member_id)
            };
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 0 {
            n += self.topology_description.encoded_len(version);
        }
        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 StreamsGroupTopologyDescriptionUpdateRequest<'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.member_id = if flex {
                get_compact_string_borrowed(buf)?
            } else {
                get_string_borrowed(buf)?
            };
        }
        if version >= 0 {
            out.topology_epoch = get_i32(buf)?;
        }
        if version >= 0 {
            out.topology_description = super::common::streams_group_topology_description_update_request::topology_description::TopologyDescription::decode_borrow(buf, version)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl StreamsGroupTopologyDescriptionUpdateRequest<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.group_id = "x";
        }
        if version >= 0 {
            m.member_id = "x";
        }
        if version >= 0 {
            m.topology_epoch = 1i32;
        }
        if version >= 0 {
            m.topology_description = super::common::streams_group_topology_description_update_request::topology_description::TopologyDescription::populated(version);
        }
        m
    }
}