crabka-protocol 0.3.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, Bytes};

use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
use crate::primitives::string_bytes::{
    compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
    put_nullable_string,
};
use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
use crate::primitives::string_bytes_borrowed::{
    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 14;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 5;
pub const FLEXIBLE_MIN: i16 = 4;

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SyncGroupResponse<'a> {
    pub throttle_time_ms: i32,
    pub error_code: i16,
    pub protocol_type: Option<&'a str>,
    pub protocol_name: Option<&'a str>,
    pub assignment: &'a [u8],
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl SyncGroupResponse<'_> {
    pub fn to_owned(&self) -> crate::owned::sync_group_response::SyncGroupResponse {
        crate::owned::sync_group_response::SyncGroupResponse {
            throttle_time_ms: (self.throttle_time_ms),
            error_code: (self.error_code),
            protocol_type: (self.protocol_type).map(std::string::ToString::to_string),
            protocol_name: (self.protocol_name).map(std::string::ToString::to_string),
            assignment: Bytes::copy_from_slice(self.assignment),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for SyncGroupResponse<'_> {
    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 >= 1 {
            put_i32(buf, self.throttle_time_ms);
        }
        if version >= 0 {
            put_i16(buf, self.error_code);
        }
        if version >= 5 {
            if flex {
                put_compact_nullable_string(buf, self.protocol_type);
            } else {
                put_nullable_string(buf, self.protocol_type);
            }
        }
        if version >= 5 {
            if flex {
                put_compact_nullable_string(buf, self.protocol_name);
            } else {
                put_nullable_string(buf, self.protocol_name);
            }
        }
        if version >= 0 {
            if flex {
                put_compact_bytes(buf, self.assignment);
            } else {
                put_bytes(buf, self.assignment);
            }
        }
        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 >= 1 {
            n += 4;
        }
        if version >= 0 {
            n += 2;
        }
        if version >= 5 {
            n += if flex {
                compact_nullable_string_len(self.protocol_type)
            } else {
                nullable_string_len(self.protocol_type)
            };
        }
        if version >= 5 {
            n += if flex {
                compact_nullable_string_len(self.protocol_name)
            } else {
                nullable_string_len(self.protocol_name)
            };
        }
        if version >= 0 {
            n += if flex {
                crate::primitives::varint::uvarint_len(
                    u32::try_from((self.assignment).len() + 1).unwrap(),
                ) + (self.assignment).len()
            } else {
                4 + (self.assignment).len()
            };
        }
        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 SyncGroupResponse<'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 >= 1 {
            out.throttle_time_ms = get_i32(buf)?;
        }
        if version >= 0 {
            out.error_code = get_i16(buf)?;
        }
        if version >= 5 {
            out.protocol_type = if flex {
                get_compact_nullable_string_borrowed(buf)?
            } else {
                get_nullable_string_borrowed(buf)?
            };
        }
        if version >= 5 {
            out.protocol_name = if flex {
                get_compact_nullable_string_borrowed(buf)?
            } else {
                get_nullable_string_borrowed(buf)?
            };
        }
        if version >= 0 {
            out.assignment = if flex {
                get_compact_bytes_borrowed(buf)?
            } else {
                get_bytes_borrowed(buf)?
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl SyncGroupResponse<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 1 {
            m.throttle_time_ms = 1i32;
        }
        if version >= 0 {
            m.error_code = 1i16;
        }
        if version >= 5 {
            m.protocol_type = Some("x");
        }
        if version >= 5 {
            m.protocol_name = Some("x");
        }
        if version >= 0 {
            m.assignment = &b"x"[..];
        }
        m
    }
}