odradek-protocol 0.1.0

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

/// `ConsumerProtocolSubscription`, versions 0-3.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ConsumerProtocolSubscription {
    /// The topics that the member wants to consume.
    pub topics: Vec<String>,
    /// User data that will be passed back to the consumer.
    pub user_data: Option<bytes::Bytes>,
    /// The partitions that the member owns.
    pub owned_partitions: Vec<TopicPartition>,
    /// The generation id of the member.
    pub generation_id: i32,
    /// The rack id of the member.
    pub rack_id: Option<String>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ConsumerProtocolSubscription {
    fn default() -> Self {
        Self {
            topics: Vec::new(),
            user_data: None,
            owned_partitions: Vec::new(),
            generation_id: -1,
            rack_id: None,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ConsumerProtocolSubscription {
    /// 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 = 3;

    /// Encode this `ConsumerProtocolSubscription` 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.topics.len()));
        } else {
            wire::put_array_len(buf, Some(self.topics.len()))?;
        }
        for item in &self.topics {
            if is_flexible(version) {
                wire::put_compact_string(buf, item);
            } else {
                wire::put_string(buf, item)?;
            }
        }
        if is_flexible(version) {
            wire::put_compact_nullable_bytes(buf, self.user_data.as_deref());
        } else {
            wire::put_nullable_bytes(buf, self.user_data.as_deref())?;
        }
        if version >= 1 {
            if is_flexible(version) {
                wire::put_compact_array_len(buf, Some(self.owned_partitions.len()));
            } else {
                wire::put_array_len(buf, Some(self.owned_partitions.len()))?;
            }
            for item in &self.owned_partitions {
                item.encode(buf, version)?;
            }
        }
        if version >= 2 {
            buf.put_i32(self.generation_id);
        }
        if version >= 3 {
            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())?;
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ConsumerProtocolSubscription`, 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 `ConsumerProtocolSubscription` 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 `ConsumerProtocolSubscription` 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.topics = {
            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,
                            if is_flexible(version) {
                                wire::get_compact_string(buf)?
                            } else {
                                wire::get_string(buf)?
                            },
                        )?;
                    }
                    items
                }
            }
        };
        this.user_data = if is_flexible(version) {
            wire::get_compact_nullable_bytes(buf)?
        } else {
            wire::get_nullable_bytes(buf)?
        };
        if version >= 1 {
            this.owned_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 = TopicPartition::decode_with_budget(buf, version, budget)?;
                            budget.progress(mark, buf.remaining())?;
                            budget.push(&mut items, item)?;
                        }
                        items
                    }
                }
            };
        }
        if version >= 2 {
            this.generation_id = wire::get_i32(buf)?;
        }
        if version >= 3 {
            this.rack_id = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

/// `TopicPartition` (nested in [`ConsumerProtocolSubscription`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TopicPartition {
    /// The topic name.
    pub topic: String,
    /// The partition ids.
    pub partitions: Vec<i32>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for TopicPartition {
    fn default() -> Self {
        Self {
            topic: String::new(),
            partitions: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl TopicPartition {
    /// Encode this `TopicPartition` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version >= 1 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.topic);
            } else {
                wire::put_string(buf, &self.topic)?;
            }
        }
        if version >= 1 {
            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 `TopicPartition`, 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 `TopicPartition` 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 `TopicPartition` 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 >= 1 {
            this.topic = if is_flexible(version) {
                wire::get_compact_string(buf)?
            } else {
                wire::get_string(buf)?
            };
        }
        if version >= 1 {
            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)
    }
}