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_bool, get_i16, get_i32, get_u16, put_bool, put_i16, put_i32, put_u16};
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::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 70;
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 ControllerRegistrationRequest<'a> {
    pub controller_id: i32,
    pub incarnation_id: crate::primitives::uuid::Uuid,
    pub zk_migration_ready: bool,
    pub listeners: Vec<Listener<'a>>,
    pub features: Vec<Feature<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for ControllerRegistrationRequest<'a> {
    fn default() -> Self {
        Self {
            controller_id: 0i32,
            incarnation_id: Default::default(),
            zk_migration_ready: false,
            listeners: Vec::new(),
            features: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> ControllerRegistrationRequest<'a> {
    pub fn to_owned(&self) -> crate::owned::controller_registration_request::ControllerRegistrationRequest {
        crate::owned::controller_registration_request::ControllerRegistrationRequest {
            controller_id: (self.controller_id),
            incarnation_id: (self.incarnation_id),
            zk_migration_ready: (self.zk_migration_ready),
            listeners: (self.listeners).iter().map(|it| it.to_owned()).collect(),
            features: (self.features).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for ControllerRegistrationRequest<'a> {
    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 { put_i32(buf, self.controller_id) }
        if version >= 0 { crate::primitives::uuid::put_uuid(buf, self.incarnation_id) }
        if version >= 0 { put_bool(buf, self.zk_migration_ready) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex); for it in &self.listeners { it.encode(buf, version)?; } } }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.features).len(), flex); for it in &self.features { it.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 += 4; }
        if version >= 0 { n += 16; }
        if version >= 0 { n += 1; }
        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex); let body: usize = (self.listeners).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.features).len(), flex); let body: usize = (self.features).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
        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 ControllerRegistrationRequest<'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.controller_id = get_i32(buf)?; }
        if version >= 0 { out.incarnation_id = crate::primitives::uuid::get_uuid(buf)?; }
        if version >= 0 { out.zk_migration_ready = get_bool(buf)?; }
        if version >= 0 { out.listeners = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(Listener::decode_borrow(buf, version)?); } v }; }
        if version >= 0 { out.features = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(Feature::decode_borrow(buf, version)?); } v }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Listener<'a> {
    pub name: &'a str,
    pub host: &'a str,
    pub port: u16,
    pub security_protocol: i16,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for Listener<'a> {
    fn default() -> Self {
        Self {
            name: "",
            host: "",
            port: 0u16,
            security_protocol: 0i16,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> Listener<'a> {
    pub fn to_owned(&self) -> crate::owned::controller_registration_request::Listener {
        crate::owned::controller_registration_request::Listener {
            name: (self.name).to_string(),
            host: (self.host).to_string(),
            port: (self.port),
            security_protocol: (self.security_protocol),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for Listener<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 0;
        if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
        if version >= 0 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
        if version >= 0 { put_u16(buf, self.port) }
        if version >= 0 { put_i16(buf, self.security_protocol) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 0;
        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 += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
        if version >= 0 { n += 2; }
        if version >= 0 { n += 2; }
        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 Listener<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 0;
        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.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 0 { out.port = get_u16(buf)?; }
        if version >= 0 { out.security_protocol = get_i16(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Feature<'a> {
    pub name: &'a str,
    pub min_supported_version: i16,
    pub max_supported_version: i16,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for Feature<'a> {
    fn default() -> Self {
        Self {
            name: "",
            min_supported_version: 0i16,
            max_supported_version: 0i16,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> Feature<'a> {
    pub fn to_owned(&self) -> crate::owned::controller_registration_request::Feature {
        crate::owned::controller_registration_request::Feature {
            name: (self.name).to_string(),
            min_supported_version: (self.min_supported_version),
            max_supported_version: (self.max_supported_version),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for Feature<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 0;
        if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
        if version >= 0 { put_i16(buf, self.min_supported_version) }
        if version >= 0 { put_i16(buf, self.max_supported_version) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 0;
        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 += 2; }
        if version >= 0 { n += 2; }
        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 Feature<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 0;
        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.min_supported_version = get_i16(buf)?; }
        if version >= 0 { out.max_supported_version = get_i16(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}