kacrab-protocol 0.1.0

Kafka wire protocol types — generated from upstream message schemas by `kacrab-codegen`.
Documentation
//! Generated from ConsumerGroupHeartbeatRequest.json - DO NOT EDIT
#![allow(
    missing_docs,
    clippy::all,
    clippy::pedantic,
    clippy::nursery,
    clippy::arithmetic_side_effects,
    reason = "Generated protocol modules mirror Kafka's schema shape and intentionally trade \
              hand-written lint style for reproducible wire-code output."
)]
use bytes::{Bytes, BytesMut};

use crate::*;

#[derive(Debug, Clone, PartialEq)]
pub struct ConsumerGroupHeartbeatRequestData {
    /// The group identifier.
    pub group_id: KafkaString,
    /// The member id generated by the consumer. The member id must be kept during the entire
    /// lifetime of the consumer process.
    pub member_id: KafkaString,
    /// 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<KafkaString>,
    /// null if not provided or if it didn't change since the last heartbeat; the rack ID of
    /// consumer otherwise.
    pub rack_id: Option<KafkaString>,
    /// -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<KafkaString>>,
    /// null if it didn't change since the last heartbeat; the subscribed topic regex otherwise.
    pub subscribed_topic_regex: Option<KafkaString>,
    /// 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<KafkaString>,
    /// null if it didn't change since the last heartbeat; the partitions owned by the member.
    pub topic_partitions: Option<Vec<TopicPartitions>>,
    pub _unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for ConsumerGroupHeartbeatRequestData {
    fn default() -> Self {
        Self {
            group_id: KafkaString::default(),
            member_id: KafkaString::default(),
            member_epoch: 0_i32,
            instance_id: None,
            rack_id: None,
            rebalance_timeout_ms: -1i32,
            subscribed_topic_names: None,
            subscribed_topic_regex: None,
            server_assignor: None,
            topic_partitions: None,
            _unknown_tagged_fields: Vec::new(),
        }
    }
}
impl ConsumerGroupHeartbeatRequestData {
    pub fn with_group_id(mut self, value: KafkaString) -> Self {
        self.group_id = value;
        self
    }
    pub fn with_member_id(mut self, value: KafkaString) -> Self {
        self.member_id = value;
        self
    }
    pub fn with_member_epoch(mut self, value: i32) -> Self {
        self.member_epoch = value;
        self
    }
    pub fn with_instance_id(mut self, value: Option<KafkaString>) -> Self {
        self.instance_id = value;
        self
    }
    pub fn with_rack_id(mut self, value: Option<KafkaString>) -> Self {
        self.rack_id = value;
        self
    }
    pub fn with_rebalance_timeout_ms(mut self, value: i32) -> Self {
        self.rebalance_timeout_ms = value;
        self
    }
    pub fn with_subscribed_topic_names(mut self, value: Option<Vec<KafkaString>>) -> Self {
        self.subscribed_topic_names = value;
        self
    }
    pub fn with_subscribed_topic_regex(mut self, value: Option<KafkaString>) -> Self {
        self.subscribed_topic_regex = value;
        self
    }
    pub fn with_server_assignor(mut self, value: Option<KafkaString>) -> Self {
        self.server_assignor = value;
        self
    }
    pub fn with_topic_partitions(mut self, value: Option<Vec<TopicPartitions>>) -> Self {
        self.topic_partitions = value;
        self
    }
    pub fn read(buf: &mut Bytes, version: i16) -> Result<Self> {
        if version < 0 || version > 1 {
            return Err(UnsupportedVersion::new(68, version).into());
        }
        let group_id;
        let member_id;
        let member_epoch;
        let instance_id;
        let rack_id;
        let rebalance_timeout_ms;
        let subscribed_topic_names;
        let mut subscribed_topic_regex = None;
        let server_assignor;
        let topic_partitions;
        let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
        group_id = read_compact_string(buf)?;
        member_id = read_compact_string(buf)?;
        member_epoch = read_i32(buf)?;
        instance_id = read_compact_nullable_string(buf)?;
        rack_id = read_compact_nullable_string(buf)?;
        rebalance_timeout_ms = read_i32(buf)?;
        subscribed_topic_names = {
            let len = read_compact_array_length(buf)?;
            if len < 0 {
                None
            } else {
                let mut arr = Vec::with_capacity(len as usize);
                for _ in 0..len {
                    arr.push(read_compact_string(buf)?);
                }
                Some(arr)
            }
        };
        if version >= 1 {
            subscribed_topic_regex = read_compact_nullable_string(buf)?;
        }
        server_assignor = read_compact_nullable_string(buf)?;
        topic_partitions = {
            let len = read_compact_array_length(buf)?;
            if len < 0 {
                None
            } else {
                let mut arr = Vec::with_capacity(len as usize);
                for _ in 0..len {
                    arr.push(TopicPartitions::read(buf, version)?);
                }
                Some(arr)
            }
        };
        let tagged_fields = read_tagged_fields(buf)?;
        for field in &tagged_fields {
            match field.tag {
                _ => {
                    _unknown_tagged_fields.push(field.clone());
                },
            }
        }
        Ok(Self {
            group_id,
            member_id,
            member_epoch,
            instance_id,
            rack_id,
            rebalance_timeout_ms,
            subscribed_topic_names,
            subscribed_topic_regex,
            server_assignor,
            topic_partitions,
            _unknown_tagged_fields,
        })
    }
    pub fn write(&self, buf: &mut BytesMut, version: i16) -> Result<()> {
        if version < 0 || version > 1 {
            return Err(UnsupportedVersion::new(68, version).into());
        }
        write_compact_string(buf, &self.group_id)?;
        write_compact_string(buf, &self.member_id)?;
        write_i32(buf, self.member_epoch);
        write_compact_nullable_string(buf, self.instance_id.as_ref())?;
        write_compact_nullable_string(buf, self.rack_id.as_ref())?;
        write_i32(buf, self.rebalance_timeout_ms);
        match &self.subscribed_topic_names {
            None => {
                write_compact_array_length(buf, -1);
            },
            Some(arr) => {
                write_compact_array_length(buf, arr.len() as i32);
                for el in arr {
                    write_compact_string(buf, el)?;
                }
            },
        }
        if version >= 1 {
            write_compact_nullable_string(buf, self.subscribed_topic_regex.as_ref())?;
        } else if self.subscribed_topic_regex != None {
            return Err(UnsupportedFieldVersion::new(68, "subscribed_topic_regex", version).into());
        }
        write_compact_nullable_string(buf, self.server_assignor.as_ref())?;
        match &self.topic_partitions {
            None => {
                write_compact_array_length(buf, -1);
            },
            Some(arr) => {
                write_compact_array_length(buf, arr.len() as i32);
                for el in arr {
                    el.write(buf, version)?;
                }
            },
        }
        let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
        all_tags.sort_by_key(|f| f.tag);
        write_tagged_fields(buf, &all_tags)?;
        Ok(())
    }
    pub fn encoded_len(&self, version: i16) -> Result<usize> {
        if version < 0 || version > 1 {
            return Err(UnsupportedVersion::new(68, version).into());
        }
        let mut len: usize = 0;
        len += compact_string_len(&self.group_id)?;
        len += compact_string_len(&self.member_id)?;
        len += 4;
        len += compact_nullable_string_len(self.instance_id.as_ref())?;
        len += compact_nullable_string_len(self.rack_id.as_ref())?;
        len += 4;
        match &self.subscribed_topic_names {
            None => {
                len += compact_array_length_len(-1);
            },
            Some(arr) => {
                len += compact_array_length_len(arr.len() as i32);
                for el in arr {
                    len += compact_string_len(el)?;
                }
            },
        }
        if version >= 1 {
            len += compact_nullable_string_len(self.subscribed_topic_regex.as_ref())?;
        } else if self.subscribed_topic_regex != None {
            return Err(UnsupportedFieldVersion::new(68, "subscribed_topic_regex", version).into());
        }
        len += compact_nullable_string_len(self.server_assignor.as_ref())?;
        match &self.topic_partitions {
            None => {
                len += compact_array_length_len(-1);
            },
            Some(arr) => {
                len += compact_array_length_len(arr.len() as i32);
                for el in arr {
                    len += el.encoded_len(version)?;
                }
            },
        }
        let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
        all_tags.sort_by_key(|f| f.tag);
        len += tagged_fields_len(&all_tags)?;
        Ok(len)
    }
}
#[derive(Debug, Clone, PartialEq)]
pub struct TopicPartitions {
    /// The topic ID.
    pub topic_id: KafkaUuid,
    /// The partitions.
    pub partitions: Vec<i32>,
    pub _unknown_tagged_fields: Vec<RawTaggedField>,
}
impl Default for TopicPartitions {
    fn default() -> Self {
        Self {
            topic_id: KafkaUuid::ZERO,
            partitions: Vec::new(),
            _unknown_tagged_fields: Vec::new(),
        }
    }
}
impl TopicPartitions {
    pub fn with_topic_id(mut self, value: KafkaUuid) -> Self {
        self.topic_id = value;
        self
    }
    pub fn with_partitions(mut self, value: Vec<i32>) -> Self {
        self.partitions = value;
        self
    }
    pub fn read(buf: &mut Bytes, _version: i16) -> Result<Self> {
        let topic_id;
        let partitions;
        let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
        topic_id = read_uuid(buf)?;
        partitions = {
            let len = read_compact_array_length(buf)?;
            let mut arr = Vec::with_capacity(len.max(0) as usize);
            for _ in 0..len {
                arr.push(read_i32(buf)?);
            }
            arr
        };
        let tagged_fields = read_tagged_fields(buf)?;
        for field in &tagged_fields {
            match field.tag {
                _ => {
                    _unknown_tagged_fields.push(field.clone());
                },
            }
        }
        Ok(Self {
            topic_id,
            partitions,
            _unknown_tagged_fields,
        })
    }
    pub fn write(&self, buf: &mut BytesMut, _version: i16) -> Result<()> {
        write_uuid(buf, &self.topic_id);
        write_compact_array_length(buf, self.partitions.len() as i32);
        for el in &self.partitions {
            write_i32(buf, *el);
        }
        let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
        all_tags.sort_by_key(|f| f.tag);
        write_tagged_fields(buf, &all_tags)?;
        Ok(())
    }
    pub fn encoded_len(&self, _version: i16) -> Result<usize> {
        let mut len: usize = 0;
        len += 16;
        len += compact_array_length_len(self.partitions.len() as i32);
        len += self.partitions.len() * 4usize;
        let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
        all_tags.sort_by_key(|f| f.tag);
        len += tagged_fields_len(&all_tags)?;
        Ok(len)
    }
}