odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
Documentation
//! `ProduceRequest` — generated from `schemas/ProduceRequest.json`.
//!
//! @generated by `cargo xtask codegen`. Do not edit by hand.

#![allow(
    clippy::field_reassign_with_default,
    clippy::nonminimal_bool,
    clippy::derivable_impls,
    clippy::borrow_deref_ref,
    clippy::double_parens,
    unused_parens
)]

use bytes::{Buf, BufMut};

use crate::budget::{Budget, Limits};
#[allow(unused_imports)]
use crate::error::{DecodeError, EncodeError};
use crate::wire::{self, RawTaggedField};

/// Whether `version` of this message uses the flexible (compact) encoding.
pub fn is_flexible(version: i16) -> bool {
    let _ = version;
    version >= 9
}

/// `ProduceRequest`, versions 3-13.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ProduceRequest {
    /// The transactional ID, or null if the producer is not transactional.
    pub transactional_id: Option<String>,
    /// The number of acknowledgments the producer requires the leader to have received before considering a request complete. Allowed values: 0 for no acknowledgments, 1 for only the leader and -1 for the full ISR.
    pub acks: i16,
    /// The timeout to await a response in milliseconds.
    pub timeout_ms: i32,
    /// Each topic to produce to.
    pub topic_data: Vec<TopicProduceData>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ProduceRequest {
    fn default() -> Self {
        Self {
            transactional_id: None,
            acks: 0,
            timeout_ms: 0,
            topic_data: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ProduceRequest {
    /// This message's api key in Kafka's registry.
    pub const API_KEY: i16 = 0;
    /// The lowest schema version this snapshot can speak.
    pub const MIN_VERSION: i16 = 3;
    /// The highest schema version this snapshot can speak.
    pub const MAX_VERSION: i16 = 13;

    /// Encode this `ProduceRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.transactional_id.as_deref());
        } else {
            wire::put_nullable_string(buf, self.transactional_id.as_deref())?;
        }
        buf.put_i16(self.acks);
        buf.put_i32(self.timeout_ms);
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.topic_data.len()));
        } else {
            wire::put_array_len(buf, Some(self.topic_data.len()))?;
        }
        for item in &self.topic_data {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ProduceRequest`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `ProduceRequest` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `ProduceRequest` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.transactional_id = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.acks = wire::get_i16(buf)?;
        this.timeout_ms = wire::get_i32(buf)?;
        this.topic_data = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => return Err(DecodeError::InvalidLength(-1)),
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item = TopicProduceData::decode_with_budget(buf, version, budget)?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for ProduceRequest {
    const API_KEY: i16 = 0;
    const MIN_VERSION: i16 = 3;
    const MAX_VERSION: i16 = 13;

    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        ProduceRequest::encode(self, buf, version)
    }
    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        ProduceRequest::decode(buf, version)
    }
    fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        ProduceRequest::decode_with_limits(buf, version, limits)
    }
}

/// `TopicProduceData` (nested in [`ProduceRequest`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TopicProduceData {
    /// The topic name.
    pub name: String,
    /// The unique topic ID
    pub topic_id: [u8; 16],
    /// Each partition to produce to.
    pub partition_data: Vec<PartitionProduceData>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for TopicProduceData {
    fn default() -> Self {
        Self {
            name: String::new(),
            topic_id: [0u8; 16],
            partition_data: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl TopicProduceData {
    /// Encode this `TopicProduceData` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version <= 12 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.name);
            } else {
                wire::put_string(buf, &self.name)?;
            }
        }
        if version >= 13 {
            wire::put_uuid(buf, self.topic_id);
        }
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.partition_data.len()));
        } else {
            wire::put_array_len(buf, Some(self.partition_data.len()))?;
        }
        for item in &self.partition_data {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `TopicProduceData`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `TopicProduceData` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `TopicProduceData` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        if version <= 12 {
            this.name = if is_flexible(version) {
                wire::get_compact_string(buf)?
            } else {
                wire::get_string(buf)?
            };
        }
        if version >= 13 {
            this.topic_id = wire::get_uuid(buf)?;
        }
        this.partition_data = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => return Err(DecodeError::InvalidLength(-1)),
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item = PartitionProduceData::decode_with_budget(buf, version, budget)?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

/// `PartitionProduceData` (nested in [`ProduceRequest`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct PartitionProduceData {
    /// The partition index.
    pub index: i32,
    /// The record data to be produced.
    pub records: Option<bytes::Bytes>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for PartitionProduceData {
    fn default() -> Self {
        Self {
            index: 0,
            records: Some(bytes::Bytes::new()),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl PartitionProduceData {
    /// Encode this `PartitionProduceData` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i32(self.index);
        if is_flexible(version) {
            wire::put_compact_nullable_bytes(buf, self.records.as_deref());
        } else {
            wire::put_nullable_bytes(buf, self.records.as_deref())?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `PartitionProduceData`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `PartitionProduceData` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `PartitionProduceData` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.index = wire::get_i32(buf)?;
        this.records = if is_flexible(version) {
            wire::get_compact_nullable_bytes(buf)?
        } else {
            wire::get_nullable_bytes(buf)?
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}