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::{Buf, BufMut};

use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_i8, put_bool, put_i16, put_i32, put_i8};
use crate::primitives::string_bytes::{
    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
    string_len,
};
use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{Decode, 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, Default)]
pub struct CreateTopicsResponse {
    pub throttle_time_ms: i32,
    pub topics: Vec<CreatableTopicResult>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for CreateTopicsResponse {
    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 >= 2 { put_i32(buf, self.throttle_time_ms) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { 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 >= 2 { n += 4; }
        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 flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> Decode<'de> for CreateTopicsResponse {
    fn decode<B: Buf>(buf: &mut B, 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 >= 2 { out.throttle_time_ms = get_i32(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(CreatableTopicResult::decode(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 CreatableTopicResult {
    pub name: String,
    pub topic_id: crate::primitives::uuid::Uuid,
    pub error_code: i16,
    pub error_message: Option<String>,
    pub num_partitions: i32,
    pub replication_factor: i16,
    pub configs: Option<Vec<CreatableTopicConfigs>>,
    pub topic_config_error_code: i16,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for CreatableTopicResult {
    fn default() -> Self {
        Self {
            name: String::new(),
            topic_id: Default::default(),
            error_code: 0i16,
            error_message: None,
            num_partitions: -1i32,
            replication_factor: -1i16,
            configs: None,
            topic_config_error_code: 0i16,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl Encode for CreatableTopicResult {
    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 >= 7 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
        if version >= 0 { put_i16(buf, self.error_code) }
        if version >= 1 { if flex { put_compact_nullable_string(buf, self.error_message.as_deref()) } else { put_nullable_string(buf, self.error_message.as_deref()) } }
        if version >= 5 { put_i32(buf, self.num_partitions) }
        if version >= 5 { put_i16(buf, self.replication_factor) }
        if version >= 5 { { let len = (self.configs).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.configs { for it in v { it.encode(buf, version)?; } } } }
        if flex {
            let mut tagged = WriteTaggedFields::new();
            if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
                let payload = encode_to_bytes(2, |b| { put_i16(b, self.topic_config_error_code); Ok(()) });
                tagged.add(0, payload);
            }
            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 >= 7 { n += 16; }
        if version >= 0 { n += 2; }
        if version >= 1 { n += if flex { compact_nullable_string_len(self.error_message.as_deref()) } else { nullable_string_len(self.error_message.as_deref()) }; }
        if version >= 5 { n += 4; }
        if version >= 5 { n += 2; }
        if version >= 5 { n += { let opt: Option<&Vec<_>> = (self.configs).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body }; }
        if flex {
            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
            if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
                known_pairs.push((0, 2));
            }
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> Decode<'de> for CreatableTopicResult {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 5;
        let mut out = Self::default();
        if version >= 0 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
        if version >= 7 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
        if version >= 0 { out.error_code = get_i16(buf)?; }
        if version >= 1 { out.error_message = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
        if version >= 5 { out.num_partitions = get_i32(buf)?; }
        if version >= 5 { out.replication_factor = get_i16(buf)?; }
        if version >= 5 { out.configs = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopicConfigs::decode(buf, version)?); } Some(v) } } }; }
        if flex {
            // Pre-declare typed slots for known tagged fields.
            let mut tag_topic_config_error_code = None;
            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
                match tag {
                0 => { tag_topic_config_error_code = Some({ let b: &mut &[u8] = payload; get_i16(b)? }); Ok(true) }
                    _ => Ok(false),
                }
            })?;
            if let Some(v) = tag_topic_config_error_code { out.topic_config_error_code = v; }
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreatableTopicConfigs {
    pub name: String,
    pub value: Option<String>,
    pub read_only: bool,
    pub config_source: i8,
    pub is_sensitive: bool,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for CreatableTopicConfigs {
    fn default() -> Self {
        Self {
            name: String::new(),
            value: None,
            read_only: false,
            config_source: -1i8,
            is_sensitive: false,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl Encode for CreatableTopicConfigs {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 5;
        if version >= 5 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
        if version >= 5 { if flex { put_compact_nullable_string(buf, self.value.as_deref()) } else { put_nullable_string(buf, self.value.as_deref()) } }
        if version >= 5 { put_bool(buf, self.read_only) }
        if version >= 5 { put_i8(buf, self.config_source) }
        if version >= 5 { put_bool(buf, self.is_sensitive) }
        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 >= 5 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
        if version >= 5 { n += if flex { compact_nullable_string_len(self.value.as_deref()) } else { nullable_string_len(self.value.as_deref()) }; }
        if version >= 5 { n += 1; }
        if version >= 5 { n += 1; }
        if version >= 5 { 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> Decode<'de> for CreatableTopicConfigs {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 5;
        let mut out = Self::default();
        if version >= 5 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
        if version >= 5 { out.value = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
        if version >= 5 { out.read_only = get_bool(buf)?; }
        if version >= 5 { out.config_source = get_i8(buf)?; }
        if version >= 5 { out.is_sensitive = get_bool(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
/// Only includes fields valid for the given version.
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
    let mut obj = ::serde_json::Map::new();
    if version >= 2 {
        obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
    }
    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
    ::serde_json::Value::Object(obj)
}