odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
Documentation
//! `AddOffsetsToTxnRequest` — generated from `schemas/AddOffsetsToTxnRequest.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 >= 3
}

/// `AddOffsetsToTxnRequest`, versions 0-4.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AddOffsetsToTxnRequest {
    /// The transactional id corresponding to the transaction.
    pub transactional_id: String,
    /// Current producer id in use by the transactional id.
    pub producer_id: i64,
    /// Current epoch associated with the producer id.
    pub producer_epoch: i16,
    /// The unique group identifier.
    pub group_id: String,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for AddOffsetsToTxnRequest {
    fn default() -> Self {
        Self {
            transactional_id: String::new(),
            producer_id: 0,
            producer_epoch: 0,
            group_id: String::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

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

    /// Encode this `AddOffsetsToTxnRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.transactional_id);
        } else {
            wire::put_string(buf, &self.transactional_id)?;
        }
        buf.put_i64(self.producer_id);
        buf.put_i16(self.producer_epoch);
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.group_id);
        } else {
            wire::put_string(buf, &self.group_id)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `AddOffsetsToTxnRequest`, 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 `AddOffsetsToTxnRequest` 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 `AddOffsetsToTxnRequest` 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_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.producer_id = wire::get_i64(buf)?;
        this.producer_epoch = wire::get_i16(buf)?;
        this.group_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for AddOffsetsToTxnRequest {
    const API_KEY: i16 = 25;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 4;

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