odradek-protocol 0.1.0

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

/// `ConsumerGroupHeartbeatResponse`, versions 0-1.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ConsumerGroupHeartbeatResponse {
    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
    pub throttle_time_ms: i32,
    /// The top-level error code, or 0 if there was no error.
    pub error_code: i16,
    /// The top-level error message, or null if there was no error.
    pub error_message: Option<String>,
    /// The member id is generated by the consumer starting from version 1, while in version 0, it can be provided by users or generated by the group coordinator.
    pub member_id: Option<String>,
    /// The member epoch.
    pub member_epoch: i32,
    /// The heartbeat interval in milliseconds.
    pub heartbeat_interval_ms: i32,
    /// null if not provided; the assignment otherwise.
    pub assignment: Option<Assignment>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ConsumerGroupHeartbeatResponse {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0,
            error_code: 0,
            error_message: None,
            member_id: None,
            member_epoch: 0,
            heartbeat_interval_ms: 0,
            assignment: None,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ConsumerGroupHeartbeatResponse {
    /// This message's api key in Kafka's registry.
    pub const API_KEY: i16 = 68;
    /// 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 = 1;

    /// Encode this `ConsumerGroupHeartbeatResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i32(self.throttle_time_ms);
        buf.put_i16(self.error_code);
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.error_message.as_deref());
        } else {
            wire::put_nullable_string(buf, self.error_message.as_deref())?;
        }
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.member_id.as_deref());
        } else {
            wire::put_nullable_string(buf, self.member_id.as_deref())?;
        }
        buf.put_i32(self.member_epoch);
        buf.put_i32(self.heartbeat_interval_ms);
        match &self.assignment {
            Some(v) => {
                buf.put_i8(1);
                v.encode(buf, version)?;
            }
            None => {
                buf.put_i8(-1);
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ConsumerGroupHeartbeatResponse`, 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 `ConsumerGroupHeartbeatResponse` 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 `ConsumerGroupHeartbeatResponse` 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.throttle_time_ms = wire::get_i32(buf)?;
        this.error_code = wire::get_i16(buf)?;
        this.error_message = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.member_id = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.member_epoch = wire::get_i32(buf)?;
        this.heartbeat_interval_ms = wire::get_i32(buf)?;
        this.assignment = if wire::get_i8(buf)? < 0 {
            None
        } else {
            Some(Assignment::decode_with_budget(buf, version, budget)?)
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for ConsumerGroupHeartbeatResponse {
    const API_KEY: i16 = 68;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 1;

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

/// `Assignment` (nested in [`ConsumerGroupHeartbeatResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Assignment {
    /// The partitions assigned to the member that can be used immediately.
    pub topic_partitions: Vec<TopicPartitions>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for Assignment {
    fn default() -> Self {
        Self {
            topic_partitions: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl Assignment {
    /// Encode this `Assignment` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.topic_partitions.len()));
        } else {
            wire::put_array_len(buf, Some(self.topic_partitions.len()))?;
        }
        for item in &self.topic_partitions {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `Assignment`, 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 `Assignment` 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 `Assignment` 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.topic_partitions = {
            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 = TopicPartitions::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)
    }
}

/// `TopicPartitions` (nested in [`ConsumerGroupHeartbeatResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TopicPartitions {
    /// The topic ID.
    pub topic_id: [u8; 16],
    /// The partitions.
    pub partitions: Vec<i32>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

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

impl TopicPartitions {
    /// Encode this `TopicPartitions` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        wire::put_uuid(buf, self.topic_id);
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.partitions.len()));
        } else {
            wire::put_array_len(buf, Some(self.partitions.len()))?;
        }
        for item in &self.partitions {
            buf.put_i32(*item);
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `TopicPartitions`, 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 `TopicPartitions` 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 `TopicPartitions` 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.topic_id = wire::get_uuid(buf)?;
        this.partitions = {
            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 {
                        budget.push(&mut items, wire::get_i32(buf)?)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}