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, put_bool, put_i16, put_i32};
use crate::primitives::string_bytes::{
    compact_nullable_string_len, compact_string_len, nullable_string_len,
    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
    string_len,
};
use crate::primitives::string_bytes_borrowed::{
    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
    get_nullable_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 = 19;
pub const MIN_VERSION: i16 = 2;
pub const MAX_VERSION: i16 = 7;
pub const FLEXIBLE_MIN: i16 = 5;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateTopicsRequest<'a> {
    pub topics: Vec<CreatableTopic<'a>>,
    pub timeout_ms: i32,
    pub validate_only: bool,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for CreateTopicsRequest<'a> {
    fn default() -> Self {
        Self {
            topics: Vec::new(),
            timeout_ms: 60_000i32,
            validate_only: false,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> CreateTopicsRequest<'a> {
    pub fn to_owned(&self) -> crate::owned::create_topics_request::CreateTopicsRequest {
        crate::owned::create_topics_request::CreateTopicsRequest {
            topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
            timeout_ms: (self.timeout_ms),
            validate_only: (self.validate_only),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for CreateTopicsRequest<'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 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
        if version >= 0 { put_i32(buf, self.timeout_ms) }
        if version >= 1 { put_bool(buf, self.validate_only) }
        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 += { 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 }; }
        if version >= 0 { n += 4; }
        if version >= 1 { n += 1; }
        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 CreateTopicsRequest<'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.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopic::decode_borrow(buf, version)?); } v }; }
        if version >= 0 { out.timeout_ms = get_i32(buf)?; }
        if version >= 1 { out.validate_only = get_bool(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreatableTopic<'a> {
    pub name: &'a str,
    pub num_partitions: i32,
    pub replication_factor: i16,
    pub assignments: Vec<CreatableReplicaAssignment>,
    pub configs: Vec<CreatableTopicConfig<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for CreatableTopic<'a> {
    fn default() -> Self {
        Self {
            name: "",
            num_partitions: 0i32,
            replication_factor: 0i16,
            assignments: Vec::new(),
            configs: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> CreatableTopic<'a> {
    pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopic {
        crate::owned::create_topics_request::CreatableTopic {
            name: (self.name).to_string(),
            num_partitions: (self.num_partitions),
            replication_factor: (self.replication_factor),
            assignments: (self.assignments).iter().map(|it| it.to_owned()).collect(),
            configs: (self.configs).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for CreatableTopic<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 5;
        if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
        if version >= 0 { put_i32(buf, self.num_partitions) }
        if version >= 0 { put_i16(buf, self.replication_factor) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.assignments).len(), flex); for it in &self.assignments { it.encode(buf, version)?; } } }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.configs).len(), flex); for it in &self.configs { 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 = version >= 5;
        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 += 4; }
        if version >= 0 { n += 2; }
        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.assignments).len(), flex); let body: usize = (self.assignments).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.configs).len(), flex); let body: usize = (self.configs).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 CreatableTopic<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 5;
        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.num_partitions = get_i32(buf)?; }
        if version >= 0 { out.replication_factor = get_i16(buf)?; }
        if version >= 0 { out.assignments = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableReplicaAssignment::decode_borrow(buf, version)?); } v }; }
        if version >= 0 { out.configs = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopicConfig::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 CreatableReplicaAssignment {
    pub partition_index: i32,
    pub broker_ids: Vec<i32>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for CreatableReplicaAssignment {
    fn default() -> Self {
        Self {
            partition_index: 0i32,
            broker_ids: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl CreatableReplicaAssignment {
    pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableReplicaAssignment {
        crate::owned::create_topics_request::CreatableReplicaAssignment {
            partition_index: (self.partition_index),
            broker_ids: (self.broker_ids).clone(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for CreatableReplicaAssignment {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 5;
        if version >= 0 { put_i32(buf, self.partition_index) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.broker_ids).len(), flex); for it in &self.broker_ids { put_i32(buf, *it); } } }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 5;
        let mut n: usize = 0;
        if version >= 0 { n += 4; }
        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.broker_ids).len(), flex); let body: usize = (self.broker_ids).iter().map(|_| 4).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 CreatableReplicaAssignment {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 5;
        let mut out = Self::default();
        if version >= 0 { out.partition_index = get_i32(buf)?; }
        if version >= 0 { out.broker_ids = { 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 }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreatableTopicConfig<'a> {
    pub name: &'a str,
    pub value: Option<&'a str>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for CreatableTopicConfig<'a> {
    fn default() -> Self {
        Self {
            name: "",
            value: None,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> CreatableTopicConfig<'a> {
    pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopicConfig {
        crate::owned::create_topics_request::CreatableTopicConfig {
            name: (self.name).to_string(),
            value: (self.value).map(|s| s.to_string()),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for CreatableTopicConfig<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 5;
        if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
        if version >= 0 { if flex { put_compact_nullable_string(buf, self.value) } else { put_nullable_string(buf, self.value) } }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 5;
        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_nullable_string_len(self.value) } else { nullable_string_len(self.value) }; }
        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 CreatableTopicConfig<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 5;
        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.value = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}