odradek-protocol 0.1.0

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

/// `ConsumerGroupHeartbeatRequest`, versions 0-1.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ConsumerGroupHeartbeatRequest {
    /// The group identifier.
    pub group_id: String,
    /// The member id generated by the consumer. The member id must be kept during the entire lifetime of the consumer process.
    pub member_id: String,
    /// The current member epoch; 0 to join the group; -1 to leave the group; -2 to indicate that the static member will rejoin.
    pub member_epoch: i32,
    /// null if not provided or if it didn't change since the last heartbeat; the instance Id otherwise.
    pub instance_id: Option<String>,
    /// null if not provided or if it didn't change since the last heartbeat; the rack ID of consumer otherwise.
    pub rack_id: Option<String>,
    /// -1 if it didn't change since the last heartbeat; the maximum time in milliseconds that the coordinator will wait on the member to revoke its partitions otherwise.
    pub rebalance_timeout_ms: i32,
    /// null if it didn't change since the last heartbeat; the subscribed topic names otherwise.
    pub subscribed_topic_names: Option<Vec<String>>,
    /// null if it didn't change since the last heartbeat; the subscribed topic regex otherwise.
    pub subscribed_topic_regex: Option<String>,
    /// null if not used or if it didn't change since the last heartbeat; the server side assignor to use otherwise.
    pub server_assignor: Option<String>,
    /// null if it didn't change since the last heartbeat; the partitions owned by the member.
    pub topic_partitions: Option<Vec<TopicPartitions>>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ConsumerGroupHeartbeatRequest {
    fn default() -> Self {
        Self {
            group_id: String::new(),
            member_id: String::new(),
            member_epoch: 0,
            instance_id: None,
            rack_id: None,
            rebalance_timeout_ms: -1,
            subscribed_topic_names: None,
            subscribed_topic_regex: None,
            server_assignor: None,
            topic_partitions: None,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ConsumerGroupHeartbeatRequest {
    /// 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 `ConsumerGroupHeartbeatRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        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_compact_string(buf, &self.member_id);
        } else {
            wire::put_string(buf, &self.member_id)?;
        }
        buf.put_i32(self.member_epoch);
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.instance_id.as_deref());
        } else {
            wire::put_nullable_string(buf, self.instance_id.as_deref())?;
        }
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.rack_id.as_deref());
        } else {
            wire::put_nullable_string(buf, self.rack_id.as_deref())?;
        }
        buf.put_i32(self.rebalance_timeout_ms);
        match &self.subscribed_topic_names {
            Some(items) => {
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, Some(items.len()));
                } else {
                    wire::put_array_len(buf, Some(items.len()))?;
                }
                for item in items {
                    if is_flexible(version) {
                        wire::put_compact_string(buf, item);
                    } else {
                        wire::put_string(buf, item)?;
                    }
                }
            }
            None => {
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, None);
                } else {
                    wire::put_array_len(buf, None)?;
                }
            }
        }
        if version >= 1 {
            if is_flexible(version) {
                wire::put_compact_nullable_string(buf, self.subscribed_topic_regex.as_deref());
            } else {
                wire::put_nullable_string(buf, self.subscribed_topic_regex.as_deref())?;
            }
        }
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.server_assignor.as_deref());
        } else {
            wire::put_nullable_string(buf, self.server_assignor.as_deref())?;
        }
        match &self.topic_partitions {
            Some(items) => {
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, Some(items.len()));
                } else {
                    wire::put_array_len(buf, Some(items.len()))?;
                }
                for item in items {
                    item.encode(buf, version)?;
                }
            }
            None => {
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, None);
                } else {
                    wire::put_array_len(buf, None)?;
                }
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ConsumerGroupHeartbeatRequest`, 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 `ConsumerGroupHeartbeatRequest` 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 `ConsumerGroupHeartbeatRequest` 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.group_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.member_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.member_epoch = wire::get_i32(buf)?;
        this.instance_id = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.rack_id = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.rebalance_timeout_ms = wire::get_i32(buf)?;
        this.subscribed_topic_names = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => None,
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        budget.push(
                            &mut items,
                            if is_flexible(version) {
                                wire::get_compact_string(buf)?
                            } else {
                                wire::get_string(buf)?
                            },
                        )?;
                    }
                    Some(items)
                }
            }
        };
        if version >= 1 {
            this.subscribed_topic_regex = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        this.server_assignor = if is_flexible(version) {
            wire::get_compact_nullable_string(buf)?
        } else {
            wire::get_nullable_string(buf)?
        };
        this.topic_partitions = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => None,
                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)?;
                    }
                    Some(items)
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for ConsumerGroupHeartbeatRequest {
    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> {
        ConsumerGroupHeartbeatRequest::encode(self, buf, version)
    }
    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        ConsumerGroupHeartbeatRequest::decode(buf, version)
    }
    fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        ConsumerGroupHeartbeatRequest::decode_with_limits(buf, version, limits)
    }
}

/// `TopicPartitions` (nested in [`ConsumerGroupHeartbeatRequest`]).
#[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)
    }
}