odradek-protocol 0.1.0

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

/// `ListGroupsResponse`, versions 0-5.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListGroupsResponse {
    /// 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 error code, or 0 if there was no error.
    pub error_code: i16,
    /// Each group in the response.
    pub groups: Vec<ListedGroup>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ListGroupsResponse {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0,
            error_code: 0,
            groups: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

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

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

    /// Decode one `ListGroupsResponse`, 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 `ListGroupsResponse` 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 `ListGroupsResponse` 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.throttle_time_ms = wire::get_i32(buf)?;
        }
        this.error_code = wire::get_i16(buf)?;
        this.groups = {
            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 = ListedGroup::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)
    }
}

impl crate::Message for ListGroupsResponse {
    const API_KEY: i16 = 16;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 5;

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

/// `ListedGroup` (nested in [`ListGroupsResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListedGroup {
    /// The group ID.
    pub group_id: String,
    /// The group protocol type.
    pub protocol_type: String,
    /// The group state name.
    pub group_state: String,
    /// The group type name.
    pub group_type: String,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ListedGroup {
    fn default() -> Self {
        Self {
            group_id: String::new(),
            protocol_type: String::new(),
            group_state: String::new(),
            group_type: String::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ListedGroup {
    /// Encode this `ListedGroup` 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.protocol_type);
        } else {
            wire::put_string(buf, &self.protocol_type)?;
        }
        if version >= 4 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.group_state);
            } else {
                wire::put_string(buf, &self.group_state)?;
            }
        }
        if version >= 5 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.group_type);
            } else {
                wire::put_string(buf, &self.group_type)?;
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ListedGroup`, 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 `ListedGroup` 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 `ListedGroup` 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.protocol_type = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        if version >= 4 {
            this.group_state = if is_flexible(version) {
                wire::get_compact_string(buf)?
            } else {
                wire::get_string(buf)?
            };
        }
        if version >= 5 {
            this.group_type = 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)
    }
}