crabka-protocol 0.3.3

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_i32, put_bool, put_i32};
use crate::primitives::string_bytes::{
    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
    string_len,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 37;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 3;
pub const FLEXIBLE_MIN: i16 = 2;

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

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CreatePartitionsRequest {
    pub topics: Vec<CreatePartitionsTopic>,
    pub timeout_ms: i32,
    pub validate_only: bool,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for CreatePartitionsRequest {
    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 >= 0 {
            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 >= 0 {
            n += 1;
        }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}
impl Decode<'_> for CreatePartitionsRequest {
    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 >= 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(CreatePartitionsTopic::decode(buf, version)?);
                }
                v
            };
        }
        if version >= 0 {
            out.timeout_ms = get_i32(buf)?;
        }
        if version >= 0 {
            out.validate_only = get_bool(buf)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl CreatePartitionsRequest {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.topics = vec![CreatePartitionsTopic::populated(version)];
        }
        if version >= 0 {
            m.timeout_ms = 1i32;
        }
        if version >= 0 {
            m.validate_only = true;
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CreatePartitionsTopic {
    pub name: String,
    pub count: i32,
    pub assignments: Option<Vec<CreatePartitionsAssignment>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for CreatePartitionsTopic {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 2;
        if version >= 0 {
            if flex {
                put_compact_string(buf, &self.name);
            } else {
                put_string(buf, &self.name);
            }
        }
        if version >= 0 {
            put_i32(buf, self.count);
        }
        if version >= 0 {
            {
                let len = (self.assignments).as_ref().map(Vec::len);
                crate::primitives::array::put_nullable_array_len(buf, len, flex);
                if let Some(v) = &self.assignments {
                    for it in v {
                        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 >= 2;
        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 += {
                let opt: Option<&Vec<_>> = (self.assignments).as_ref();
                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
                    opt.map(std::vec::Vec::len),
                    flex,
                );
                let body: usize =
                    opt.map_or(0, |v| v.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 Decode<'_> for CreatePartitionsTopic {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 2;
        let mut out = Self::default();
        if version >= 0 {
            out.name = if flex {
                get_compact_string_owned(buf)?
            } else {
                get_string_owned(buf)?
            };
        }
        if version >= 0 {
            out.count = get_i32(buf)?;
        }
        if version >= 0 {
            out.assignments = {
                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(CreatePartitionsAssignment::decode(buf, version)?);
                        }
                        Some(v)
                    }
                }
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl CreatePartitionsTopic {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.name = "x".to_string();
        }
        if version >= 0 {
            m.count = 1i32;
        }
        if version >= 0 {
            m.assignments = Some(vec![CreatePartitionsAssignment::populated(version)]);
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CreatePartitionsAssignment {
    pub broker_ids: Vec<i32>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for CreatePartitionsAssignment {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 2;
        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 >= 2;
        let mut n: usize = 0;
        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 Decode<'_> for CreatePartitionsAssignment {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 2;
        let mut out = Self::default();
        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)
    }
}
#[cfg(test)]
impl CreatePartitionsAssignment {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.broker_ids = vec![1i32];
        }
        m
    }
}

/// 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();
    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
    obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
    obj.insert("validateOnly".to_string(), ::serde_json::Value::Bool(false));
    ::serde_json::Value::Object(obj)
}

impl crate::ProtocolRequest for CreatePartitionsRequest {
    const API_KEY: i16 = API_KEY;
    const MIN_VERSION: i16 = MIN_VERSION;
    const MAX_VERSION: i16 = MAX_VERSION;
    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
    type Response = super::create_partitions_response::CreatePartitionsResponse;
}